Sub topic 12
Context transition: bridging row context and filter context
This chapter assumes you know the basics of row context and filter context, can read simple DAX, and understand one-to-many relationships in a star schema. Row context means "the current row". Filter context means "the set of rows left visible by slicers, filters and chart axes". Context transition is the step that converts the first into the second.
The problem it solves
A row context knows which row you are on, but aggregation functions such as SUM ignore it. They look only at the filter context. So a measure written inside a row context does not behave the way many people expect.
Consider a calculated column on the Product table:
Product Sales (wrong) = SUM(Sales[Line Amount])Every product row shows the same number: the grand total of all sales. The row context on Product exists, but SUM does not use it. The filter context is empty, so every row sees every sale.
What CALCULATE does with a row context
When CALCULATE runs inside a row context, it takes the values of the current row's columns and turns them into filters. This conversion is context transition.
Product Sales = CALCULATE(SUM(Sales[Line Amount]))In the Product table, this column now shows each product's own sales. For one row, CALCULATE adds filters such as Product[ProductKey] = 17 (and the other columns of that row). Relationships then carry the filter to Sales, so SUM adds up only that product's rows.
The same happens whenever you reference a measure inside a row context. A measure reference is silently wrapped in CALCULATE:
Product Sales (via measure) = [Total Sales]As a calculated column on Product, this gives the same result as the previous example.
Where you meet it in practice
Iterator functions such as SUMX create a row context. If the expression inside calls a measure, context transition happens on every row:
Sales per Product Average =
AVERAGEX(Product, [Total Sales])For each product, the row context becomes a filter, [Total Sales] is computed for that product, and AVERAGEX averages those results. Without the transition, every product would return the grand total.
Common errors
- Expecting
SUMto respect the row. Plain aggregators never trigger context transition. Wrap them inCALCULATEor use a measure. - Duplicate rows. The transition filters on all columns of the current row. If a table has fully identical rows, each of them is matched together and the result is counted more than once.
- Cost on large tables. The transition runs once per row. A transitioning calculated column on a large table can slow refresh noticeably, and a measure inside an iterator over a large table can slow queries.
- Using it in a column by habit. A calculated column that uses transition stores a value that ignores report filters. A measure reflects the current filters.
Exercise
A calculated column on the Product table is defined as = SUM(Sales[Line Amount]). Every row shows the same value. Why, and how do you fix it so each row shows that product's sales?
Show answer
The row context on Product is ignored by SUM, which only reads the filter context, and that is empty for the column. Every row therefore sees all Sales rows. Wrap the aggregation in CALCULATE, as in CALCULATE(SUM(Sales[Line Amount])), or reference an existing measure such as [Total Sales]. Context transition then converts the current Product row into a filter that flows to Sales.