Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Sania
Frequent Visitor

How to calculate last row-previous row difference in matrix based on date

hi all,

I have matrix as below where I can drill down data based on year/qurter/month/dates. Now I want to have a row in this matrix which calculates last row-previous row value.

 

Today as its 17th feb 2023, matrix will have data till 16th feb 2023. 

So today on 17th feb 2023 = 16th data -15th data (want to calculate difference data of 16-data of 15 and display it)

on 18th = 17th data-16th data.
on 19th=18th-17th data

 

and so on.... So its based on current row - previous row

Sania_0-1676637986448.png

Thankyou in advance for help

6 REPLIES 6
Sania
Frequent Visitor

  • People please kindly share it or add relevant tags , hashes so that the post can have a better reach. I am new to this community and not sure how to maximize the reach of post to get it to take it to the wider audience to help ke on it.

Hi @Sania 

Would something like this help?

v1_Diff = 
VAR _CurrDt = SELECTEDVALUE( 'DataTable'[date] )
VAR _PrevDt = 
    MAXX(
        FILTER(
            ALL( 'DataTable'[date] ),
            'DataTable'[date] < _CurrDt
        ),
        'DataTable'[date]
    )
VAR _Curr_v1 = 
    CALCULATE(
        SUM( 'DataTable'[v1] ),
        'DataTable'[date] = _CurrDt
    )
VAR _Prev_v1 = 
    CALCULATE(
        SUM( 'DataTable'[v1] ),
        'DataTable'[date] = _PrevDt
    )
VAR _Result = _Curr_v1 - _Prev_v1
RETURN
    _Result

pbix: Previous_Values.pbix

 

Hi @Sania 

After re-reading your question, I noticed I forgot to offset the dates by 1.

The first variable defined should be:

VAR _CurrDt = SELECTEDVALUE( 'DataTable'[date] ) - 1
 

Thankyou @grantsamborn @, will try the given solution.

Sania
Frequent Visitor

Hi Olgad,

 

I would like to provide some sample data.

Here the last date is 16th, prev day is 15th,so i want difference in values as below -

datev1v2v3v4
2/1/2023986510010
2/2/2023994520020
2/3/20231002330030
2/4/20231011040040
2/5/2023546750050
2/6/2023565860060
2/7/202374870070
2/8/202383980080
2/9/2023252990090
2/10/2023911000100
2/11/202398902000101
2/12/202376783000102
2/13/202354674000103
2/14/20233245565000104
2/15/202321346000105
2/16/202312237000106
Difference b/w last row and previous row-9-1110001

now if I take tomorrow , I would get 17th data , so i want to subtract 17th -16th data values

datev1v2v3v4
2/1/2023986510010
2/2/2023994520020
2/3/20231002330030
2/4/20231011040040
2/5/2023546750050
2/6/2023565860060
2/7/202374870070
2/8/202383980080
2/9/2023252990090
2/10/2023911000100
2/11/202398902000101
2/12/202376783000102
2/13/202354674000103
2/14/20233245565000104
2/15/202321346000105
2/16/202312237000106
2/17/202376783000102
Difference b/w last row and previous row6455-4000-4

 

If not exact any thing that can achieve me to do this would be helpful. I provided simple data, but my actual data goes at hierarchical level. Sample snippet -

Sania_0-1676656119192.png

Thankyou in advance.

olgad
Super User
Super User

Hi, if you are on a new Power BI version (after Dec 2022), then window functions.

 

Please refer to the article with a step by step how to do it using offset function.

 

https://www.linkedin.com/pulse/can-window-functions-do-heavy-lifting-hr-reports-olga-dontsova/?track...

Let me know if you have any questions


DID I ANSWER YOUR QUESTION? PLEASE MARK MY POST AS A SOLUTION! APPRECIATE YOUR KUDO/LIKE!
PROUD TO BE A SUPER USER!
Best Stories, Interesting Cases: PowerBI Storytime Newsletter
Linkedin Profile: Linkedin
YouTube Channel: PowerBI Storytime

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.