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

Finding the difference between values on two dates the first being hte most current and the second

Hello,

 

I have a dataset that has a value column with an associated Timestamp column. The timestamps are not uniform so the time between any two time stamps is different. What I am trying to do is to find the difference between the value associated with the current time stamp and the value at the next most recent time stamp. 

 

Table1

Value

TimeStamp

500

30-05-19 10:02

129

30-05-19 11:10

178

31-05-19 12:20

168

31-05-19 19:45

201

01-06-19 20:01

1235

02-06-19 19:42

1352

03-06-19 19:43

 

Is Latest Date Column = MAX(Table1[TimeStamp])

 

Last Date = MAX(Table1[Is Latest Date Column])

 

Second Last Day Column = calculate(max(Table1[TimeStamp]),filter(Table1,(Table1[TimeStamp])<(Table1[Is Latest Date Column])))

 

Second Last Date = MAX(Table1[Second Last Day Column])

 

Current Value = CALCULATE(MAX(Value[Value),filter(Table1,Table1[TimeStamp]=MAX(Value[TimeStamp])))

 

Previous Value = CALCULATE(MAX(Table1 [Value]),filter(Table1,MAX(Table1 [TimeStamp])< Table1 [Is Latest Date Column]))

 

Difference = calculate(Table1 [Current Value]- Table1 [Previous Value])

 

The Current Value being returned is correct but the Previous Value value being returned is the maximum of the value list not the filtered value

 

Any ideas?

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @sirgseymour ,

You can try to use following measure to calculate diff between current and previous value:

Measure =
VAR _currDate =
    MAX ( Table[TimeStamp] )
VAR _previous =
    CALCULATE (
        MAX ( Table[TimeStamp] ),
        FILTER ( ALLSELECTED ( Table ), [TimeStamp] < _currDate )
    )
RETURN
    MAX ( Table[Value] ) - LOOKUPVALUE ( Table[Value], Table[TimeStamp], _previous )

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

Hi @sirgseymour ,

You can try to use following measure to calculate diff between current and previous value:

Measure =
VAR _currDate =
    MAX ( Table[TimeStamp] )
VAR _previous =
    CALCULATE (
        MAX ( Table[TimeStamp] ),
        FILTER ( ALLSELECTED ( Table ), [TimeStamp] < _currDate )
    )
RETURN
    MAX ( Table[Value] ) - LOOKUPVALUE ( Table[Value], Table[TimeStamp], _previous )

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
tarunsingla
Solution Sage
Solution Sage

You would have to use the EARLIER function to get the previous timestamp.

 

See this file for the reference: EARLIER Example

 

Hope this helps.

 

Regards,

Tarun

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.