Sub topic 10
Context transition: from row context to filter context
This chapter assumes you know the difference between a measure and a calculated column. It also assumes you can read basic DAX such as SUM and COUNTROWS, and that you understand one-to-many relationships. Earlier chapters covered filter context, row context, iterators, and CALCULATE. Here they come together.
The problem context transition solves
A row context points at one row of one table. It lets you read that row's columns, but it does not filter anything. A filter context restricts which rows the engine reads when it aggregates. These are separate mechanisms, and a row context alone never changes what an aggregation sees.
Consider this calculated column in the Product table:
-- Calculated column in the Product table
Sales Without Transition =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )The column is evaluated once for each Product row, so a row context exists on Product. But SUMX iterates the whole Sales table, because nothing filters Sales. Every product row shows the same grand total.
Context transition fixes this. It converts the current row of a row context into an equivalent filter context. After the conversion, the calculation behaves as if you had filtered the table to that one row.
What context transition does
When CALCULATE runs inside a row context, it performs these steps:
- It reads the values of all columns of the table in the current row context.
- It turns each column and its value into a filter, such as Product[ProductKey] = 17 and Product[Color] = "Red".
- It adds those filters to the filter context. A filter on a column replaces any existing filter on that same column.
- It evaluates its first argument in the new filter context.
Because the filters are on Product, they reach Sales through the relationship. Sales then contains only the rows for that product.
Three ways to trigger it
Context transition happens in three situations. They all come down to CALCULATE running while a row context is active.
Explicit CALCULATE
Wrapping an expression in CALCULATE inside a row context triggers the transition:
-- Calculated column in the Product table
Product Sales Explicit =
CALCULATE ( SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ) )CALCULATE has no filter arguments here. Its only effect is the transition, and each product now shows its own sales.
Measure references
Every reference to a measure is silently wrapped in CALCULATE. This is the implicit CALCULATE covered in the previous chapter. With the existing measure, the column becomes trivial:
-- Calculated column in the Product table
Product Sales = [Sales Amount]The measure reference is wrapped in an implicit CALCULATE, so the current Product row becomes a filter that reaches Sales through the relationship, and each product shows its own sales. Compare this with the earlier SUMX version that did not transition. The measure Sales Amount is defined as follows:
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )The body is the same as the failing column. The difference is that the measure reference adds the transition, while the inline SUMX does not.
Column references are different. Sales[Quantity] is not wrapped in CALCULATE. Only measures have this behavior.
Iterators that call measures
An iterator opens a row context on each row of its table. If the expression it evaluates is a measure, each iteration transitions:
Average Sales per Customer =
AVERAGEX ( Customer, [Sales Amount] )For each Customer row, AVERAGEX opens a row context. The reference to [Sales Amount] converts that row to a filter on Customer. The filter propagates to Sales, so the measure returns that customer's sales. AVERAGEX then averages those values.
The same mechanism drives FILTER:
Products Over 1000 =
COUNTROWS (
FILTER ( Product, [Sales Amount] > 1000 )
)FILTER iterates Product, and the measure is evaluated for each product. The result is the number of products whose sales exceed 1000.
Comparing row context and transition
| Situation | Row context present | Filter on the table | Result of a measure-like aggregation |
|---|---|---|---|
| Calculated column with inline SUMX | Yes | No | Grand total on every row |
| Calculated column with CALCULATE or measure reference | Yes | Yes, from the row | Value for that row |
| Measure in a visual cell | No | From the visual | Value for the cell |
| Iterator calling a measure | Yes, per iteration | Yes, per iteration | Value per iterated row |
What exactly becomes a filter
Context transition filters on every column of the table in the row context, not only the key. If the row has values in all columns, the filter is the combination of those values, and the engine keeps only the rows that match the entire combination.
Two consequences follow.
- If the table has a unique key column, the combination identifies exactly one row, and the transition behaves as you expect.
- If two rows have identical values in every column, both rows pass the filter. Each of those rows then sees the sales of both.
Transition also interacts with existing filters. The new filters replace existing filters on the same columns. Filters on other tables, and on other columns of the same table, remain. A slicer on Product[Color] does not disappear in a calculated column, because calculated columns are computed without a visual filter, but inside a measure iterating over Product it can interact with the transition.
Using transition with other filter arguments
You can combine transition with filter arguments in the same CALCULATE. The row filters from the transition are applied together with the explicit filters. The result is the intersection unless an explicit argument targets the same column.
Red Sales =
CALCULATE ( [Sales Amount], Product[Color] = "Red" )This version adds a filter on Product[Color] and replaces any existing filter on that column. To intersect with the existing selection instead, wrap the condition in KEEPFILTERS:
Red Sales Within Selection =
CALCULATE (
[Sales Amount],
KEEPFILTERS ( Product[Color] = "Red" )
)Pitfalls
Duplicate rows
Transition filters on all columns. A table without a key can contain fully duplicated rows. Each duplicate returns the same number, which counts the sales of all duplicates together. Summing that column over the table then counts those sales more than once.
Fix the data model so every table that you iterate with transition has a unique key. If you cannot, remove the duplicates in the source or in Power Query.
Missing relationships and keys
Transition produces filters on the iterated table. If that table has no relationship to the table you aggregate, the filter has nowhere to travel. The measure returns the same value for every row, just as in the non-transition case, and the formula gives no error. Check the model diagram when every row shows the same value.
Slow iterations
Each transition evaluates the measure once per iterated row. Iterating a table with millions of rows and calling a measure inside the iterator is expensive. Prefer a small table, such as a dimension, for the iteration. Avoid putting a measure inside an iterator over a large fact table when a column expression such as Sales[Quantity] * Sales[Unit Price] gives the same result.
-- Fast: no transition, iterates the fact table with column references
Sales Amount Fast =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )
-- Slow on large tables: one transition per Sales row
Sales Amount Slow =
SUMX ( Sales, [Sales Amount] )The second formula is also wrong in a subtler way. The measure is evaluated under a filter built from the current Sales row, so each iteration returns the amount of every sales row that matches that row's columns. With duplicate sales rows the total is inflated.
Nested row contexts
A row context can contain another one. When FILTER iterates the same table inside a calculated column, the inner row context hides the outer row's columns. The old solution was the EARLIER function. A variable is clearer. It captures the outer value before the inner iteration begins:
-- Calculated column in the Product table
Price Rank =
VAR CurrentPrice = Product[Unit Price]
RETURN
COUNTROWS ( FILTER ( Product, Product[Unit Price] > CurrentPrice ) ) + 1The variable is computed in the outer row context. Inside FILTER, Product[Unit Price] refers to the inner row, and CurrentPrice still holds the outer value. The count of products with a higher price plus one gives the rank. Products that tie on price share the same rank.
A checklist for predicting a result
- Find out whether the expression runs in a row context. Calculated columns and iterator arguments do.
- Look for CALCULATE or a measure reference inside that row context. If there is one, a transition happens.
- List the columns of the row's table. They all become filters.
- Follow the relationships from that table to the table being aggregated.
- Check for duplicate rows and for existing filters on the same columns.
Exercise
A calculated column on the Customer table is defined as Total Sales = SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ). Every customer shows the same number. Give two different ways to fix it, and name the condition the Customer table must meet for the fix to be reliable.
Show answer
Wrap the expression in CALCULATE, as in CALCULATE ( SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ) ), or replace it with a reference to the measure, = [Sales Amount]. Both trigger context transition, so the current Customer row filters Sales through the relationship. The Customer table must have no fully duplicated rows, ideally a unique key column, and a relationship to Sales.
Exercise
What does Products Over 1000 count, and why does it produce a different answer from COUNTROWS ( FILTER ( Product, SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ) > 1000 ) )?
Show answer
The first formula counts products whose own sales exceed 1000, because [Sales Amount] is wrapped in an implicit CALCULATE and transitions each Product row into a filter on Sales. The second formula has no transition, so SUMX returns the same total for every product. Either all products are counted or none, depending on whether the total exceeds 1000.