Sub topic 7
Comparing with previous, next, first, and last rows
This chapter assumes you can author a report in Power BI Desktop, write basic DAX measures, and have already created a visual calculation in the calculation editing experience. Here you compare each row with a neighbor or with the ends of the visual.
The relative functions
Relative functions read a value from another row in the same visual, based on its position. The four you need are:
| Function | Returns the value from |
|---|---|
| `PREVIOUS` | The row before the current row |
| `NEXT` | The row after the current row |
| `FIRST` | The first row on the axis |
| `LAST` | The last row on the axis |
Each takes the expression to read, such as [Sales Amount]. Position depends on the visual's layout and sort order. If you sort a table by Sales Amount descending, "previous" means the row above in that sorted order, not the previous month.
Change from the previous row
Suppose a table has Month on rows and Sales Amount as a value. Add this visual calculation:
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])For the first row, PREVIOUS has no row to read and returns blank. The subtraction then returns the row's own Sales Amount, because DAX treats a blank as zero in subtraction. That makes the first row look like a large change. If you prefer a blank, test for it:
Change vs previous =
IF(
ISBLANK(PREVIOUS([Sales Amount])),
BLANK(),
[Sales Amount] - PREVIOUS([Sales Amount])
)To express the change as a percentage, use DIVIDE so the first row does not cause a division error:
Percent change = DIVIDE([Sales Amount] - PREVIOUS([Sales Amount]), PREVIOUS([Sales Amount]))Looking forward with NEXT
NEXT works the same way in the other direction. It is useful when you want to see what a row is about to become:
Next month sales = NEXT([Sales Amount])The last row returns blank, since no row follows it.
Comparing with the first and last rows
FIRST and LAST give a fixed reference point. Every row reads the same value, so they suit index-style comparisons:
Versus first = [Sales Amount] - FIRST([Sales Amount])Versus last = [Sales Amount] - LAST([Sales Amount])The first example shows growth since the start of the period. You can also divide by the first value to get an index, where the first row equals 1:
Index = DIVIDE([Sales Amount], FIRST([Sales Amount]))Common errors
- Wrong sort order. The result follows the visual's sort. Sort Month chronologically before trusting a comparison.
- Unexpected first-row values. A blank from
PREVIOUSbecomes zero in arithmetic. Handle it explicitly, as shown above. - Multiple levels. In a matrix with a hierarchy on rows, these functions move within the current level by default. Use the axis and reset parameters, covered in the next chapters, to change that.
- Missing fields. The calculation can only refer to fields that are in the visual. Add the field first, and hide it if you do not want to display it.
Exercise
Exercise
A table shows Month on rows and Sales Amount as a value, sorted by Month ascending. Write a visual calculation that returns each month's Sales Amount as a percentage of the first month's Sales Amount, and explain what the first row shows.
Show answer
Percent of first = DIVIDE([Sales Amount], FIRST([Sales Amount])). Format the result as a percentage. The first row shows 100%, because it is divided by itself. Later rows show how much larger or smaller they are than the first month.