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.

Intermediate 18 min read

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:

ABCDE
1RegionAccessoriesComputersDisplaysGrand Total
2East240567807
3North17523202495
4South3309501280
5West16803782058
6Grand Total74549509456640

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:

  1. Applies Region = North to Customers, leaving only Anna Weber (C1).
  2. Applies Category = Computers to Products, leaving Laptop and Tablet (P1, P5).
  3. Follows both relationships down to Sales and keeps only rows with CustomerID C1 and ProductID P1 or P5: orders 1001 and 1009.
  4. 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.

ABCDE
1RegionAccessoriesComputersDisplaysGrand Total
2East240567807
3South3309501280
4West16803782058
5Grand Total57026309454145

Same PivotTable with the Segment slicer set to Business.

Tip. When a number looks wrong, list the filter context of that cell out loud: every row label, column label, slicer and report filter. Nine times out of ten the surprise is a filter you forgot, such as a slicer on another sheet connected to the same PivotTable.

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.

DAX
Line Cost = Sales[Qty] * RELATED ( Products[Cost] )
A calculated column in Sales: Sales[Qty] is this row's quantity.

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:

DAX
Wrong Total = SUM ( Sales[Amount] )
Calculated column in Customers.

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:

DAX
Customer Revenue = CALCULATE ( SUM ( Sales[Amount] ) )
Calculated column in Customers, now filtered per customer.
ABCDE
1CustomerIDCustomerRegionWrong TotalCustomer Revenue
2C1Anna WeberNorth66402495
3C2Brook DentalSouth66401280
4C3Carter & CoEast6640807
5C4Delta SchoolsWest66402058

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.

Note. Context transition has one more subtlety: the filter it creates contains all columns of the current row. If a table has duplicate rows, the filter matches several rows at once and the result can be a multiple of what you expected. Dimension tables with a unique key do not have this problem.

Both contexts together

Iterators combine the two contexts. In a PivotTable cell for Computers, the measure

DAX
Revenue (X) := SUMX ( Sales, Sales[Qty] * RELATED ( Products[Price] ) )
An iterator measure.

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 contextRow context
Created byPivotTable rows, columns, slicers, filters; CALCULATECalculated columns; iterators such as SUMX, FILTER, RANKX
MeaningWhich rows are visibleWhich single row is current
Propagates along relationshipsYes, from one side to many sideNo (use RELATED to look up, CALCULATE to convert)
Present in measuresAlwaysOnly 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?

2. Why does a calculated column = SUM ( Sales[Amount] ) in Customers show the grand total on every row?

3. What does CALCULATE do in a calculated column?

4. Which of these creates a row context?

5. Is the Grand Total of a PivotTable the sum of the visible cells?