Rules / DAX Expressions

Use the DIVIDE function for division

warning USE_THE_DIVIDE_FUNCTION_FOR_DIVISION · ported · model layer · scope: Measure, CalculatedColumn, CalculationItem

What it checks

Expressions that use the division operator right after a closing bracket or parenthesis, such as [Sales] / [Cost] or SUM(...) / SUM(...). A slash that starts a comment is ignored.

Each finding names the object that holds the expression: a measure as [Average Price], a calculated column as 'Sales'[Unit Price], a calculation item by its name with its calculation group in the detail.

Example

Fires the rule
table Sales
	column Amount
		dataType: decimal
		sourceColumn: Amount
	column Quantity
		dataType: int64
		sourceColumn: Quantity
	measure 'Average Price' = SUM(Sales[Amount]) / SUM(Sales[Quantity])
		formatString: #,0.00
After the fix
table Sales
	column Amount
		dataType: decimal
		sourceColumn: Amount
	column Quantity
		dataType: int64
		sourceColumn: Quantity
	measure 'Average Price' = DIVIDE(SUM(Sales[Amount]), SUM(Sales[Quantity]))
		formatString: #,0.00

Why it matters

Dividing by a zero or blank denominator with / produces an error, and an error in one cell takes down the whole visual with a generic message. DIVIDE returns blank in that case, or an alternate result you choose, so the visual shows a gap where the data has one instead of failing.

How to fix it

Pass the numerator and the denominator as the first two arguments, and add a third where blank is the wrong answer for an empty denominator:

Average Price = DIVIDE ( SUM ( Sales[Amount] ), SUM ( Sales[Quantity] ) )

In Power BI Desktop, select the measure in the Data pane and edit it in the formula bar; a calculated column and a calculation item are edited the same way. In the TMDL file, edit the expression after measure 'Average Price' =, after column Name = for a calculated column, or after calculationItem Name = in the calculation group.

When to ignore it

A denominator that cannot be zero makes the guard dead weight. Dividing by a literal, [Total Sales] / 12 for a monthly average, is the clearest case: there is no blank to protect against, and the operator says what it means. The same goes for a denominator the source constrains to be positive, as long as you can point at the constraint rather than at the data you happen to have today. Everywhere else, check what the visual should show when the denominator is empty and put that in DIVIDE's third argument.

To ignore this rule on one object, add annotation pbiplint.ignore = USE_THE_DIVIDE_FUNCTION_FOR_DIVISION under the object in its TMDL file. Power BI Desktop keeps the annotation. To turn the rule off for a whole project, set "USE_THE_DIVIDE_FUNCTION_FOR_DIVISION": "off" under rules in pbiplint.config.json.

Quirks

Ported from Microsoft's Best Practice Analyzer ruleset (opens in a new tab).

Check a model for this Improve this page (opens in a new tab)