Sub topic 1
How DAX Decides What a Formula Sees
Every DAX formula runs against a data model, but it never sees the whole model. It sees a slice, and the slice depends on where the formula runs. The same measure can return 85 in one cell of a report and 15 in the cell next to it. This chapter introduces the two mechanisms that define the slice, which together are called the evaluation context. Later chapters cover each mechanism in detail. Here you learn what they are, where each one comes from, and how to tell which one is active.
Prerequisites
This chapter assumes you already know the following:
- Tables, columns, and one-to-many relationships 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.
You do not need to know CALCULATE or iterators yet. Both appear briefly here and get full chapters later.
The question every formula answers
A DAX expression such as SUM ( Sales[Amount] ) does not say which rows to add. It says "add the Amount column". The engine decides which rows of Sales take part, and it decides this separately each time the expression is evaluated.
The rules that make this decision are the evaluation context. DAX has two kinds:
- Filter context is a set of filters on columns. It decides which rows of each table are visible to the calculation.
- Row context is a pointer to one row of one table. It decides which row a column reference such as
Sales[Quantity]reads from.
These two are independent, and a formula can have either, both, or neither. Most DAX errors at the intermediate level come from assuming one context does the other's job.
A small model for the examples
The examples in this chapter use two tables. Product is on the one side of a one-to-many relationship, and Sales is on the many side. The relationship runs on ProductKey.
| ProductKey | Category | Color |
|---|---|---|
| P1 | Bikes | Red |
| P2 | Helmets | Blue |
| P3 | Bikes | Red |
The Sales table has these rows:
| SaleID | ProductKey | Quantity | Unit Price |
|---|---|---|---|
| 1 | P1 | 2 | 10 |
| 2 | P1 | 1 | 10 |
| 3 | P2 | 3 | 5 |
| 4 | P3 | 1 | 40 |
Multiplying quantity by price for each row gives 20, 10, 15, and 40. The grand total is 85. Red products account for 70, and the blue product accounts for 15.
Filter context: which rows are visible
Consider this measure:
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )Place it in a matrix with Product[Color] on the rows. Three cells are produced, and the measure text is identical in each:
| Color | Sales Amount |
|---|---|
| Blue | 15 |
| Red | 70 |
| Total | 85 |
For the Blue row, the visual applies the filter Product[Color] = "Blue". That filter reaches Sales through the relationship, so only sale 3 is visible. The Red row sees sales 1, 2, and 4. The Total row has no filter on Color, so all four rows are visible.
Filter context comes from several sources, and they all combine:
- Row and column headers of a visual.
- Slicers and report, page, and visual-level filters.
- Filter arguments of
CALCULATE, which you will meet in a later chapter. - Relationships, which pass a filter from one table to the tables related to it.
Filter context is a set of column filters, not a single row. Filtering Color = "Red" leaves two products and three sales rows visible. A filter on Category and a filter on Color can be active together, and a row must satisfy both to remain visible.
Row context: which row is current
Row context answers a different question: "which row am I standing on?" It exists whenever DAX walks a table one row at a time. The simplest case is a calculated column, which DAX computes once for every row of its table.
-- Calculated column in the Sales table
Line Amount = Sales[Quantity] * Sales[Unit Price]
-- Calculated column in the Sales table, using the relationship
Product Category = RELATED ( Product[Category] )In the first column, Sales[Quantity] is not a whole column. It is the Quantity value of the current row. For sale 1 the result is 20, and for sale 3 it is 15. The second column uses RELATED, which follows the relationship from the current Sales row to its single matching Product row and reads the category.
Row context has three sources:
- Calculated columns, which evaluate once per row of their table.
- Iterator functions such as
SUMX,AVERAGEX, andFILTER, which open a row context on the table they iterate. - Some other table-walking constructs, which this tutorial covers where needed.
Inside SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ), the iterator steps through the visible rows of Sales. For each row it evaluates the second argument with that row current, then sums the results.
Two contexts, two jobs
The table below summarizes the differences.
| Property | Filter context | Row context |
|---|---|---|
| What it holds | Filters on columns | One current row of one table |
| Effect on tables | Hides rows | None, rows stay in the table |
| Main sources | Visuals, slicers, `CALCULATE` | Calculated columns, iterators |
| Lets you read | Aggregates over the visible rows | Single values of columns in that row |
| Follows relationships | Yes, as filters | Only through `RELATED` and `RELATEDTABLE` |
The last row of the table is the one that causes the most confusion. Filters travel through relationships automatically. A row context does not.
A row context does not filter
Put this calculated column in the Product table:
-- Calculated column in the Product table
Sales Without Transition =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )A newcomer expects each product to show its own sales, which would be 30, 15, and 40. Instead every product row shows 85. The calculated column creates a row context on Product, but SUMX iterates the Sales table, and nothing in a bare row context restricts Sales. The filter context during a calculated column refresh is empty, so all four sales rows are visible.
The next column fixes this by referencing the measure:
-- Calculated column in the Product table
Product Sales = [Sales Amount]This now returns 30, 15, and 40. Referencing a measure wraps it in an implicit CALCULATE, and CALCULATE converts the current row into filters. That conversion is called context transition, and it is the subject of later chapters. For now, remember the contrast: the first column shows that a row context alone does not filter, and the second shows that something must turn the row into a filter.
How the contexts layer in one evaluation
A single measure evaluation often involves both contexts, in a fixed order. The filter context is set first by the report and by any CALCULATE calls. Row contexts are then opened inside it by iterators.
Reading the diagram from top to bottom explains the matrix result for Blue. The header sets Color = "Blue", the relationship leaves only sale 3 visible, and SUMX opens one row context on that row and computes 15. The filter context controls how many rows the iterator visits, and the row context controls which row each step reads.
Deciding which context applies
When you read an unfamiliar formula, ask these questions in order:
- Where does this expression run? A measure starts with no row context. A calculated column starts with a row context on its own table, and its filter context is empty during refresh.
- What filters are active? For a measure, look at the visual, slicers, and any enclosing
CALCULATE. Each adds to or changes the filter context. - Does an iterator open a row context? Check for
SUMX,AVERAGEX,FILTER, and similar functions. The row context applies only inside their expression argument. - Does a measure reference or
CALCULATEappear inside that row context? If yes, context transition turns the current row into filters.
Following these steps on Products Over 1000 from the plan gives a quick preview of how they combine:
Products Over 1000 =
COUNTROWS (
FILTER ( Product, [Sales Amount] > 1000 )
)The measure starts with whatever filter context the visual supplies. FILTER iterates the visible products and opens a row context on each one. The [Sales Amount] reference inside it triggers context transition, so the sales amount is computed for that product only. Products that pass the test are counted. Every step in this chain gets its own chapter.
Common mistakes at this stage
These errors come from mixing up the two contexts.
- Expecting a row context to filter related tables. As the
Sales Without Transitioncolumn shows, iterating a table in a row context does not restrict other tables. UseRELATED,RELATEDTABLE, or context transition. - Expecting
RELATEDto work from the one side.RELATEDreads from the many side toward the one side, where each row has exactly one match. Going fromProducttoSalesreturns many rows, so you needRELATEDTABLEinstead. - Reading a column directly inside a measure. In a measure with no iterator,
Sales[Quantity]has no current row and DAX raises an error. Wrap the column in an aggregation such asSUM, or iterate withSUMX. - Treating the Total row as a sum of the rows above. The Total row has its own filter context with no filter on the grouped column. A measure that is not additive across rows can show a total that does not equal the visible column sum.
- Forgetting that slicers change every measure. A measure has no fixed value. Before you debug a number, write down the active filters for that cell.
What the rest of the tutorial covers
The remaining chapters take each piece apart in turn. They cover filter context and how filters travel across relationships, then row context with RELATED and RELATEDTABLE. Iterators come next, followed by CALCULATE and the filter modifiers ALL, REMOVEFILTERS, and KEEPFILTERS. The final chapters cover context transition, its pitfalls with duplicate rows and slow iterations, and nested row contexts with variables. The goal throughout is that you can predict the result of a formula before you run it.
Exercise
Using the model in this chapter, a matrix shows Product[Category] on the rows with the Sales Amount measure. What does the Bikes row show, and which rows of Sales were visible when it was computed?
Show answer
The Bikes row shows 70. The filter Category = "Bikes" leaves products P1 and P3, and the relationship passes that filter to Sales, so sales 1, 2, and 4 are visible (20 + 10 + 40). Sale 3 belongs to the Helmets product and is hidden.
Exercise
A calculated column in the Product table is defined as SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ). Why does it return 85 for every product, and what single change makes it return each product's own sales?
Show answer
The calculated column creates a row context on Product, but a row context does not filter Sales, and the filter context is empty during column refresh, so SUMX iterates all four Sales rows. Replacing the expression with a reference to the Sales Amount measure (or wrapping the SUMX in CALCULATE) triggers context transition, which turns the current Product row into a filter on Sales and gives 30, 15, and 40.
Exercise
Name the context that each of these creates: (a) a slicer selection on Color, (b) a calculated column, (c) the iteration done by AVERAGEX over Customer.
Show answer
(a) Filter context. (b) Row context on the table that holds the column. (c) Row context on the Customer table, one row at a time, inside whatever filter context already applies.