Power Pivot & DAX course · Lesson 6 of 16
Filter context and row context in DAX
The two DAX contexts explained with numbers: how PivotTable filters reach a measure, how row context works in columns, and context transition.
Every DAX formula is evaluated inside a context, and almost every surprising DAX result comes from misunderstanding which context is in play. There are two of them. Filter context decides which rows a measure can see. Row context says which single row a formula is currently looking at. Once you can tell them apart, CALCULATE, iterators and time intelligence stop being magic. This lesson is mostly concepts, but every idea is shown with numbers you can verify.
Filter context: what a measure sees
Take the measure Revenue := SUM ( Sales[Amount] ) and put it in a PivotTable with Customers[Region] on rows and Products[Category] on columns:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Region | Accessories | Computers | Displays | Grand Total |
| 2 | East | 240 | 567 | 807 | |
| 3 | North | 175 | 2320 | 2495 | |
| 4 | South | 330 | 950 | 1280 | |
| 5 | West | 1680 | 378 | 2058 | |
| 6 | Grand Total | 745 | 4950 | 945 | 6640 |
Revenue by Region (rows) and Category (columns). Blank cells: no rows survive the filters, so SUM returns BLANK.
The formula is the same in every cell, yet each cell shows a different number. That is because each cell has its own filter context: the set of filters applied before the formula runs. For the North / Computers cell the filter context is Customers[Region] = North and Products[Category] = Computers. The engine then:
- Applies
Region = Northto Customers, leaving only Anna Weber (C1). - Applies
Category = Computersto Products, leaving Laptop and Tablet (P1, P5). - Follows both relationships down to Sales and keeps only rows with CustomerID C1 and ProductID P1 or P5: orders 1001 and 1009.
- Evaluates
SUM ( Sales[Amount] )over those two rows: 1,900 + 420 = 2,320.
The Grand Total row and column work the same way, just with fewer filters: the North total has only the region filter, the bottom-right cell has none at all. Totals are not sums of the cells above them; they are new evaluations with a smaller filter context. For SUM the two give the same answer; for distinct counts, averages and ratios they do not, as you saw in Your first DAX measures: SUM, AVERAGE, COUNTROWS, DISTINCTCOUNT.
Where filters come from
- Row labels and column labels of the PivotTable.
- Report filters (the Filters area) and slicers or timelines connected to the PivotTable.
- Filter arguments inside
CALCULATE(next lesson), which can add, replace or remove filters.
Here is the same PivotTable with a slicer on Customers[Segment] set to Business. Anna Weber is a consumer, so North disappears entirely: the slicer adds a third filter to every cell.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Region | Accessories | Computers | Displays | Grand Total |
| 2 | East | 240 | 567 | 807 | |
| 3 | South | 330 | 950 | 1280 | |
| 4 | West | 1680 | 378 | 2058 | |
| 5 | Grand Total | 570 | 2630 | 945 | 4145 |
Same PivotTable with the Segment slicer set to Business.
Row context: one row at a time
Row context exists when DAX steps through a table row by row. It is created in two places: calculated columns (the formula runs once for each row of the table) and iterator functions such as SUMX (see Iterators in DAX: SUMX, AVERAGEX, MAXX and RANKX). Inside a row context, a column reference such as Sales[Qty] means the value of Qty in the current row.
Line Cost = Sales[Qty] * RELATED ( Products[Cost] )A measure, by contrast, has no row context. It is evaluated for a PivotTable cell, which may cover hundreds of rows. Writing Sales[Amount] alone in a measure therefore makes no sense, and Power Pivot reports an error along the lines of a single value for column Amount in table Sales cannot be determined. In a measure, a column must always be wrapped in something that turns many values into one: SUM, MAX, COUNTROWS, an iterator and so on.
Row context does not filter
This is the most important sentence of the lesson. Add a calculated column to the Customers table:
Wrong Total = SUM ( Sales[Amount] )You might expect each customer's own revenue. Instead every row shows 6,640, the total of all sales. The row context knows which Customers row is current, but SUM works on the Sales table, and a row context on Customers does not filter Sales. There is no filter context at all during a calculated-column refresh, so SUM sees every row.
The fix is to wrap the expression in CALCULATE. CALCULATE performs context transition: it turns the current row context into an equivalent filter context (CustomerID = C1, Customer = Anna Weber, Region = North...), which then flows through the relationship to Sales:
Customer Revenue = CALCULATE ( SUM ( Sales[Amount] ) )| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | CustomerID | Customer | Region | Wrong Total | Customer Revenue |
| 2 | C1 | Anna Weber | North | 6640 | 2495 |
| 3 | C2 | Brook Dental | South | 6640 | 1280 |
| 4 | C3 | Carter & Co | East | 6640 | 807 |
| 5 | C4 | Delta Schools | West | 6640 | 2058 |
Customers with both calculated columns. Only the CALCULATE version filters Sales.
Check Anna Weber: orders 1001, 1004 and 1009 total 2,495. The same happens automatically when you reference a measure inside a row context, because every measure is implicitly wrapped in CALCULATE. So Customer Revenue = [Revenue] would also work. This hidden behaviour is powerful but can be slow on big tables, which is one reason calculated columns on fact tables should be rare.
Both contexts together
Iterators combine the two contexts. In a PivotTable cell for Computers, the measure
Revenue (X) := SUMX ( Sales, Sales[Qty] * RELATED ( Products[Price] ) )first receives the cell's filter context, so the Sales table it iterates contains only computer sales. Then SUMX creates a row context on each of those rows, multiplies quantity by price and adds up the results. Filter context chooses the rows; row context walks through them.
Summary
| Filter context | Row context | |
|---|---|---|
| Created by | PivotTable rows, columns, slicers, filters; CALCULATE | Calculated columns; iterators such as SUMX, FILTER, RANKX |
| Meaning | Which rows are visible | Which single row is current |
| Propagates along relationships | Yes, from one side to many side | No (use RELATED to look up, CALCULATE to convert) |
| Present in measures | Always | Only inside an iterator |
With this vocabulary, the next lesson, CALCULATE: changing the filter context in DAX, is about the one function that lets you change the filter context on purpose.
Check yourself
1. What decides which Sales rows a measure uses in a PivotTable cell?
Filter context selects the visible rows.
2. Why does a calculated column = SUM ( Sales[Amount] ) in Customers show the grand total on every row?
Without CALCULATE, the row context on Customers does not become a filter on Sales.
3. What does CALCULATE do in a calculated column?
Context transition converts the current row into filters.
4. Which of these creates a row context?
Iterators such as SUMX walk through a table row by row.
5. Is the Grand Total of a PivotTable the sum of the visible cells?
Totals are evaluated in their own, smaller filter context.