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