Sub topic 7
Comparing with Previous, Next, First, and Last Rows
This chapter assumes you can build a matrix or chart in Power BI Desktop and write basic DAX measures. It also assumes you have created a visual calculation, as covered in the earlier chapters. Here you will use the relative functions PREVIOUS, NEXT, FIRST, and LAST to compare each row of a visual with another row.
Why relative functions exist
Many business questions compare one row with a neighbor. Is this month higher than last month? How far is each product from the best seller? A measure can answer these, but it needs time intelligence functions or CALCULATE with filter manipulation. A visual calculation can refer directly to the row above or below in the visual.
The four functions are:
| Function | Returns the value from |
|---|---|
| `PREVIOUS` | The row before the current row |
| `NEXT` | The row after the current row |
| `FIRST` | The first row of the axis |
| `LAST` | The last row of the axis |
All four take an expression as the first argument. They work on the visual's own layout, so "previous" means previous in the order the visual shows, not previous in the data model.
Setting up the example
The examples use a matrix with Month on the rows and [Sales Amount] as a value. Sort Month in calendar order. The sort order matters, because the relative functions follow whatever order the visual displays.
| Month | Sales Amount |
|---|---|
| Jan | 100 |
| Feb | 120 |
| Mar | 90 |
| Apr | 150 |
Comparing with the previous row
Select the visual, choose New calculation, and enter:
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])The result for each row:
| Month | Sales Amount | Change vs previous |
|---|---|---|
| Jan | 100 | 100 |
| Feb | 120 | 20 |
| Mar | 90 | -30 |
| Apr | 150 | 60 |
The first row has no previous row. PREVIOUS returns blank there, and DAX treats blank as zero in subtraction. January therefore shows 100, which is misleading. Most reports want a blank instead.
A safer version:
Change vs previous =
IF(
ISBLANK(PREVIOUS([Sales Amount])),
BLANK(),
[Sales Amount] - PREVIOUS([Sales Amount])
)Now January shows nothing and the other rows are unchanged.
Percent change from the previous row
A percent change builds on the same idea. Use DIVIDE to avoid division by zero errors.
Percent change =
DIVIDE(
[Sales Amount] - PREVIOUS([Sales Amount]),
PREVIOUS([Sales Amount])
)DIVIDE returns blank when the denominator is blank or zero. January therefore shows blank, which is the behavior you want. Set the data type to a percentage with the formatting options covered in a later chapter.
Looking ahead with NEXT
NEXT mirrors PREVIOUS. It is useful when you want to see what happens after the current row, for example to compare a forecast with the following period.
Change to next = NEXT([Sales Amount]) - [Sales Amount]| Month | Sales Amount | Change to next |
|---|---|---|
| Jan | 100 | 20 |
| Feb | 120 | -30 |
| Mar | 90 | 60 |
| Apr | 150 | -150 |
The last row has no next row, so NEXT returns blank and the result is 0 - 150 = -150. The same blank-handling advice applies.
Stepping more than one row
PREVIOUS and NEXT accept an optional second argument, the number of steps. The default is 1. To compare each month with the same month in the prior quarter, step back three rows on a monthly axis:
Change vs 3 rows back = [Sales Amount] - PREVIOUS([Sales Amount], 3)This is a position-based comparison. It is correct only when the visual shows every month with no gaps. If a filter removes a month, "three rows back" no longer means three months back. For true same-period-last-year logic, a measure that uses time intelligence is more reliable.
Comparing with the first and last rows
FIRST and LAST return the value at the beginning or end of the axis. They are well suited to index calculations, where every row is expressed relative to a baseline.
Index vs first = DIVIDE([Sales Amount], FIRST([Sales Amount]))| Month | Sales Amount | Index vs first |
|---|---|---|
| Jan | 100 | 1.00 |
| Feb | 120 | 1.20 |
| Mar | 90 | 0.90 |
| Apr | 150 | 1.50 |
Format the result as a percentage and January reads 100%, April 150%. Using LAST reverses the baseline:
Gap to latest = LAST([Sales Amount]) - [Sales Amount]This shows how far each month sits below April. Because LAST is evaluated for the whole axis, every row compares against the same value.
Like PREVIOUS and NEXT, FIRST and LAST can take an optional number of steps. FIRST([Sales Amount], 2) returns the value of the second row, counting from the start, and LAST([Sales Amount], 2) returns the second row from the end.
How the functions read the visual
The functions do not query the model again. They read the cells already computed for the visual. This explains several behaviors you will notice.
Three consequences follow:
- Sorting the visual changes the results. If you sort Month by Sales Amount, "previous" becomes the next-larger value, not the previous month.
- Filtering the visual changes the results. Slicers that remove rows change which row counts as previous or first.
- Totals behave differently. The total row has no neighbor, so relative functions usually return blank there. Expect an empty or odd total on a comparison column.
Multiple fields on the axis
When rows contain more than one field, such as Year and then Month, the relative functions need to know how to move through the layout. By default they use the visual's default axis. Chapter-level details of the ROWS and COLUMNS choices come in the next chapter on the axis parameter. For now, note that a calculation that works with one field on the rows may behave differently once you add a second level, because "previous" may cross a year boundary or stay within one.
Common errors
- A blank-as-zero first row. Covered above. Wrap with
ISBLANKor useDIVIDE. - Wrong order. The visual is sorted by value, not by time. Sort by the field that defines the order you want.
- Comparing across gaps. A filter removed a period, so the neighboring row is not the period you meant. Use a measure when the comparison must be calendar-aware.
- Using the result in a total. Summing a change column rarely matches what you expect. Check the total row before sharing a report.
- Referencing a field not in the visual. A visual calculation can refer only to fields that are in the visual. Add the field first, then hide it if you do not want it displayed.
When to choose a measure instead
Use a relative function when the comparison is about display position: row to row, first to current, current to last. Use a measure when the comparison must hold regardless of layout, such as "same month last year" that should survive filters, or when other visuals and reports must reuse the logic. A visual calculation lives on one visual and cannot be reused elsewhere.
Exercises
Exercise
A matrix shows Month on the rows, sorted in calendar order, with [Sales Amount]. You write Change = [Sales Amount] - PREVIOUS([Sales Amount]) and the first month shows the full sales amount instead of a blank. Why does this happen, and how do you fix it?
Show answer
PREVIOUS returns a blank on the first row because there is no earlier row, and DAX treats the blank as zero in subtraction, so the result equals the current value. Fix it by testing for the blank, for example IF(ISBLANK(PREVIOUS([Sales Amount])), BLANK(), [Sales Amount] - PREVIOUS([Sales Amount])).
Exercise
Write a visual calculation that shows each month's sales as a share of the last month's sales in the visual, and explain what the last row will show.
Show answer
Use DIVIDE([Sales Amount], LAST([Sales Amount])). The last row divides the value by itself, so it shows 1, which is 100% when formatted as a percentage. Every other row shows its ratio to the last month.
Exercise
A colleague sorts the matrix by Sales Amount descending and the Change vs previous column no longer shows month-over-month changes. Why?
Show answer
Relative functions follow the order the visual displays. After sorting by sales, the previous row is the next-higher sales value, not the previous month. Sort the matrix by Month again, or use a measure with time intelligence if the comparison must not depend on sort order.