EARLIER and EARLIEST in DAX, and variables instead

What EARLIER and EARLIEST do in DAX, reaching the value of an outer row context inside a nested iteration, a running total and a rank as calculated columns, why variables replace them today, and how to rewrite old formulas.

Short answer: EARLIER ( column ) returns the value of column in the outer row context when you are inside a nested one, typically a FILTER inside a calculated column. EARLIER ( column, 2 ) goes two levels out, and EARLIEST goes to the outermost. They come from the time before DAX had variables. Today, store the outer value in a VAR before the inner iteration: the formula reads better and does the same.

Old models and forum answers are full of EARLIER. It is worth reading them, and worth rewriting them when you touch them.

What problem does EARLIER solve?

In a calculated column on Sales, a FILTER ( Sales, ... ) starts a new row context over Sales. Inside it, Sales[Order Date] means the inner row. To compare with the row the column is being calculated for, you need the outer row:

-- calculated column on Sales: running total of amount by date
Running Amount =
SUMX (
    FILTER ( Sales, Sales[Order Date] <= EARLIER ( Sales[Order Date] ) ),
    Sales[Amount]
)

EARLIER ( Sales[Order Date] ) is the date of the row being calculated; Sales[Order Date] alone is the row FILTER is looking at.

How do you write the same with a variable?

Running Amount =
VAR ThisDate = Sales[Order Date]
RETURN
    SUMX ( FILTER ( Sales, Sales[Order Date] <= ThisDate ), Sales[Amount] )

The variable is evaluated in the outer row context, before FILTER starts, and stays constant inside it. Same result, no level counting.

What about ranks?

-- calculated column on Product: rank by list price (1 = most expensive)
Price Rank Old =
COUNTROWS ( FILTER ( 'Product', 'Product'[List Price] > EARLIER ( 'Product'[List Price] ) ) ) + 1

Price Rank =
VAR ThisPrice = 'Product'[List Price]
RETURN
    COUNTROWS ( FILTER ( 'Product', 'Product'[List Price] > ThisPrice ) ) + 1

-- or, simplest
Price Rank RANKX = RANKX ( 'Product', 'Product'[List Price] )

What do the levels mean?

  1. 1
    Outermostthe row of the calculated column (EARLIEST)
  2. 2
    Level 1 outthe first FILTER or iterator (EARLIER ( col, 1 ), the default)
  3. 3
    Currentthe innermost iterator's row: the plain column reference
Nested row contexts in a calculated column

Microsoft notes that EARLIER succeeds only if there is a row context before the beginning of the table scan; otherwise it returns an error. In a measure there is no outer row context unless an iterator creates one.

Should these be columns at all?

Running totals and ranks as calculated columns are fixed at refresh and ignore slicers. In most reports they belong in measures:

Running Revenue =
CALCULATE ( [Revenue], FILTER ( ALLSELECTED ( 'Date'[Date] ), 'Date'[Date] <= MAX ( 'Date'[Date] ) ) )

Product Rank = RANKX ( ALLSELECTED ( 'Product'[Product] ), [Revenue] )

Or, on a single visual, a visual calculation: RUNNINGSUM ( [Revenue] ).

What goes wrong?

  • EARLIER in a measure without an iterator. There is no outer row; error.
  • Counting levels wrong. With three nested iterators, EARLIER ( col, 2 ) is easy to get wrong; variables avoid it.
  • Slow columns. FILTER ( Sales, ... ) per row of Sales is quadratic; on millions of rows a refresh can take hours. Use measures or Power Query.

Where can you check this?

Where do you start?

Search your model for EARLIER and rewrite each with a VAR; then ask whether the column should be a measure. Row context explained covers nested row contexts.

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.