Sub topic 9
Context transition: from row context to filter context
This chapter assumes you know basic DAX measures and calculated columns, and that you have a star-schema model with one-to-many relationships. It also assumes you have read the earlier chapters on filter context, row context and CALCULATE basics.
The problem: a row context does not filter
An iterator such as SUMX, AVERAGEX or FILTER walks through a table one row at a time. For each row it creates a row context, which lets you read that row's column values. A row context does not filter anything. It does not reach related tables, and it does not change what an aggregation like SUM sees.
Consider a calculated column on Customer:
Customer Sales Wrong = SUM ( Sales[Amount] )Every customer row gets the same number: the total of all sales. SUM reads the filter context, and the row context on Customer is not part of it.
The fix: CALCULATE converts row context into filters
When CALCULATE runs inside a row context, it takes the values of all columns in the current row and adds them to the filter context as filters. This is called context transition. The row context remains available, but the new filters now restrict the tables that CALCULATE evaluates.
Customer Sales = CALCULATE ( SUM ( Sales[Amount] ) )Now each customer row shows that customer's sales. The row's column values become filters on Customer, and those filters propagate through the relationship to Sales.
Measures contain an implicit CALCULATE
Every reference to a measure is wrapped in a CALCULATE automatically. This is why a measure works inside an iterator:
Average Sales per Customer =
AVERAGEX ( Customer, [Total Sales] )For each row of Customer, [Total Sales] is evaluated as CALCULATE ( [Total Sales] ). Context transition makes the expression see only that customer's sales. If you wrote AVERAGEX ( Customer, SUM ( Sales[Amount] ) ) instead, there would be no transition. Every iteration would return the grand total, and the average would equal the total sales in the current filter context.
Context transition is triggered in two ways:
- You call
CALCULATEorCALCULATETABLEinside a row context. - You reference a measure inside a row context.
What transition filters exactly
Context transition filters on every column of the row, not only the key. If Customer has a unique key, the filter identifies one row. If the table has no unique key and contains duplicate rows, the filter matches all duplicates, and their values are counted together.
This has two costs:
- Transition on a wide table with many columns is expensive, because the engine builds a filter on every column.
- On a large table, iterating row by row with a measure inside can be slow. Iterate over a small table, such as a dimension, rather than over
Sales.
Existing filters are replaced
The transition filters behave like the Boolean filters from the previous chapters. If the outer filter context already filters Customer[Country], the row's country replaces that filter inside the CALCULATE. The transition filter wins for every column of the iterated table, but filters on other tables stay in place. A slicer on Product[Color] still applies to the customer's sales.
Worked example
Suppose a report shows sales by Customer[Country], and you want the average of per-customer sales within each country.
Average Sales per Customer =
AVERAGEX ( Customer, [Total Sales] )In the row for France, the filter context contains Country = France. AVERAGEX iterates only the French customers. For each one, the implicit CALCULATE adds a filter for that customer's columns, then [Total Sales] runs. The iterator averages the results.
Summary of behavior
| Situation | Row context present | Filters the model |
|---|---|---|
| `SUM` inside an iterator | Yes | No |
| `CALCULATE ( SUM ( ... ) )` inside an iterator | Yes | Yes, on all columns of the iterated table |
| Measure reference inside an iterator | Yes | Yes, through its implicit `CALCULATE` |
Exercise
A measure is defined as AVERAGEX ( Customer, SUM ( Sales[Amount] ) ). It returns the same value for every customer and equals the total sales in the current filter context. Why, and how do you fix it?
Show answer
SUM does not trigger context transition, so the row context on Customer does not filter Sales. Each iteration sums all sales in the current filter context. Fix it by wrapping the expression in CALCULATE, as in AVERAGEX ( Customer, CALCULATE ( SUM ( Sales[Amount] ) ) ), or by referencing a measure such as [Total Sales], which has an implicit CALCULATE.
Exercise
Why can transition on a table with duplicate rows give surprising results?
Show answer
Transition filters on all columns of the row, not on a key. Two identical rows produce the same filter, so each of them sees the combined sales of both rows.