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
v2
Frequent Visitor

Subtract value from earlier rows depending on Category

Hi,

 

I am new to power Bi and I have seen a couple of solutions to this but have been unable to incorporate this in my solution.

 

Attached  table has a desired value I want to achieve using measures or any  any other way in Power BI

  • The date row has to be sorted in ascending order
  • the desired value should look to see if there is an earlier date for the row category  and subtract the current value form the previous value. If there is no previous date, it should list the same  value.  

 

DateCategoryValueDesired value
10-03-13CAT-A9456no value before this date so the value should remain as 9456
16-03-13CAT-B15238  no value before this date  so value remains the same 15238
03-04-13CAT-A12657= 12657-9456
29-01-14CAT-C3452=3452
30-03-14CAT-A14324=14324 - last CAT-A value
01-09-15CAT-B18234=18234 -15238
04-02-15CAT-C15234=15234-3452
15-10-15CAT-D4561=4561

 

 

Hope this makes sense!

 

Thanks

 

 

 

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Screenshot 2021-08-04 003432.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

4 REPLIES 4
CNENFRNL
Community Champion
Community Champion

Screenshot 2021-08-04 003432.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Great solution! Worked very well on my dataset aswell. Is there a way to create a similar solution in the Power Query Editor? It seems that due to large amount of data i keep getting errors.

 

Much appreciated - thanks!

v2
Frequent Visitor

Excellent ! exactly what i needed. Thank you.

Greg_Deckler
Super User
Super User

@v2 See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous

 

Also: https://community.powerbi.com/t5/Quick-Measures-Gallery/Mean-Time-Between-Failure-MTBF/m-p/625082#M3...


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.