Sub topic 7
Measures inside iterators and context transition
This chapter assumes you can write basic measures with SUM and CALCULATE, and that you know what row context and filter context are. You will see why a measure reference behaves differently inside an iterator than it does in a plain column expression.
The problem: a row context does not filter
An iterator such as SUMX or AVERAGEX creates a row context. That row context points at one row of the table being iterated, but it does not filter any table. A measure evaluates against the filter context only. So a measure called inside an iterator would, on its own, ignore the current row.
Context transition fixes this. When CALCULATE is evaluated inside a row context, DAX converts each column of the current row into a filter. Those filters are added to the filter context, and the expression is then evaluated.
Measure references carry an implicit CALCULATE
Every time you reference a measure, DAX wraps it in CALCULATE. Inside an iterator, that wrapper triggers context transition. These two measures are equivalent:
Avg Sales per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)Avg Sales per Customer Explicit =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
CALCULATE ( [Sales Amount] )
)For each customer key, the row context is turned into a filter on Customer[CustomerKey]. [Sales Amount] then returns the sales for that one customer, and AVERAGEX averages those values.
Without a measure, the row is ignored
Compare the version above with one that uses a bare column aggregation:
Broken Avg =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
SUM ( Sales[Quantity] )
)SUM is not a measure reference, so there is no implicit CALCULATE. No context transition happens. Every customer gets the same value, the total quantity in the current filter context, and the average equals that total. To get per-customer quantities, write CALCULATE ( SUM ( Sales[Quantity] ) ) or define a measure and reference it.
What context transition does to columns
Context transition filters on every column of the current row, not only the key. If the iterated table has no unique key, rows with identical values are filtered together. In a table with duplicate rows, the transition returns the combined result of all duplicates, and the iterator counts that combined value once per duplicate. This inflates totals.
Iterating over a table with a unique column, such as VALUES ( Customer[CustomerKey] ) or a dimension table, avoids this problem.
Common mistakes
- Using an aggregation function such as SUM inside an iterator and expecting it to follow the current row.
- Iterating over a fact table and calling a measure on every row. This is correct but slow, because each row triggers a context transition.
- Iterating over a table with duplicate rows, which makes context transition double count.
Exercise
The measure Total Qty per Product = SUMX ( VALUES ( Product[ProductKey] ), SUM ( Sales[Quantity] ) ) returns the same number for every product in a matrix. Why, and how do you fix it?
Show answer
SUM is a column aggregation, not a measure reference, so no context transition occurs. The row context on ProductKey does not filter Sales, and each iteration returns the total quantity for the current filter context. Fix it by wrapping the aggregation in CALCULATE, as in CALCULATE ( SUM ( Sales[Quantity] ) ), or by referencing a measure such as [Total Quantity].
Exercise
Why is it safer to iterate over VALUES ( Customer[CustomerKey] ) than over a table that may contain duplicate rows when a measure is called inside the iterator?
Show answer
Context transition filters on all columns of the current row. With duplicate rows, each duplicate filters to the same set of rows, so the measure returns the combined value for each duplicate and the total is counted more than once. A unique key column gives one iteration per member.