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 the difference between row context and filter context from the earlier chapters. It covers one behavior: what happens when an iterator calls a measure for each row.
The problem: row context does not filter
An iterator such as SUMX or AVERAGEX creates a row context. The row context says "the current row is this one", and column references like Sales[Quantity] use it to read a value from that row.
A row context does not filter anything. If you call an aggregation such as SUM inside the iterator, SUM only sees the filter context. It does not see the row you are on.
Here is a measure that looks reasonable and gives the wrong answer. It assumes the Sales table has a Quantity column.
Wrong Avg Quantity per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
SUM ( Sales[Quantity] )
)For each customer, SUM evaluates in the same filter context as the whole visual. The result is the total quantity sold for every customer. The average of N identical totals is that total, so the measure returns the total quantity, not an average.
Context transition
A measure reference behaves differently. When you write [Sales Amount] inside an iterator, DAX treats it as CALCULATE ( [Sales Amount] ). CALCULATE converts the current row context into an equivalent filter context before evaluating its expression. This conversion is called context transition.
The corrected measure from this tutorial's plan uses it:
Avg Sales per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)Take the iteration step where the current customer is key 1042. The row context holds Customer[CustomerKey] = 1042. Context transition adds the filter Customer[CustomerKey] = 1042 to the existing filter context. [Sales Amount] then runs against only that customer's sales. The relationship carries the filter from Customer to Sales.
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)You can fix the earlier wrong measure the same way, by wrapping the aggregation in CALCULATE:
Avg Quantity per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
CALCULATE ( SUM ( Sales[Quantity] ) )
)What context transition filters on
Context transition works on every column of the row context, not only on the column you named in VALUES. The effect depends on which table you iterate over.
- Iterating over
VALUES ( Customer[CustomerKey] ). The row context has one column, so the transition adds one filter. This is cheap and predictable. - Iterating over a dimension table with a unique key. The row context has all columns of the table, and the transition filters on all of them. Because the key is unique, the filter identifies one row, so the result is correct.
- Iterating over a table that can have duplicate rows. If two rows have identical values in every column, the transition filters on those values and matches both rows. A measure that sums over the table then counts each of the two rows twice, once for each iteration step.
The third case is the one that causes bad numbers. Suppose a fact table has no key column and two sales lines are identical in every column. Calling a measure per row of that table doubles their contribution. Iterating over a fact table and calling a measure on it is rarely what you want. If you iterate over Sales, use column expressions such as Sales[Quantity] * Sales[Net Price] directly.
How blanks behave
AVERAGEX ignores rows where the expression returns blank. This matters when the measure inside the iterator returns blank for some members.
Avg Daily Sales =
AVERAGEX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
)This measure shows the context transition on dates. A date with no sales makes [Sales Amount] return blank, and AVERAGEX leaves it out of the average. The result is the average over days that had sales, not over every calendar day.
The same applies to customers. Filters on the Date table reach Sales, but they do not reach the Customer table. VALUES ( Customer[CustomerKey] ) therefore still lists every customer, including those with no sales in the selected period. Their [Sales Amount] is blank, so AVERAGEX skips them. The result is the average per customer who bought something.
If you want zero to count, convert blanks before they reach AVERAGEX:
Avg Daily Sales Including Zero Days =
AVERAGEX (
VALUES ( 'Date'[Date] ),
[Sales Amount] + 0
)Adding zero turns a blank into 0, so every date in the current filter context counts. If the Date table holds dates far outside your data range, those dates are included too. Limit the Date table in the visual or in the formula when that is a problem.
Calling a measure versus repeating its logic
You can avoid context transition by writing the logic inside the iterator against the iterated table. Both versions below return the same result in a typical star schema.
Total Cost via Measure =
SUMX (
VALUES ( Product[ProductKey] ),
[Sales Amount] * RELATED ( Product[Unit Cost] ) / [Sales Amount]
)That formula is convoluted and slower than the direct approach. This one is better:
Total Cost =
SUMX (
Sales,
Sales[Quantity] * RELATED ( Product[Unit Cost] )
)Use the following table to decide between the two styles.
| Situation | Better choice |
|---|---|
| The per-row value comes from columns of the iterated fact table and related tables | Column expressions with RELATED, no measure call |
| The per-row value is an existing measure you want to reuse, over a set of members such as customers, products, or dates | Call the measure and rely on context transition |
| The iterated table can contain fully duplicate rows | Avoid measure calls. Iterate over a key column with VALUES instead |
| The measure uses a time intelligence or other calculation that depends on filter context | Call the measure so that the transition applies the member filter |
Checking that the transition happened
You can test your understanding with a quick experiment in a table visual that shows Customer name and one measure per column.
- Add [Sales Amount] and
Avg Sales per Customer. At the customer level, the average equals the customer's own sales, because only one customer is in the filter context. - In the total row, the average becomes the mean across customers. The iterator now has many customers to visit and each one is evaluated through context transition.
- Replace the measure inside AVERAGEX with
SUM ( Sales[Quantity] )and compare. Every customer gets the same value, which shows that no transition happened.
Common mistakes
- Expecting SUM to see the current row. Aggregation functions over columns do not trigger context transition. Wrap them in CALCULATE or use a measure.
- Treating context transition as free. Each iteration step runs a separate evaluation. Iterating over a table with millions of rows and calling a measure on each row is slow.
- Calling a measure per row of a table with duplicates. The transition filters on all columns, so identical rows are matched together and counted more than once.
- Forgetting that blanks drop out of AVERAGEX. Average per member is not the same as total divided by member count.
- Iterating over more members than needed. Pick the table argument carefully.
VALUES ( Customer[CustomerKey] )is usually a better choice than the whole Customer table, because the row context is a single column.
Summary of the working model
An iterator evaluates its expression once for each row of its table argument. Column references read from the row context. Column aggregations ignore it. Measure references, and anything inside CALCULATE, turn it into filters. The iterator then combines the results, and AVERAGEX skips blanks when it does.
Exercise
A table visual shows each product category. The measure Avg per Product = AVERAGEX ( VALUES ( Product[ProductKey] ), SUM ( Sales[Quantity] ) ) returns the same number for every product within a category. Why, and how do you fix it?
Show answer
SUM is a column aggregation and does not trigger context transition, so inside the iterator it ignores the current ProductKey and returns the total quantity for the category on every step. The average of identical values equals that total. Fix it by wrapping the aggregation in CALCULATE, as in CALCULATE ( SUM ( Sales[Quantity] ) ), or by defining a measure such as [Total Quantity] = SUM ( Sales[Quantity] ) and calling it inside AVERAGEX. Either way, the row context for ProductKey becomes a filter on Product, which filters Sales.
Exercise
Avg Daily Sales iterates over VALUES ( 'Date'[Date] ) and calls [Sales Amount]. In a month where sales happened on 20 of 30 days, is the result the monthly total divided by 20 or by 30? How would you get the other number?
Show answer
It is divided by 20, because [Sales Amount] returns blank on the 10 days without sales and AVERAGEX ignores blank results. To divide by 30, change the expression to [Sales Amount] + 0 so that blanks become zero and every date in the filter context counts. Make sure the Date table contains only the dates you want to count.