Sub topic 1
Row context: evaluating an expression one row at a time
A DAX expression can only use a column by name when DAX knows which row it is looking at. Row context is the mechanism that supplies that row. It underlies every iterator in this tutorial, including SUMX, AVERAGEX, and RANKX. This chapter covers what a row context is, where one comes from, what it can and cannot do, and the errors that appear when it is missing.
Prerequisites
This chapter assumes you can do the following:
- You know the basics of a Power BI or Analysis Services tabular model: tables, columns, and relationships.
- You can write simple measures with SUM, COUNT, and CALCULATE.
- You understand star-schema modelling, with fact tables (such as Sales) on the many side and dimension tables (such as Product) on the one side.
The examples use a Sales fact table with Quantity and Net Price columns. It relates many-to-one to a Product table that has Unit Cost and Product Name columns.
What a row context is
A row context is a pointer to a single row of a table. While it is active, a column reference such as Sales[Quantity] returns the value of that column in the current row. The expression is evaluated for that row alone, and it does not look at the other rows.
A row context has no other effect:
- It does not filter the model.
- It does not affect other tables.
- It does not change the result of aggregations such as SUM.
It only answers the question "which row's value should Sales[Quantity] return?"
Where row contexts come from
Row contexts are created in a small number of places.
| Source | When the row context exists | Rows visited |
|---|---|---|
| Calculated column | While the column is computed | Every row of the table that owns the column |
| Iterator function (SUMX, AVERAGEX, RANKX, and others) | While the iterator evaluates its expression argument | Every row of the table argument |
| FILTER | While it tests its condition | Every row of its table argument |
| ADDCOLUMNS | While it computes each new column | Every row of its table argument |
An ordinary measure does not create a row context. A measure is evaluated against a filter context only. This difference explains most of the errors in this chapter.
Row context in a calculated column
A calculated column is the simplest case. DAX computes it once for each row when the model refreshes.
Line Amount = Sales[Quantity] * Sales[Net Price]For each row of Sales, DAX sets the row context to that row. It reads Quantity and Net Price from it and stores the product. Nothing here is special syntax. The row context was created automatically because this is a column definition.
Why a measure cannot use bare columns
Suppose you write the same expression as a measure:
Line Amount Measure = Sales[Quantity] * Sales[Net Price]DAX reports an error. In Power BI Desktop the message says that a single value for column Quantity cannot be determined. The cause is that a measure has no row context. In a visual, the measure sees many Sales rows at once, so Sales[Quantity] is a whole column of values and not one number.
Aggregators such as SUM collapse a column into one number. But SUM(Sales[Quantity]) * SUM(Sales[Net Price]) is a different calculation. It multiplies two totals and does not total the products. To get a per-row product you must create a row context, and an iterator does that.
Creating a row context with an iterator
An iterator takes a table and an expression. It visits each row of the table, evaluates the expression with that row as the current row context, and then combines the results.
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)Here is how this behaves for three sales rows:
| Row | Quantity | Net Price | Expression result |
|---|---|---|---|
| 1 | 2 | 10 | 20 |
| 2 | 1 | 25 | 25 |
| 3 | 3 | 5 | 15 |
SUMX adds the results and returns 60. A plain SUM(Sales[Quantity]) would return 6, and no single-column aggregation can produce 60. The next chapters develop the iterator pattern further. For now, note that the second argument of SUMX is the part that runs inside the row context.
Row context does not filter
A row context does not filter other tables or other rows. It is easy to assume that if you are standing on one product row, a SUM over Sales will include only that product's sales. It will not.
Consider this calculated column on the Product table:
Product Quantity = SUM ( Sales[Quantity] )The row context points at one product, but SUM ignores it. A calculated column has no filter context either. SUM therefore sees the entire Sales table, and every product row shows the same grand total.
Making the row context filter related tables requires context transition, which CALCULATE performs. A later chapter covers it in detail. At this point, remember the rule that row context does not propagate through relationships on its own.
Row context and relationships
A row context on one table does not give access to columns of other tables. Which function reaches across the relationship depends on the direction.
- From the many side to the one side, use RELATED. It follows the relationship and returns the value from the single matching row.
- From the one side to the many side, use RELATEDTABLE. It returns the set of matching rows as a table.
On the many side, a calculated column in Sales can read the product cost:
Line Cost = Sales[Quantity] * RELATED ( Product[Unit Cost] )RELATED works the same inside an iterator, because an iterator also provides a row context on Sales:
Total Cost =
SUMX (
Sales,
Sales[Quantity] * RELATED ( Product[Unit Cost] )
)Without RELATED, writing Product[Unit Cost] inside the iterator over Sales fails. The row context points at a Sales row, so DAX cannot tell which Product row you mean.
You can also count the sales for each product from the one side:
Sales Rows = COUNTROWS ( RELATEDTABLE ( Sales ) )Used as a calculated column on Product, this returns the number of Sales rows for the current product.
Nested row contexts
Iterators can be nested. Each level adds its own row context, and the contexts stack. Inside the inner expression, a bare column reference to a table refers to that table's innermost active row context. This can hide the outer row.
The safest way to compare an inner row with a value from an outer or earlier calculation is a variable. A variable stores a value at the point where it is defined and keeps it unchanged.
Lines Above Average =
VAR AverageLine =
AVERAGEX ( Sales, Sales[Quantity] * Sales[Net Price] )
RETURN
COUNTROWS (
FILTER (
Sales,
Sales[Quantity] * Sales[Net Price] > AverageLine
)
)AVERAGEX opens a row context over Sales to compute the average line amount. That row context closes when AVERAGEX finishes. FILTER then opens a new one to test each row. AverageLine is a single number, so it can be used inside the FILTER condition without ambiguity.
The older EARLIER function reaches an outer row context of the same table. You will still meet it in existing models, but variables are clearer and are the recommended style.
Common errors and how to read them
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about a single value for a column that cannot be determined | A bare column reference in a measure, with no row context | Wrap the calculation in an iterator, or aggregate the column |
| Same value on every row of a calculated column | An aggregation such as SUM, which ignores the row context | Use CALCULATE (covered later), or a row-level expression |
| Error saying a column cannot be found or a value cannot be determined for a dimension column inside an iterator over the fact table | The dimension column is referenced without RELATED | Wrap it in RELATED |
| Totals do not match the sum of the rows | Multiplying aggregated totals where per-row products were intended | Use SUMX with the per-row expression |
Checking your understanding of scope
When you write a DAX expression, ask these questions in order:
- Is a row context active here? A calculated column or the expression argument of an iterator provides one. A measure body alone does not.
- Which table does that row context belong to?
- Do I need a column from a different table? If so, check which side of the relationship I am on, and use RELATED or RELATEDTABLE.
- Do I expect this row to filter anything? If so, a row context alone will not do it.
Exercise
A measure is defined as Margin = Sales[Net Price] - RELATED ( Product[Unit Cost] ), and DAX returns an error. Why does it fail, and how would you rewrite it to return total margin per unit sold, summed over all sales rows?
Show answer
The measure has no row context, so Sales[Net Price] is a whole column and RELATED has no current row to follow. Create the row context with an iterator: Total Margin = SUMX ( Sales, Sales[Quantity] * ( Sales[Net Price] - RELATED ( Product[Unit Cost] ) ) ). The row context on Sales lets both the column reference and RELATED work for each row.
Exercise
You add the calculated column Product Quantity = SUM ( Sales[Quantity] ) to the Product table, and every product shows the same value. Why? Name the property of row context that explains it.
Show answer
A row context does not filter. SUM ignores which product row is current, and a calculated column has no filter context, so SUM sees the entire Sales table and returns the grand total on every row. Making the column show per-product quantities needs context transition, for example wrapping the SUM in CALCULATE.