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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Newbie992
Regular Visitor

How to get the difference between 2 rows in a column for category

I want to get the difference between the successive rows in a column(Confirmed_cases). I have indexed the columns by category but can't go further. Please help.

D1oT2vX.png

1 ACCEPTED SOLUTION

@Newbie992 try this

 

Diff = 
VAR __index = SELECTEDVALUE ( 'Table'[Index] ) 
VAR __prevIndex = __index - 1
VAR __prevValue = 
CALCULATE ( 
    SUM ( 'Table'[Confirmed_cases] ), 
    ALLEXCEPT ( 'Table', 'Table'[Location_Key] ), 
    'Table'[Index] = __prevIndex 
) 
RETURN
SUM ( 'Table'[Confirmed_cases] ) - __prevValue

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

6 REPLIES 6
parry2k
Super User
Super User

@Newbie992 try this measure

 

Diff =
VAR __index = SELECTEDVALUE ( Table[Index ) 
VAR __prevIndex = __index - 1
VAR __country = SELECTEDVALUE Table[Country] )
VAR __prevValue = CALULATE ( SUM ( Table[Cases] ), Table[Index] = __prevIndex, Table[Country] = __country )
RETURN
SUM ( Table[Cases] ) - __prevValue

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

  



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi,

Thanks for your reply!

It's not working for me. In the result, I get the same values that are in Confirmed_cases column. I checked and in the variable _prevValue I am not getting any values.

 

Thanks!

@Newbie992 do you getting value in the _index variable. Can you paste data in a table format so that I can copy it? I probably missed something, I pasted the expression from my phone and was a bit hard to type.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi @parry2k ,

 

Yes I see data in __index variable but not in __prevValue.

Here  is a sample of my data:

 

Location_KeyNew_Cases_DateConfirmed_casesIndex
Afghanistan2020-04-19 00:00:009961
Afghanistan2020-04-20 00:00:0010262
Afghanistan2020-04-21 00:00:0010923
Afghanistan2020-04-22 00:00:0011764
Afghanistan2020-04-23 00:00:0012795
Afghanistan2020-04-24 00:00:0013516
Albania2020-03-09 00:00:0021
Albania2020-03-10 00:00:00102
Albania2020-03-11 00:00:00123
Albania2020-03-12 00:00:00234

 

Thanks!

@Newbie992 try this

 

Diff = 
VAR __index = SELECTEDVALUE ( 'Table'[Index] ) 
VAR __prevIndex = __index - 1
VAR __prevValue = 
CALCULATE ( 
    SUM ( 'Table'[Confirmed_cases] ), 
    ALLEXCEPT ( 'Table', 'Table'[Location_Key] ), 
    'Table'[Index] = __prevIndex 
) 
RETURN
SUM ( 'Table'[Confirmed_cases] ) - __prevValue

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi @parry2k ,

 

It works!

 

Thank you so much for your help!

Appreciate it!

 

Kind Regards!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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