Sub topic 2
Row context and iterators in brief
This chapter assumes you can write basic measures with SUM and COUNTROWS. It also assumes you know that slicers and visuals filter the model, which the previous chapter covered as filter context. Here you learn the second kind of context, row context. You need it to understand how FILTER works later.
What a row context is
A row context means "the current row". It lets a formula read column values from one row of a table. Filter context is different. It is a set of filters that decides which rows of the model a measure can see.
Row context exists in two places:
- Calculated columns. DAX evaluates the formula once for each row of the table.
- Iterator functions. These functions walk through a table and evaluate an expression on each row.
A calculated column shows the idea:
Line Amount = Sales[Quantity] * Sales[Net Price]Each row of Sales has its own quantity and price. The formula multiplies them for that row only.
Iterators
An iterator takes a table and an expression. It evaluates the expression for each row of the table, then combines the results. The names of most iterators end in X.
Sales Amount Calc =
SUMX ( Sales, Sales[Quantity] * Sales[Net Price] )SUMX creates a row context on Sales, one row at a time. It multiplies quantity by price in each row and adds up the products. A plain SUM cannot do this, because SUM takes one column and has no row context.
Other common iterators:
| Function | What it does for each row | Result |
|---|---|---|
| `SUMX` | Evaluates an expression | Sum of the values |
| `AVERAGEX` | Evaluates an expression | Average of the values |
| `MAXX` | Evaluates an expression | Largest value |
| `COUNTX` | Evaluates an expression | Count of non-blank values |
| `FILTER` | Evaluates a true/false condition | The rows where it is true |
FILTER is on this list on purpose. It is an iterator that returns a table instead of a single number. The next chapters rely on that.
Row context does not filter other tables
A row context does not filter anything. It only lets you read the current row's columns. Suppose you iterate over Customer and write SUM ( Sales[Amount] ) inside the iterator. Each customer row still returns the same number. That number is the sales total under the current filter context, because SUM ignores the row context.
Context transition
A measure behaves differently inside a row context. DAX wraps a measure reference in an implicit CALCULATE. CALCULATE converts the current row into filters. This is called context transition.
Average Sales per Customer =
AVERAGEX ( Customer, [Total Sales] )For each customer row, [Total Sales] is evaluated with a filter on that customer. Each iteration therefore returns that customer's sales. AVERAGEX then averages those values. If you wrote SUM ( Sales[Amount] ) in place of [Total Sales], every row would return the overall total and the average would be meaningless.
Why this matters for FILTER
FILTER takes a table and a condition. It evaluates the condition in a row context on that table and keeps the rows where the condition is true. That is why a condition can combine several columns of the same row:
Large Line Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] * Sales[Net Price] > 1000 )
)The condition multiplies two columns, so a simple boolean filter on one column cannot express it. Row context makes it possible. The chapter on FILTER covers this pattern in detail.
Common mistakes
- Reading a column outside any row context. In a measure,
Sales[Net Price]on its own raises an error, because no current row exists. Wrap it in an aggregation or an iterator. - Expecting an iterator to filter related tables. Only context transition, through
CALCULATEor a measure reference, does that. - Iterating over a large table when a column aggregation is enough.
SUMX ( Sales, Sales[Amount] )returns the same result asSUM ( Sales[Amount] ), and iterating needlessly adds work.
Exercise
A measure is defined as AVERAGEX ( Customer, SUM ( Sales[Amount] ) ). It returns the same value for every customer row inside the iterator. Why, and how do you fix it?
Show answer
SUM does not use the row context, so context transition does not occur and each iteration sees the same filter context. Wrap the expression in CALCULATE, or reference the measure [Total Sales], which has an implicit CALCULATE. For example: AVERAGEX ( Customer, CALCULATE ( SUM ( Sales[Amount] ) ) ).