Forum Discussion

sonm10's avatar
sonm10
Icon for Helper I rankHelper I
6 years ago
Solved

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.

 

  • 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

6 Replies

    • sonm10's avatar
      sonm10
      Icon for Helper I rankHelper I

      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.

       

      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.

       

       

       

       

      • Greg_Deckler's avatar
        Greg_Deckler
        Icon for Community Champion rankCommunity Champion

        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
  • 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)
    • sonm10's avatar
      sonm10
      Icon for Helper I rankHelper I

      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

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft 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