Sub topic 1
Row context: evaluating an expression one row at a time
Prerequisites
This chapter assumes you can:
- Write simple measures with SUM, COUNT, and CALCULATE.
- Describe tables, columns, and relationships in a Power BI or tabular model.
- Recognize a star schema, with a Sales fact table and dimension tables such as Product and Customer.
What a row context is
A row context is a pointer to one row of a table. While it exists, a column reference such as Sales[Quantity] returns the value from that row. Without it, DAX has no single value to return, because a column holds many values.
A row context does not filter anything. It does not shrink the table, change a report total, or affect other tables. It only says "the current row is this one."
Where row contexts come from
DAX creates a row context in two common places:
- Calculated columns. DAX evaluates the formula once for every row of the table that holds the column.
- Iterator functions such as SUMX, AVERAGEX, and RANKX. They create a row context for each row of the table you pass as their first argument.
This calculated column works because it runs inside a row context:
Line Amount = Sales[Quantity] * Sales[Net Price]Each row of Sales gets its own result. The multiplication is valid because Sales[Quantity] and Sales[Net Price] each resolve to one value.
The same formula in a measure
A measure has no row context of its own. It only has a filter context, which decides which rows are visible. This measure therefore fails:
Broken Amount = Sales[Quantity] * Sales[Net Price]DAX reports an error, because each column reference would return a whole column of values. Nothing tells DAX which row to use.
To fix it, create a row context yourself with an iterator:
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)SUMX walks through the visible rows of Sales. For each row it evaluates the second argument, then adds up the results.
Row context and other tables
A row context on Sales covers only Sales columns. The following formula fails, because Product[Unit Cost] has no current row:
Bad Cost =
SUMX (
Sales,
Sales[Quantity] * Product[Unit Cost]
)Later chapters show how RELATED follows a relationship from the current Sales row to its Product row. For now, remember that a row context is tied to one table.
Nested row contexts
Row contexts can be nested. If one iterator sits inside another, the inner expression sees the current row of both tables. Both are active at once, and columns from either table can be referenced. If both tables have a column with the same name, qualify it with the table name, as in Sales[Quantity].
Common errors
| Symptom | Likely cause |
|---|---|
| Error that a column "cannot be determined" in a measure | A bare column reference was used with no row context. |
| Measure works but is slow on large tables | An iterator visits every row. This is a cost, not an error. |
| A column from a related table fails inside an iterator | The row context belongs to a different table. |
Exercise
A colleague writes the measure Margin = Sales[Net Price] - Sales[Unit Cost], where both columns are in the Sales table, and it returns an error. Explain why, then rewrite it so that it returns total margin for the rows in the current report.
Show answer
A measure has no row context, so each column reference would return a whole column rather than one value. Use an iterator to create the row context: Margin = SUMX ( Sales, Sales[Net Price] - Sales[Unit Cost] ). SUMX evaluates the subtraction once per visible Sales row and adds up the results.