Sub topic 9
Why a measure reference acts like CALCULATE
This chapter assumes you know the difference between a measure and a calculated column. It also assumes you can read a simple SUMX and have seen CALCULATE with a filter argument. Here you will see why writing a measure name inside another expression changes the result when a row context is active.
The rule
Every time DAX evaluates a measure reference, it wraps the reference in an implicit CALCULATE. Writing [Sales Amount] behaves like writing CALCULATE ( [Sales Amount] ).
When no row context is active, the wrapper changes nothing. CALCULATE with no filter arguments leaves the filter context as it is. When a row context is active, the wrapper does something. CALCULATE converts the current row into filters. That conversion is context transition, which the next chapter covers in detail.
Measure reference versus column reference
A column reference such as Sales[Quantity] reads a value from the current row. A measure reference does not read a value from the row. It evaluates a whole aggregation in a filter context. In a row context, the implicit CALCULATE makes that filter context include the current row.
The two calculated columns below show the difference. Both are in the Product table, and both aggregate Sales.
-- Calculated column in the Product table
Sales Without Transition =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )The row context of the Product row does not filter Sales. Every product row shows the same grand total.
-- Calculated column in the Product table
Product Sales = [Sales Amount]Here [Sales Amount] is a measure reference, so it carries an implicit CALCULATE. The current Product row becomes a filter. That filter reaches Sales through the relationship, and each product shows its own sales.
The measure used above is defined like this:
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )The body of Sales Amount is the same expression as the first column. The only difference is how it is called. Inlining the expression gives the grand total. Referencing the measure gives the per-product value.
Where this shows up in practice
Iterators open a row context for every row they scan. A measure reference inside the iterator therefore transitions for each row.
Average Sales per Customer =
AVERAGEX ( Customer, [Sales Amount] )AVERAGEX opens a row context on each Customer row. The reference to [Sales Amount] turns that row into a filter on Customer. The measure is then computed for that customer alone, and AVERAGEX averages the results.
Products Over 1000 =
COUNTROWS (
FILTER ( Product, [Sales Amount] > 1000 )
)FILTER iterates Product. For each product, the measure reference evaluates sales for that product only. The measure counts the products whose sales exceed 1000.
Common errors
- Expecting a grand total in an iterator. If you reference a measure inside an iterator, each row gets its own value. Use
ALLorREMOVEFILTERSinsideCALCULATEif you want the total. - Expecting a per-row value from an inlined expression. Copying a measure's body into a calculated column removes the implicit
CALCULATE. Reference the measure by name instead, or writeCALCULATEyourself. - Forgetting the relationship. The row becomes a filter on its own table. That filter reaches another table only through a relationship, so it needs a path to the table you aggregate.
Summary
| Expression in a row context | Implicit CALCULATE? | Result |
|---|---|---|
| `Sales[Quantity]` | No | Value from the current row |
| `SUMX ( Sales, ... )` written inline | No | Aggregation over the current filter context, often the grand total |
| `[Sales Amount]` | Yes | Aggregation filtered to the current row |
Exercise
In a calculated column in the Customer table, you write Total = SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ) and every customer shows the same number. You then define the same expression as a measure [Sales Amount] and change the column to Total = [Sales Amount]. Why does each customer now show a different number?
Show answer
A measure reference is wrapped in an implicit CALCULATE. In the calculated column's row context, CALCULATE converts the current Customer row into a filter, and the filter reaches Sales through the relationship. The inline SUMX has no such wrapper, so the row context of Customer does not filter Sales and each row returns the grand total.