EARLIER DAX Function in Power BI
The EARLIER() function in Power BI DAX (Data Analysis Expressions) is a row context function used when working with calculated columns, not with measures. It allows you to access the value of a column in a previous row context. This is especially useful when performing row-by-row calculations that depend on values from earlier or outer iterations.
DAX Syntax EARLIER(column_name, number)
The function has the following parameters:
- column_name: The column whose earlier row context you want to retrieve.
- number: It is an optional parameter. The number of outer row contexts to go back to. Default is 1.
Conceptual Understanding When we use EARLIER(), we are nesting row contexts — for example, using CALCULATE, FILTER, or iterators like SUMX inside a calculated column. DAX maintains a stack of row contexts. EARLIER() gives access to values from outer contexts in that stack.
📊 Practical Example Scenario: We have a table of sales data like this:
| Salesperson | Region | Sales |
|---|---|---|
| Alice | East | 500 |
| Bob | East | 700 |
| Alice | West | 600 |
| Bob | West | 800 |
Now, we want to calculate a ranking of each salesperson within each region based on sales.
Step-by-Step Use of EARLIER 1. Add a calculated column: Sales Rank
DAX
Sales Rank =
CALCULATE(
COUNTROWS('SalesData'),
FILTER(
'SalesData',
'SalesData'[Region] = EARLIER('SalesData'[Region])
&& 'SalesData'[Sales] > EARLIER('SalesData'[Sales])
)
) + 1 How This Works: Let’s break it down:
- We're in a row context of the outer row (say Alice, East, 500).
- Inside FILTER, we're iterating over the table again (new row context).
- EARLIER() grabs the outer row's values for Region and Sales.
- FILTER returns how many rows in the same region have higher sales than the outer row.
- COUNTROWS counts those and adds 1 to get the rank.
Output:
| Salesperson | Region | Sales | Sales Rank |
|---|---|---|---|
| Alice | East | 500 | 2 |
| Bob | East | 700 | 1 |
| Alice | West | 600 | 2 |
| Bob | West | 800 | 1 |
When to Use EARLIER:
✅ Use in:
- Calculated columns
- Ranking within groups
- Group-wise difference calculations
- When iterating over rows and comparing with outer row values
🚫 Don’t use in:
- Measures — EARLIER() works only with row context, and measures don’t operate row-by-row.
Equivalent Using Variables (Modern DAX Approach)
DAX
Sales Rank =
VAR CurrentRegion = 'SalesData'[Region]
VAR CurrentSales = 'SalesData'[Sales]
RETURN
CALCULATE(
COUNTROWS('SalesData'),
FILTER(
'SalesData',
'SalesData'[Region] = CurrentRegion
&& 'SalesData'[Sales] > CurrentSales
)
) + 1 This version is easier to read and avoids the confusing nested row context of EARLIER().