Sub topic 3
Handling Zero and Blank Denominators in DAX
Assume you know Power BI Desktop’s Report view and Fields pane, and you have a loaded Sales table. A measure calculates a result when a visual evaluates it. Its filter context includes slicers, filters, and the visual’s own row or column.
Handling zero and blank denominators
A ratio can fail when its denominator is zero or blank. Use DIVIDE instead of the / operator:
Total Sales = SUM(Sales[SalesAmount])
Order Lines = COUNTROWS(Sales)
Average Sales per Order Line =
DIVIDE([Total Sales], [Order Lines])With no order lines, DIVIDE returns BLANK(). You can provide an alternate result as its third argument:
Average Sales per Order Line (0 if none) =
DIVIDE([Total Sales], [Order Lines], 0)Use BLANK() when an empty result should disappear from a visual. Use 0 when the report must display an explicit zero.
Naming, formatting, and organizing measures
Give measures names that describe the business result, such as Total Sales or Average Sales per Order Line. Avoid names like Measure1.
After creating a measure, select it in the Fields pane. Set its format to currency, decimal, percentage, or whole number in the Measure tools ribbon. Store related measures in a dedicated table when the model grows. Display folders can group measures without moving their source tables.
Why explicit measures beat implicit aggregations
An implicit aggregation is created when you drag a numeric column into a visual and Power BI automatically chooses Sum, Average, or another operation. An explicit measure has a named DAX definition.
Explicit measures are easier to reuse, format, test, and change. They also make the business rule visible. For example, every visual can use [Total Sales] rather than separately guessing how to aggregate Sales[SalesAmount].
Counting rows of a filtered table
COUNTROWS can receive a filtered table expression:
Large Order Lines =
COUNTROWS(
FILTER(
Sales,
Sales[SalesAmount] > 1000
)
)FILTER returns only rows whose SalesAmount exceeds 1000. COUNTROWS then counts those rows in the current filter context.
Exercise
Which expression returns 0 when there are no order lines?
Show answer
DIVIDE([Total Sales], [Order Lines], 0)