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
sonm10
Helper I
Helper I

Previous value with group/category

Hello all,

 

I have values for 3 items against date. I want to calculate the rate of change of the value for the item.The dates are not necessarily consecutives and each item may not have the same dates. If there was no item, I can use index to track the previous value. However with items I am lost.  I want my data as follows.

 

DateItemValueReturn (prev value - current)
11/12/2020A1 
11/15/2020A21
11/16/2020A30.5
11/17/2020A2-0.333333333
11/18/2020A1-0.5
11/19/2020A21
11/21/2020A1-0.5
11/22/2020A43
11/26/2020A50.25
11/12/2020B12 
11/15/2020B11-0.083333333
11/16/2020B120.090909091
11/17/2020B3-0.75
11/18/2020B216
11/19/2020B1-0.952380952
11/21/2020B21
11/22/2020B41
11/26/2020B60.5
11/12/2020C7 
11/15/2020C1-0.857142857
11/16/2020C21
11/17/2020C30.5
11/18/2020C50.666666667
11/19/2020C60.2
11/21/2020C70.166666667
11/22/2020C80.142857143
11/26/2020C6-0.25

 

Thank you.

 

1 ACCEPTED SOLUTION

@sonm10 - Sorry, I messed that up. 

 

Column =
VAR __Current = [Value]
VAR __PreviousDate = MAXX(FILTER('Test','Test'[Date] < EARLIER('Test'[Date]) && [Group] = EARLIER([Group])),[Date])
VAR __Previous = MAXX(FILTER('Test','Test'[Date] = __PreviousDate && [Group] = EARLIER([Group])),[Value])
RETURN
__Current - __Previous

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

View solution in original post

6 REPLIES 6
mahoneypat
Employee
Employee

Please try this column expression that gets your desired results.

 

Change =
VAR thisvalue = 'Table'[Value]
VAR thisitem = 'Table'[Item]
VAR thisdate = 'Table'[Date]
VAR prev =
CALCULATE (
LASTNONBLANKVALUE ( 'Table'[Date], MAX ( 'Table'[Value] ) ),
ALL ( 'Table' ),
'Table'[Date] < thisdate,
'Table'[Item] = thisitem
)
RETURN
DIVIDE ( thisvalue - prev, prev )

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


amitchandak
Super User
Super User

@sonm10 , Try a new column like

New column =
Var _Last_date = maxx(filter(table,[Date]<earlier([Date])),[Date])
var _Last_value = maxx(filter(table,[Date]=_Last_date),[Value])
return 
divide([Value]-_Last_value,_Last_value)

Thank you @amitchandak for your solution. However, since I have multiple items on the same date and i want the calculation to be done for each separate item, its not working. Your solution is working if there is no multiple item

Greg_Deckler
Super User
Super User

@sonm10 - If I understand what you are asking, 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 __Previous = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date]) && [Item] = EARLIER([Item])),[Value])
RETURN
  __Current - __Previous


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

Hello @Greg_Deckler. I have implemented your solution but few data are coming right but most of them are incorrect. Attached below the screenshot of the sample dataset.

 

sonm10_0-1598273625472.png

I have calcualted the new column as follows:


Column =
VAR __Current = [Value]
VAR __Previous = MAXX(FILTER('Test','Test'[Date] < EARLIER('Test'[Date]) && [Group] = EARLIER([Group])),[Value])
RETURN
__Current - __Previous


The return field is what should be the correct value. Its been calculated in excel for comparisons.

 

 

 

 

@sonm10 - Sorry, I messed that up. 

 

Column =
VAR __Current = [Value]
VAR __PreviousDate = MAXX(FILTER('Test','Test'[Date] < EARLIER('Test'[Date]) && [Group] = EARLIER([Group])),[Date])
VAR __Previous = MAXX(FILTER('Test','Test'[Date] = __PreviousDate && [Group] = EARLIER([Group])),[Value])
RETURN
__Current - __Previous

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