Sub topic 8
Handling zero and blank denominators with an alternate result
This chapter assumes you have already written Total Sales with SUM, Order Lines with COUNTROWS, and a ratio measure with DIVIDE. It also assumes you know that slicers and visuals change the rows a measure sees. Here you learn what DIVIDE returns when the denominator is zero or blank, and how to choose that result yourself.
The problem: dividing by nothing
A ratio such as average sales per order line has a denominator that changes with every filter. Pick a region, product, or month with no sales, and the denominator becomes zero or blank. The / operator then returns a value that spoils the visual:
| Expression | Result |
|---|---|
| `10 / 0` | Infinity |
| `0 / 0` | NaN |
| `DIVIDE(10, 0)` | BLANK |
Infinity and NaN show up as text in cards and tables. You would have to wrap every division in an IF to hide them. DIVIDE does that check for you.
DIVIDE with the default result
You already have this measure from the earlier chapter:
Average Sales per Order Line = DIVIDE([Total Sales], [Order Lines])When [Order Lines] is zero or blank in the current filter context, the measure returns BLANK. Power BI treats BLANK as "nothing to show." In a table or bar chart, categories whose result is BLANK are usually left out, so empty combinations don't clutter the report.
Choosing an alternate result
DIVIDE takes an optional third argument. It is the value returned when the denominator is zero or blank:
Average Sales per Order Line (0 if none) = DIVIDE([Total Sales], [Order Lines], 0)Now the same empty filter context shows 0. The alternate result is not a general default. DIVIDE uses it only when the denominator is zero or blank. If the denominator has a valid value, you get the normal quotient.
Blank or zero: which should you pick?
The two choices behave differently in a report. The right one depends on what the visual should say.
- BLANK (default). Use it when "no data" is the honest answer. Visuals hide empty categories, and averages or totals built on top of the measure skip those rows.
- 0. Use it when zero is a meaningful result, such as "no sales means a rate of 0." Every category now appears in the visual, including those with no activity. Charts show a zero-height bar instead of no bar.
A zero can also mislead. An average of 0 for a region with no orders looks the same as a region whose orders all had zero value. If readers might confuse the two, keep BLANK.
Average Sales per Order Line (0 if none) = DIVIDE([Total Sales], [Order Lines], 0)The alternate result is an ordinary DAX expression, so it can be a number such as 0 or -1. Keep it a number. Returning text such as "n/a" from a measure that otherwise returns numbers causes type problems and breaks number formatting.
Seeing it in a report
Follow these steps to compare the behavior:
- Add a table visual with
Sales[Region](or any category column) and your two ratio measures. - Add a slicer for a column such as year or product.
- Pick a slicer value that leaves some categories with no sales.
- Compare the two columns. The default measure drops those categories or shows an empty cell. The alternate-result measure lists them with
0.
If the table shows no difference, check that the categories actually have no Sales rows in the current filter. A category with rows whose SalesAmount is 0 is a different case. The numerator is zero but the denominator, Order Lines, is not, so the quotient is a normal 0.
Common errors
- Using
/and wrapping it in IF.IF([Order Lines] = 0, 0, [Total Sales] / [Order Lines])works, but it is longer and treats blank and zero differently from DIVIDE. Prefer DIVIDE. - Putting the fallback in the wrong position. The alternate result is the third argument.
DIVIDE([Total Sales], 0, [Order Lines])divides by a constant zero and returns[Order Lines]every time. - Expecting the alternate result to cover a blank numerator. If
[Total Sales]is blank and the denominator is valid, the result is still BLANK. The alternate result applies to the denominator only. - Choosing 0 everywhere. A report full of zeros for empty combinations hides which cells have no data.
Exercise
A measure is defined as DIVIDE([Total Sales], [Order Lines], 0). A slicer selects a year with no Sales rows. What does a card showing the measure display, and what would it display if the third argument were removed?
Show answer
The card shows 0, because [Order Lines] is zero or blank in that filter context, so DIVIDE returns the alternate result. Without the third argument, DIVIDE returns BLANK, and the card shows an empty value.