Aggregations, iterators, blanks, and variables¶
Choose the right aggregation¶
| Requirement | Function/pattern | Common mistake |
|---|---|---|
| Sum a stored numeric column | SUM(Sales[SalesAmount]) |
Passing a multi-column expression into SUM |
| Sum a row expression | SUMX(Sales, Quantity * UnitPrice) |
Forgetting its row context or eligible table |
| Count eligible rows | COUNTROWS(Sales) |
Calling it a distinct customer/order count |
| Count a key's distinct values | DISTINCTCOUNT(Sales[SaleID]) |
Ignoring the key's grain or blank values |
| Safe ratio | DIVIDE(numerator, denominator) |
Returning zero where undefined should remain blank |
This dataset's SaleID identifies one transaction line. The Transaction Count is 12, not a promise about complete business orders in another dataset. DISTINCTCOUNT can count a blank key, so key quality must be checked.
Reuse base measures¶
Gross Profit = [Revenue] - [Cost]
Build derived measures from clear base measures so changes to the business definition happen in one place. Do not introduce multiple subtly different "Revenue" formulas into every report.
A blank is not always a zero¶
SUM over no eligible Sales rows returns BLANK. The default DIVIDE alternate
is also BLANK when the denominator is zero/blank. Clients may render it as an
empty cell. Use COALESCE([Revenue], 0) for a deliberate presentation requirement,
not to conceal a relationship or missing-data problem.
DAX blank coercion depends on the operator. Avoid assuming SQL NULL behavior
or that every comparison treats zero and blank differently; use ISBLANK
when the distinction is the requirement.
Variables are evaluated values, not delayed formulas¶
Revenue YoY % =
VAR PreviousRevenue = [Revenue PY]
RETURN
DIVIDE ( [Revenue] - PreviousRevenue, PreviousRevenue )
PreviousRevenue is evaluated where the variable is defined. It improves readability and can avoid repeating the same expression.
This pattern does not recalculate a scalar under the new product context:
VAR CurrentRevenue = [Revenue]
RETURN CALCULATE ( CurrentRevenue, REMOVEFILTERS ( DimProduct ) )
For that goal, evaluate the measure inside CALCULATE instead, as in All Products Revenue. Do not generalize the scalar mistake into a rule that variables are bad.
Exercise: compare the two denominator patterns in a ProductName matrix. Record the row and grand-total results. Sources: SUMX, DIVIDE, and DAX variables.