cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ashrafkotb
Helper I
Helper I

How to create a calculated column to subtract previous row value?

Hello,

I need to create a calculated column that subtracts the current row value from the previous row value based on date and customer name. For example:

 

DateValueCustomerCalc_column (expected value)
01-04-2020100A 
01-04-2020400B 
01-04-2020500C 
02-04-2020120A20
02-04-2020440B40
02-04-2020560C60

 

Thanks in advance!

 

1 ACCEPTED SOLUTION
az38
Super User
Super User

Hi @ashrafkotb 

try

Calc_column = 
var _thisDate = Table[Date]
var _prevDate = CALCULATE(MAX(Table[Date]), ALLEXCEPT(Table, Table[Customer]), Table[Date] < _thisDate )

RETURN
Table[Value] - CALCULATE(MAX(Table[Value]), ALLEXCEPT(Table, Table[Customer]), Table[Date] = _prevDate )

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

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...

 

So in your case, something like: 

Calc_column = 
  VAR __PreviousDate = MAXX(FILTER('Table',[Date] < EARLIER([Date]) && [Customer] = EARLIER([Customer])),[Date])
  VAR __PreviousValue = MAXX(FILTER('Table',[Date] = __PreviousDate) && [Customer] = EARLIER([Customer])),[Date])
RETURN
  [Value] - __PreviousValue
  

@ me in replies or I'll lose your thread!!!
Check out my External Tool for Power BI Desktop! Microsoft Hates Greg's Quick Measures
YouTube Channel! Microsoft Hates Greg
Check out my latest book!

Getting a Syntax error for this formula.

amitchandak
Super User
Super User

@ashrafkotb ,

Try new columns like

max date = maxx(filter(Table, table[Date]<earlier(Table[Date]) && table[Customer]=earlier(Table[Customer])),Table[Date])
diff = Table[value] - maxx(filter(Table, table[Date]=earlier(Table[max date]) && table[Customer]=earlier(Table[Customer])),Table[value])

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin

Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!
az38
Super User
Super User

Hi @ashrafkotb 

try

Calc_column = 
var _thisDate = Table[Date]
var _prevDate = CALCULATE(MAX(Table[Date]), ALLEXCEPT(Table, Table[Customer]), Table[Date] < _thisDate )

RETURN
Table[Value] - CALCULATE(MAX(Table[Value]), ALLEXCEPT(Table, Table[Customer]), Table[Date] = _prevDate )

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

This worked, thank you.

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

November Power BI Update 768x460.png

Check it Out!

Click here to read more about the November 2021 Updates!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.