How to Get the Previous Row Value in Power BI Using the DAX INDEX Function

As the title suggests, in this post I show you how to get the previous row's value using the DAX INDEX function.

Previous Row using Index DAX function in Power BI
Previous Row using Index DAX function in Power BI

You need the previous row more often than you might think. Some common examples are:

  • Calculating the variance between the current and previous period (day, month, quarter, or year)
  • Comparing sales, profit, or growth % across products or categories
  • Calculations where the current value depends on the previous one

These are just a few scenarios. There are many more!

There are several ways to get a previous row value in Power BI. You can add an index column in Power Query, use the OFFSET window function, or use a visual calculation. In this post, though, I'll focus only on the INDEX function. Once you understand how it works, you can apply it to plenty of other requirements.

What Is the DAX INDEX Function?

INDEX is a DAX window function that returns a row at an absolute position within a table you define. I'm going to show 2 examples and because the usage is inside a measure, it works directly in any table or matrix visual.

Example 1: Previous Row Using the Month Number

I built a simple table visual with the Month column and the [Total Sales] measure. Here's the measure that returns the previous row's sales:

Previous Row Using Index Function the Month Number
Previous Row Using Index Function the Month Number

As the screenshot shows, [Previous Row Sales] correctly returns the Total Sales value from the row above.

How the Calculation Works

1. The position argument

The first argument to INDEX is the position. Here it's SELECTEDVALUE(Datedim[Month]) - 1.
The Month column stores the month number: 1 for January, 2 for February, and so on. SELECTEDVALUE returns the current row's month number, and subtracting 1 gives the previous month's position. For February (2), the result is 1 (January).

2. The relation (table) argument

The second argument is the table INDEX searches: ALL(Datedim[Month Name], Datedim[Month]). It includes every field used in the visual. I included both Month Name and Month so the months keep their correct sort order.

3. The sort order

ORDERBY(Datedim[Month], ASC) tells INDEX exactly how to order the rows. Without it, INDEX falls back to a default sort, which may not match the order you expect.

4. The return statement

CALCULATE evaluates [Total Sales] using the row that INDEX returned. The Indx variable acts as the filter, so each row gets the sales value of the row before it.

What about January?

January has no previous row. Position 0 doesn't exist, so INDEX returns blank and the measure shows blank for January. That's usually the result you want.

Example 2: Previous Row Without a Month Number

There's a catch. What if your visual doesn't use months? If there's no numeric column like a month number, you have nothing to subtract 1 from.

This is where ROWNUMBER helps. It returns the position of each row in the visual, and you can pass that number into INDEX:
Previous Row Using Index and Rownumber Function
Previous Row Using Index and Rownumber Function

The result is exactly the same. Because ROWNUMBER generates the position for you, this approach works on any kind of data: products, categories, customers, or anything else.

Tip: If your report has slicers and you want the previous row to respect the user's selection, try ALLSELECTED instead of ALL in the relation argument.

Conclusion

Getting the previous row value is a common need in Power BI reporting, and the DAX INDEX function handles it cleanly inside a measure.

In this post, I showed:
  • How INDEX returns a row at an absolute position
  • Using the month number to find the previous row
  • Why an explicit ORDERBY matters
  • How ROWNUMBER makes the approach work for any data, not just months
Once you're comfortable with INDEX, you can use the same pattern for variance calculations, period-over-period comparisons, and other row-to-row logic.

Have you used INDEX, OFFSET, or visual calculations for previous-row logic? Let me know which one you prefer in the comments.

Continue Exploring Power BI

If you found this useful, you might also like:

For more tutorials, visit Power BI with Akshay.

Thanks for reading! If you prefer video walkthroughs, catch my latest tutorials on my YouTube channel, Power BI with Akshay. Keep learning!


Comments