Sub topic 1
How DAX decides what a formula sees
A DAX formula does not return the same result everywhere. The measure SUM ( Sales[Quantity] ) gives a different number in each cell of a matrix, and on a card with no filters it gives the grand total. The formula text stays the same. What changes is the evaluation context, the set of conditions DAX applies before it calculates. This chapter introduces the two kinds of context. Later chapters cover each one in detail.
Prerequisites
This chapter assumes you already know:
- What tables, columns, and one-to-many relationships are in a data model.
- Simple aggregations such as
SUMandCOUNTROWS. - The difference between a measure and a calculated column.
- How to build a basic report visual in Power BI or a similar tool.
The examples use a model with a Sales table on the many side. It has Quantity and Unit Price columns. Sales relates to Product and Customer, which sit on the one side.
The two contexts
Every DAX expression is evaluated in a context. There are two kinds, and they answer different questions.
Filter context
The filter context is a set of filters on columns. It decides which rows of each table are visible to the calculation. It comes from slicers, from the rows, columns, and filters of a visual, and from the filter arguments of CALCULATE.
Take this measure:
Total Quantity = SUM ( Sales[Quantity] )In a matrix with Product[Color] on the rows, the Red row evaluates it with a filter on color equal to Red. That filter reaches Sales through the relationship, so only sales of red products are summed. The formula does not mention color at all.
Row context
The row context is a pointer to one row of one table. It lets you read that row's column values directly. It does not filter anything. It comes from two places:
- Calculated columns, which DAX evaluates once for each row of the table.
- Iterator functions such as
SUMX, which step through a table one row at a time.
Here is a calculated column:
-- Calculated column in the Sales table
Line Amount = Sales[Quantity] * Sales[Unit Price]For each Sales row, DAX points at that row. Sales[Quantity] therefore means "the quantity in this row" and returns one value. Outside a row context, the same reference is only a column, and DAX cannot reduce it to a single value.
Where each context comes from
| Source | Filter context | Row context |
|---|---|---|
| Slicer or visual axis | Yes | No |
| `CALCULATE` filter argument | Yes | No |
| Calculated column | No | Yes, one row at a time |
| Iterator such as `SUMX` | Not created, but inherited | Yes, over the iterated table |
The last row shows that the two contexts can be active together. They are independent mechanisms that happen to combine.
Both contexts in one formula
Look again at the iterator version of a sales total:
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )Two things happen when a matrix cell evaluates this measure:
- The filter context from the visual decides which
Salesrows are in play. For the Red row, those are the rows for red products. SUMXopens a row context over those rows. It evaluatesQuantity * Unit Priceonce per row and adds up the results.
The filter context chooses the rows, and the row context does the per-row arithmetic. Most DAX errors come from mixing up these two roles.
A quick way to reason about any formula
Ask these questions in order:
- Is the formula a measure, a calculated column, or an expression inside an iterator? This tells you whether a row context exists.
- What filters does the surrounding report apply? This is the filter context.
- Does a function such as
CALCULATEchange those filters before the expression runs?
If you can answer all three, you can usually predict the result without running the formula.
Exercise
A calculated column in the Sales table is defined as Line Amount = Sales[Quantity] * Sales[Unit Price]. A measure is defined as Qty = SUM ( Sales[Quantity] ). Which of the two formulas is evaluated in a row context, and where does the filter context for the measure come from when it is placed in a matrix grouped by Product[Color]?
Show answer
The calculated column is evaluated in a row context, once for each Sales row, so it reads that row's Quantity and Unit Price. The measure has no row context. Its filter context comes from the matrix. Each color row filters Product to that color, and the filter propagates to Sales through the relationship.