Row context in DAX explained with examples

What row context is in DAX, where it comes from (calculated columns and iterators like SUMX and FILTER), why it does not filter other tables, how RELATED and RELATEDTABLE follow relationships, nested row contexts, and how CALCULATE turns a row into a filter.

Short answer: row context means "the current row": DAX knows which row it is on and can read that row's columns. It exists in two places: in a calculated column (each row in turn) and inside an iterator function such as SUMX, AVERAGEX, FILTER or ADDCOLUMNS (each row of the table you give it). Row context does not filter anything: a SUM inside it still sums the whole table. To follow a relationship use RELATED (to the one side) or RELATEDTABLE (to the many side); to turn the row into a filter, use CALCULATE.

Two contexts drive every DAX result: filter context (what the visual and slicers filter) and row context (which row an iteration is on). Mixing them up gives the classic puzzle: a calculated column = SUM(Sales[Amount]) shows the grand total on every row.

Where does row context come from?

  1. 1
    Calculated columnthe formula runs once per row of its table
  2. 2
    IteratorSUMX, AVERAGEX, MAXX, COUNTX, FILTER, ADDCOLUMNS, RANKX run the expression once per row
  3. 3
    InsideTable[Column] reads the current row's value
Two sources of row context
-- calculated column on Sales: row context from the column
Line Amount = Sales[Quantity] * Sales[Unit Price]

-- measure: row context from SUMX over Sales
Revenue = SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )

A measure has no row context of its own: Sales[Quantity] alone in a measure is an error ("a single value for column cannot be determined"), because there is no current row.

Why does SUM in a column give the grand total?

-- calculated column on Customer
Customer Sales Wrong = SUM ( Sales[Amount] )
Customer Sales Right = CALCULATE ( SUM ( Sales[Amount] ) )
Customer Sales Also Right = SUMX ( RELATEDTABLE ( Sales ), Sales[Amount] )
CustomerCustomer Sales WrongCustomer Sales Right
Ana12,4003,100
Ben12,4007,800
Chen12,4001,500
Calculated columns on the Customer table

The row context on Customer says "the current customer is Ana", but it filters nothing, so SUM sees all of Sales. CALCULATE performs context transition: the current row becomes a filter (Customer = Ana), which flows through the relationship to Sales. RELATEDTABLE returns Ana's sales rows directly.

Row context can follow relationships in a direction:

FunctionFromReturns
RELATED ( Product[Category] )a row on the many side (Sales)the one matching value on the one side
RELATEDTABLE ( Sales )a row on the one side (Customer)the table of matching rows on the many side
-- on Sales: the category of each line's product
Category = RELATED ( 'Product'[Category] )

-- measure: revenue at list price, reading the product's list price per line
Revenue at List =
SUMX ( Sales, Sales[Quantity] * RELATED ( 'Product'[List Price] ) )

What about nested row contexts?

Iterators can be nested; each adds a row context. An inner reference to the same table sees the inner row. To reach the outer row, store it in a variable first (older formulas used EARLIER):

-- calculated column on Product: rank by list price within the category
Price Rank in Category =
VAR ThisCategory = 'Product'[Category]
VAR ThisPrice = 'Product'[List Price]
RETURN
    COUNTROWS (
        FILTER (
            'Product',
            'Product'[Category] = ThisCategory
                && 'Product'[List Price] > ThisPrice
        )
    ) + 1

Inside FILTER, 'Product'[Category] is the inner row; ThisCategory holds the outer one. See EARLIER and EARLIEST for the older form.

What does it cost?

An iterator runs its expression once per row. SUMX ( Sales, ... ) over 50 million rows is fast when the expression is simple column math, because the storage engine does it in bulk. It gets slow when each row calls a measure (a context transition per row) or FILTER over a whole large table. Iterate the smallest table that gives the right answer: SUMX ( VALUES ( Customer[Customer] ), [Revenue] ) iterates customers, not sales lines.

What goes wrong?

  • Aggregates in a column without CALCULATE. They ignore the row; wrap them in CALCULATE or use RELATEDTABLE.
  • A column reference in a measure without an iterator. No row context: use an aggregate or an iterator.
  • RELATED the wrong way. RELATED goes to the one side only; from the one side use RELATEDTABLE.
  • Measures inside large iterators. Each call is a context transition; iterate a smaller table.

Where can you check this?

Where do you start?

Write Customer Sales Wrong and Customer Sales Right side by side in a test model and look at the difference. Then read filter context and context transition, the other half of DAX.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.