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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
jbaucke
Regular Visitor

Finding Difference in Values Between Last Date and Second to Last Date

Using a data set with a structure like below, I am trying to determine if a value related to a certain field, in this case id, is either rising or falling. We are using the most recent value and the value prior to the last one to determine that. In the example below, the latest value is and the previous value is 0, so the change metric on this would equal 2, so the value would be defined as "rising". 

 

personidDate Value
101/14/2020 0
101/29/2020 1
102/13/2020 1
102/28/2020 2
103/14/2020 1
103/29/2020 0
104/13/2020 2
 
Here is what I have tried so far.
 
Rising Falling Stable =
VAR date_a = VALUES(facttable[date])
VAR person_id = VALUES(facttable[personId])
VAR date_rank = CALCULATE (RANK.EQ ( date_a, facttable[date], DESC ), FILTER ( ALL (facttable), facttable[personId] = person_id ))
 
VAR latest_value = CALCULATE(VALUES(facttable2[OverallValue]), FILTER(facttable, date_rank=1))
VAR previous_value = CALCULATE(VALUES(facttable2[OverallValue]), FILTER(facttable, date_rank=2))
 
VAR value_difference = latest_value-previous_value

RETURN
value_difference

This results in the following table being displayed:
personidDateValuedate_ranklatest_valueprevious_valueRising Falling Stable
101/14/202007   
101/29/202016   
102/13/202015   
102/28/202024   
103/14/202013   
103/29/202002 00
104/13/2020212 2

 

Do anyone know how I can get the previous and current metrics into the same row in this table so I can get a change since previous date metric to be showing in the Rising Falling Stable column? 

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Here is a measure expression you can use in a Table visual with the PersonID column to get your desired result.

 

Trend =
VAR latestdate =
    MAX ( 'Cases'[Date] )
VAR prevdate =
    CALCULATE ( MAX ( 'Cases'[Date] ), 'Cases'[Date] < latestdate )
VAR latestvalue =
    CALCULATE ( SUM ( 'Cases'[Value] ), 'Cases'[Date] = latestdate )
VAR prevvalue =
    CALCULATE ( SUM ( 'Cases'[Value] ), 'Cases'[Date] = prevdate )
RETURN
    SWITCH (
        TRUE (),
        latestvalue > prevvalue, "Rising",
        latestvalue < prevvalue, "Falling",
        "Stable"
    )

 

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


View solution in original post

1 REPLY 1
mahoneypat
Microsoft Employee
Microsoft Employee

Here is a measure expression you can use in a Table visual with the PersonID column to get your desired result.

 

Trend =
VAR latestdate =
    MAX ( 'Cases'[Date] )
VAR prevdate =
    CALCULATE ( MAX ( 'Cases'[Date] ), 'Cases'[Date] < latestdate )
VAR latestvalue =
    CALCULATE ( SUM ( 'Cases'[Value] ), 'Cases'[Date] = latestdate )
VAR prevvalue =
    CALCULATE ( SUM ( 'Cases'[Value] ), 'Cases'[Date] = prevdate )
RETURN
    SWITCH (
        TRUE (),
        latestvalue > prevvalue, "Rising",
        latestvalue < prevvalue, "Falling",
        "Stable"
    )

 

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


Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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