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 |
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 |
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
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
![]() |
| Previous Row Using Index and Rownumber Function |
Conclusion
- 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
Continue Exploring Power BI
If you found this useful, you might also like:
- Learn how to Create a Pareto Chart in Power BI using DAX.
- See how I computed Running Total in Power BI using DAX Window Function.
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
Post a Comment