Sub topic 12
Nested Row Contexts, EARLIER, and Variables
This chapter assumes you know the difference between a measure and a calculated column. It also assumes you know what a row context is and how an iterator such as FILTER creates one. You should also know that referencing a measure inside a row context triggers context transition.
What a nested row context is
A row context points at one row of one table. Sometimes a formula opens a second row context while the first is still active. The result is a nested row context.
A common case is a calculated column that uses an iterator over the same table. The column itself runs once per row, which gives the outer row context. The iterator then opens an inner row context over every row of the table.
Suppose you want to rank each product by price. For each product, you count how many products cost more, then add 1.
The problem: which row does the column refer to?
Inside FILTER, the expression Product[Unit Price] reads the inner row. You need a way to also read the outer row's price. A first attempt might look like this:
-- Calculated column in the Product table (does not do what you want)
Price Rank Wrong =
COUNTROWS ( FILTER ( Product, Product[Unit Price] > Product[Unit Price] ) ) + 1Both references resolve to the inner row. The condition compares each price with itself, so it is never true. The column returns 1 for every product.
The old answer: EARLIER
EARLIER reads a column from the previous (outer) row context. It exists for exactly this case.
-- Calculated column in the Product table
Price Rank Earlier =
COUNTROWS ( FILTER ( Product, Product[Unit Price] > EARLIER ( Product[Unit Price] ) ) ) + 1This works, but it is hard to read. A reader has to count row contexts to know what EARLIER points at. With three levels of nesting, the question "how far back does it go?" becomes real.
The better answer: variables
A variable stores a value at the point where you define it. If you define it before FILTER opens the inner row context, it keeps the outer row's value.
-- Calculated column in the Product table
Price Rank =
VAR CurrentPrice = Product[Unit Price]
RETURN
COUNTROWS ( FILTER ( Product, Product[Unit Price] > CurrentPrice ) ) + 1CurrentPrice is evaluated once, in the outer row context. Inside FILTER, Product[Unit Price] is the inner row and CurrentPrice is the outer value. The two never get mixed up, and the name tells the reader what the value means.
Variables and context
A variable is evaluated in the context where it is declared, not where it is used. This is the property that makes the rewrite work. It also matters for filter context. If you store a measure result in a variable before CALCULATE changes the filters, the variable keeps the old result.
Sales Share of Total =
VAR CurrentSales = [Sales Amount]
VAR TotalSales = CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product ) )
RETURN
DIVIDE ( CurrentSales, TotalSales )CurrentSales is computed under the visual's filters. TotalSales is computed after REMOVEFILTERS clears the product filter. Neither value changes after it is computed.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Referencing the same column twice inside `FILTER` and expecting the outer row | Both references read the inner row, so the comparison is always false | Capture the outer value in a variable first |
| Declaring the variable inside the iterator | The variable is evaluated per inner row, so it holds the inner value | Declare it before the iterator |
| Expecting a variable to react to a later `CALCULATE` | The variable keeps the value from where it was declared | Compute a second variable inside the `CALCULATE` |
| Using `EARLIER` across several nesting levels | The target row context is unclear and easy to get wrong | Use one named variable per value you need |
Exercise
A calculated column in the Product table is written as COUNTROWS ( FILTER ( Product, Product[Unit Price] < Product[Unit Price] ) ). Why does it always return 0, and how do you rewrite it so that it counts the products cheaper than the current one?
Show answer
Both Product[Unit Price] references read the inner row context that FILTER creates, so each row is compared with itself and the condition is never true. Capture the outer price first: VAR CurrentPrice = Product[Unit Price] RETURN COUNTROWS ( FILTER ( Product, Product[Unit Price] < CurrentPrice ) ).