Sub topic 3
Row context: working on one row at a time
This chapter assumes you know what tables, columns, and one-to-many relationships are, and that you can write simple aggregations such as SUM and COUNTROWS. You should also know the difference between a measure and a calculated column.
What a row context is
A row context is an evaluation context that points at exactly one row of one table. While it is active, DAX can read the columns of that row by name, such as Sales[Quantity], and each reference returns the value stored in that row.
Filter context answers the question "which rows of the tables are visible?" Row context answers a different question: "which single row am I standing on right now?" The two are independent. A formula can run in one, the other, both, or neither.
A row context does not filter anything. It does not hide other rows, and it does not reach other tables through relationships. It only gives column references a meaning.
Where row contexts come from
Two things create a row context.
- Calculated columns. DAX evaluates the column expression once for every row of the table that holds the column.
- Iterator functions. Functions such as
SUMX,AVERAGEX,FILTER, andADDCOLUMNSopen a row context on each row of the table you give them.
A measure does not create a row context by itself. A measure that reads Sales[Quantity] directly, outside any iterator, fails with an error, because no single row is selected.
Calculated columns
The following calculated column is evaluated once per Sales row. Inside it, Sales[Quantity] and Sales[Unit Price] are the values of the current row.
-- Calculated column in the Sales table
Line Amount = Sales[Quantity] * Sales[Unit Price]The result is stored in the model when the data is refreshed. It is not recalculated when a user changes a slicer. That is a key difference from a measure.
Iterators
An iterator takes a table and an expression. It walks through the table, evaluates the expression in a row context for each row, and then combines the results.
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )SUMX creates a row context over Sales and adds up the per-row product. The surrounding filter context decides which Sales rows are iterated. If a slicer selects the year 2025, the table that SUMX receives contains only the 2025 rows, and the row context visits those rows one at a time.
This shows how the two contexts cooperate. Filter context chooses the rows. Row context walks through them.
Reading columns of the current row
Inside a row context, you can refer to a column of the iterated table with its full name. DAX returns the value from the current row.
A common beginner mistake is to expect a column reference to return a list of values. In a row context it returns one value. Outside a row context, in a measure, a bare column reference has no single value, so DAX raises an error. To work on a column in a measure, you aggregate it (SUM ( Sales[Quantity] )) or iterate over it.
| Place the expression runs | Row context? | What `Sales[Quantity]` means |
|---|---|---|
| Calculated column in `Sales` | Yes, one per row | The quantity of the current row |
| Argument of `SUMX ( Sales, ... )` | Yes, one per iterated row | The quantity of the current row |
| Measure body, outside an iterator | No | Not valid alone. Needs an aggregator |
| Inside `SUM ( Sales[Quantity] )` | No | The whole column, as filtered by the filter context |
Row context and relationships
A row context on one table can read columns from the "one" side of a relationship. This works because each row on the "many" side points at exactly one row on the "one" side. RELATED follows that relationship.
-- Calculated column in the Sales table, using the relationship
Product Category = RELATED ( Product[Category] )For each Sales row, RELATED finds the matching Product row and returns its Category.
The reverse direction is different. A row context on Product can reach many Sales rows. RELATEDTABLE ( Sales ) returns the table of all Sales rows that belong to the current product, and you pass that table to an aggregator such as COUNTROWS. The next chapters cover RELATED and RELATEDTABLE in more detail.
The important rule for this chapter is what a row context does not do. It does not propagate through relationships the way a filter does. A row on Product does not, by itself, restrict Sales.
A row context does not filter other tables
Consider a calculated column on Product that tries to compute sales per product.
-- Calculated column in the Product table
Sales Without Transition =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )You might expect each product to show its own sales. Instead, every product row shows the same number: the grand total of all sales.
The reason is that the calculated column creates a row context on Product. SUMX then iterates Sales. Nothing in this formula applies a filter to Sales, and a row context on Product does not filter Sales. The filter context is empty at this point, because calculated columns are evaluated when the model is refreshed, not under a visual. So SUMX sees every Sales row.
This is the gap that context transition closes. A later chapter explains it. For now, remember the observable symptom: a row context on one table and an aggregation over another table gives the grand total on every row.
-- Calculated column in the Product table
Product Sales = [Sales Amount]This second column does return each product's own sales. A reference to a measure behaves as if it were wrapped in CALCULATE, and CALCULATE converts the current row into a filter. We only observe the effect here, and we leave the mechanism for the chapters on CALCULATE and context transition.
Iterators over a filtered table
An iterator's first argument can be any table expression, not only a model table. This lets you control which rows get a row context.
FILTER is itself an iterator. It evaluates a condition in a row context for each row and keeps the rows where the condition is true.
Products Over 1000 =
COUNTROWS (
FILTER ( Product, [Sales Amount] > 1000 )
)FILTER opens a row context on each Product row. Referencing the measure [Sales Amount] inside it triggers context transition, so the measure is evaluated for the current product. COUNTROWS then counts the products that passed the test.
The same idea applies to AVERAGEX.
Average Sales per Customer =
AVERAGEX ( Customer, [Sales Amount] )AVERAGEX opens a row context on each Customer row. The measure reference turns each row into a filter, so each customer's sales are computed separately. AVERAGEX then averages those values.
Nested row contexts
Row contexts can be nested. If you write an iterator inside another iterator, or call FILTER inside a calculated column, there are two row contexts on the same table at once: an outer one and an inner one.
Column references always resolve to the innermost row context that covers that table. So in the expression below, the outer row is hidden once the inner iteration starts.
Suppose you want to rank each product by its unit price within a calculated column on Product. The naive attempt compares the column with itself.
-- Calculated column in the Product table
-- Wrong: both sides refer to the inner row, so the condition is never true
Wrong Price Rank =
COUNTROWS ( FILTER ( Product, Product[Unit Price] > Product[Unit Price] ) ) + 1Both Product[Unit Price] references resolve to the inner row, which is the row FILTER is currently testing. A value is never greater than itself, so the count is 0 and every product gets rank 1.
Older DAX code solved this with EARLIER, which reads a column from the previous (outer) row context. 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 ) ) + 1VAR is evaluated in the outer row context, so CurrentPrice holds the price of the product being ranked. Inside FILTER, Product[Unit Price] is the inner row, and CurrentPrice is the fixed outer value. The count is the number of products with a higher price, plus one.
Common errors
- Using a bare column in a measure. A measure has no row context by default. Wrap the column in an aggregator or use an iterator.
- Expecting a row context to filter another table. It does not. Aggregating a different table inside a row context returns the grand total, unless context transition applies.
- Using
RELATEDfrom the wrong side.RELATEDworks from the many side towards the one side. From the one side, useRELATEDTABLEand an aggregator. - Comparing a column with itself in nested iterations. Capture the outer value in a variable first.
- Assuming a calculated column reacts to slicers. It is computed at refresh time, so it does not.
Summary
- A row context points at one row of one table and lets column references return that row's value.
- Calculated columns and iterators create row contexts. Measures do not on their own.
- A row context does not filter tables and does not follow relationships.
RELATEDandRELATEDTABLEare how you read across a relationship. - An iterator over a table in a filter context loops only over the visible rows.
- With nested row contexts, column references resolve to the innermost one. Use variables to keep outer values.
Exercise
A calculated column in the Product table is defined as Total = SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ). Every product row shows the same value. Why does this happen, and which small change makes each product show its own sales?
Show answer
The calculated column creates a row context on Product, but a row context does not filter Sales, and no filter context exists at refresh time. SUMX therefore iterates all Sales rows and returns the grand total for every product. Replacing the expression with a reference to the measure, Total = [Sales Amount], makes the reference act like CALCULATE, which turns the current Product row into a filter that reaches Sales, so each product shows its own sales.
Exercise
In a calculated column on Product, you write COUNTROWS ( FILTER ( Product, Product[Unit Price] > Product[Unit Price] ) ) + 1 to rank products by price, and every product gets rank 1. Rewrite it so it works, and explain the fix.
Show answer
Store the outer price in a variable before FILTER runs: VAR CurrentPrice = Product[Unit Price] RETURN COUNTROWS ( FILTER ( Product, Product[Unit Price] > CurrentPrice ) ) + 1. Inside FILTER both column references point at the inner row, so the original compares a value with itself and never counts any row. The variable holds the outer row's value, so the inner comparison tests each product against the product being ranked.