Difference of most recent values

  • Difference of most recent values

    Posted by DSC Communities on January 22, 2021 at 4:27 pm
    • Martin Pogacnik

      Member

      January 22, 2021 at 4:27 PM

      Hi guys,

      I cannot get this to work.Ā  My table has a column DATE, a column PRICE, and a column GRADE.Ā  There are two GRADE types (text).Ā  On any single day, there can be a price for one or the other or both or none of the grades.Ā  I need a measure calculating the difference between the most recent prices between both grades on any single day.Ā  Any ideas?

      Thanks,

      Martin

      ——————————
      Martin Pogacnik
      ——————————

    • Nicolas MENDEZ

      Member

      January 23, 2021 at 7:07 AM

      Hi…

      If everithing is clear in my mind..; here is my contrib…

      Price diff =
      VAR __LastPrice =
          CALCULATE (
              LASTNONBLANK (
                  Prices[Price],
                  Prices[Price]
              ),
              FILTER (
                  ALL ( Prices ),
                  Prices[Grade]
                      <> SELECTEDVALUE ( Prices[Grade] )
              ),
              FILTER (
                  ALL ( Prices ),
                  Prices[Date]
                      <= SELECTEDVALUE ( Prices[Date] )
              )
          )
      VAR __Result =
          SELECTEDVALUE ( Prices[Price] ) - __LastPrice
      RETURN
          __Result
      ?

      I built a table called ‘Prices’ as following :

      I hope this helps…

      Feedback welcome

      ——————————
      Nicolas MENDEZ
      Data & BI consultant
      Becom Consulting
      ——————————
      ——————————————-

    • Martin Pogacnik

      Member

      January 23, 2021 at 11:26 AM

      Hi Nicolas,

      thank you so much for taking the time!Ā  I am a total novice here, so really appreciate it.Ā  The result is as it should be, except my description was incomplete:Ā  how would the formula change, if Price diff were always calculated only in one direction, i.e. Grade A – Grade B, such that, for instance, the result in the third row (22-Jan, 64, Grade B) would show 5 rather than -5 as Price Diff?

      Merci,

      Martin

      ——————————
      Martin Pogacnik
      ——————————
      ——————————————-

    • Ka Ly

      Member

      January 23, 2021 at 1:16 PM

      Martin, if you are just interested in the price difference then use the DAX function:

      DAX

      ABS(<number>)

      To get your value to always show in a positive number regardless of the result.Ā  A-B or B-A
      https://docs.microsoft.com/en-us/dax/abs-function-dax
      In this instanceĀ  after the return, you type
      ABS(_Result)

      ——————————
      Ka Ly
      Clinical Informatics Program Manager
      ——————————
      ——————————————-

    • Martin Pogacnik

      Member

      January 23, 2021 at 3:41 PM

      Hi Kay Li,

      thank you for this.Ā  Actually, I was not looking for absolute difference, but one-directional difference only, and with an added complication that there are more than two grades.

      The approach suggested by Nicolas, modified slightly with an additional IF statement, worked here as well.

      Price diff =

      VAR __LastPriceA =

      CALCULATE(

      Ā Ā Ā  LASTNONBLANK(Prices[Price],Prices[Price]),

      Ā Ā Ā  FILTER(ALL(Prices), Prices[Grade] = “Grade A”),

      Ā Ā Ā  FILTER(ALL(Prices),Prices[Date] <= SELECTEDVALUE(Prices[Date]))

      Ā Ā Ā  )

      Ā Ā Ā  VAR __LastPriceB =

      Ā Ā Ā Ā CALCULATE(

      Ā Ā Ā  LASTNONBLANK(Prices[Price],Prices[Price]),

      Ā Ā Ā  FILTER(ALL(Prices), Prices[Grade] = “Grade B”),

      Ā Ā Ā  FILTER(ALL(Prices),Prices[Date] <= SELECTEDVALUE(Prices[Date]))

      Ā Ā Ā  )

      Ā Ā Ā  VAR __Result =

      Ā Ā Ā Ā if(

      Ā Ā Ā Ā Ā Ā Ā  SELECTEDVALUE(Prices[Grade]) = “Grade A”,

      Ā Ā Ā Ā Ā Ā Ā  SELECTEDVALUE(Prices[Price]) – __LastPriceB,

      Ā Ā Ā Ā Ā Ā Ā  if(

      Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  SELECTEDVALUE(Prices[Grade]) = “Grade B”,

      Ā Ā Ā Ā Ā Ā Ā  __LastPriceA – SELECTEDVALUE(Prices[Price]),

      Ā Ā Ā Ā Ā Ā  __LastPriceA – __LastPriceB )

      Ā Ā Ā  )

      Ā Ā Ā  RETURN __Result

      You guys are great, I learn so much from your posts.Ā  Thanks for helping me out on this!

      Best,

      Martin

      ——————————
      Martin Pogacnik
      ——————————
      ——————————————-

    DSC Communities replied 4 years, 7 months ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

The discussion ‘Difference of most recent values’ is closed to new replies.

Start of Discussion
0 of 0 replies June 2018
Now

Welcome to our new site!

Here you will find a wealth of information created for peopleĀ  that are on a mission to redefine business models with cloud techinologies, AI, automation, low code / no code applications, data, security & more to compete in the Acceleration Economy!