Row context
A row context supplies one current row
A row context lets an expression read columns from a particular current row. It is created in a calculated column and when an iterator evaluates an expression for each row of a table. It is not itself a filter on the entire fact table. Use the same clean retail Import model from the previous lesson. The rows have quantity, price and stored RevenueUSD. These examples use the model's original five rows; no additional table is required.
Example 1 — row-level values versus an aggregate
Create two calculated columns in a disposable FactSales copy, one at a time:
Context Line Amount = FactSales[Quantity] * FactSales[UnitPriceUSD]Context Whole Table = SUM(FactSales[RevenueUSD])The first formula uses the current row's fields. The second performs an aggregate without turning the current row into a filter. With the unfiltered Import model during calculation, its expected value repeats the full 200 USD on every row.
| Line | Quantity | Price USD | Context Line Amount | Context Whole Table |
|---|---|---|---|---|
| L01 | 2 | 12 | 24 | 200 |
| L02 | 1 | 30 | 30 | 200 |
| L03 | 3 | 12 | 36 | 200 |
| L04 | 1 | 50 | 50 | 200 |
| L05 | 2 | 30 | 60 | 200 |
Use a detail table and do not aggregate the diagnostic column; summing it would produce 1000 USD, which is not revenue. A North slicer can hide L03 and L05 but cannot recalculate the saved 200 values. Import columns are computed on model processing, whereas the Context Revenue measure responds at query time. Remove the redundant diagnostic columns from real models after the exercise.
Example 2 — iterators and nested row contexts
Create the following measure separately:
Context Iterator Revenue =SUMX(FactSales, FactSales[Quantity] * FactSales[UnitPriceUSD])SUMX takes a table and an expression. With North selected, the first argument sees three fact rows; the expression evaluates to 24, 30 and 50. The sum is 104. A row context exists for each expression evaluation, but the slicer controls the input table.
For a deliberately more verbose equivalent on the clean model, use:
Context Nested Revenue =SUMX( DimProduct, VAR CurrentKey = DimProduct[ProductKey] RETURN SUMX( FILTER(FactSales, FactSales[ProductKey] = CurrentKey), FactSales[Quantity] * FactSales[UnitPriceUSD] ))FILTER evaluates a Boolean test for each candidate fact row; the variable captures the outer product key before that inner row context begins. For North, the product contributions are 24, 30 and 50. Without the explicit key comparison, the same North rows would be visited for each of the three products, totaling 312 instead of 104. Outer row context alone does not apply a dimension filter to the fact. This teaching example assumes unique product keys and no orphans; do not adopt it as a faster general replacement for the first SUMX. FILTER returns a table, not a scalar total.
Common context mistakes
A naked FactSales[Quantity] reference in an ordinary measure has no guaranteed single current row. Use an aggregation or iterator appropriate to the question rather than hoping the visual supplies a row context. Do not confuse a table visual's displayed row with DAX row context. A variable stores the value evaluated in its defining context, which is why CurrentKey is stable inside the inner loop. A model measure invoked in a row context has special context-transition behavior; it is intentionally not used in this nested example and will be explained in Chapter 21. These Import examples do not establish calculated-column or RLS support in DirectQuery.
Practice — trace both contexts
Select South. List the surviving line amounts, the stored whole-table diagnostic values and the iterator result. Trace the nested measure by product, then predict the flawed result if the inner key condition is removed. State why the diagnostic column is not a usable revenue measure.