Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Subtract previous average from current average

Hi Dax community,

 

I would like to calculate the difference in average satisfaction between the current and previous period but I could not figure out a DAX.  I tried indexing and using the earlier function to no avail.  I kept getting an error message with the earlier function. Is there a workaround?

 

Example Data

Survey PeriodIDSatisfaction
Jan-2014
Jan-2024
Jan-2033
Jan-2044
Jan-2052
Jul-2013
Jul-2023
Jul-2031
Jul-2041
Jan-2112
Jan-2121
Jan-2132
Jul-2114
Jul-2123
Jul-2135
Jul-2143
Jan-2211
Jan-2225
Jan-2235
Jan-2244
Jan-2252

 

Result

Survey PeriodCurrent PreviousDifference
Jan-203.4nullnull
Jul-203.53.40.1
Jan-2133.5-0.5
Jul-213.2530.25
Jan-2243.250.75
  • Hi Anonymous,

     

    You may try this solution.

    1 Create a Calculated column

    Date =
    VAR Month_ =
        SWITCH (
            LEFT ( 'Table'[Survey Period], 3 ),
            "Jan", 1,
            "Feb", 2,
            "Mar", 3,
            "Apr", 4,
            "May", 5,
            "Jun", 6,
            "Jul", 7,
            "Aug", 8,
            "Sep", 9,
            "Oct", 10,
            "Nov", 11,
            "Dec", 12
        )
    VAR Year_ =
        2000 + RIGHT ( 'Table'[Survey Period], 2 )
    RETURN
        DATE ( Year_, Month_, 1 )

     

    2 Create the following Measures

    Current = CALCULATE(AVERAGE('Table'[Satisfaction]),FILTER(ALL('Table'),'Table'[Date]<=MAX('Table'[Date])))

     

    Previous = CALCULATE([Current],FILTER(ALL('Table'),'Table'[Date]<MAX('Table'[Date])))

     

    Difference = IF(ISBLANK([Previous]),BLANK(),[Current]-[Previous])

     

    Then, the result looks like this.

     

    Also, attach the pbix file as reference. Hope it helps.

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let me know. Thanks a lot!

     

    Best Regards,

    Community Support Team _ Caiyun

2 Replies

  • Anonymous , Create a new tbale with distinct Survey and period and then try meausre like these

     

    This Period = CALCULATE(sum('Table'[Qty]), FILTER(ALL(Period),Period[Period]=max(Period[Period]) && period[Survey] =max(period[Survey] ) ))
    Last Period = CALCULATE(sum('Table'[Qty]), FILTER(ALL(Period),Period[Period]=max(Period[Period])-1 && period[Survey] =max(period[Survey] ) ))

     

     

    period table = summarize(Table, Table[Survey], Table[Period])

  • v-cazheng-msft's avatar
    v-cazheng-msft
    Community Support

    Hi Anonymous,

     

    You may try this solution.

    1 Create a Calculated column

    Date =
    VAR Month_ =
        SWITCH (
            LEFT ( 'Table'[Survey Period], 3 ),
            "Jan", 1,
            "Feb", 2,
            "Mar", 3,
            "Apr", 4,
            "May", 5,
            "Jun", 6,
            "Jul", 7,
            "Aug", 8,
            "Sep", 9,
            "Oct", 10,
            "Nov", 11,
            "Dec", 12
        )
    VAR Year_ =
        2000 + RIGHT ( 'Table'[Survey Period], 2 )
    RETURN
        DATE ( Year_, Month_, 1 )

     

    2 Create the following Measures

    Current = CALCULATE(AVERAGE('Table'[Satisfaction]),FILTER(ALL('Table'),'Table'[Date]<=MAX('Table'[Date])))

     

    Previous = CALCULATE([Current],FILTER(ALL('Table'),'Table'[Date]<MAX('Table'[Date])))

     

    Difference = IF(ISBLANK([Previous]),BLANK(),[Current]-[Previous])

     

    Then, the result looks like this.

     

    Also, attach the pbix file as reference. Hope it helps.

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let me know. Thanks a lot!

     

    Best Regards,

    Community Support Team _ Caiyun