Forum Discussion
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 Period | ID | Satisfaction |
| Jan-20 | 1 | 4 |
| Jan-20 | 2 | 4 |
| Jan-20 | 3 | 3 |
| Jan-20 | 4 | 4 |
| Jan-20 | 5 | 2 |
| Jul-20 | 1 | 3 |
| Jul-20 | 2 | 3 |
| Jul-20 | 3 | 1 |
| Jul-20 | 4 | 1 |
| Jan-21 | 1 | 2 |
| Jan-21 | 2 | 1 |
| Jan-21 | 3 | 2 |
| Jul-21 | 1 | 4 |
| Jul-21 | 2 | 3 |
| Jul-21 | 3 | 5 |
| Jul-21 | 4 | 3 |
| Jan-22 | 1 | 1 |
| Jan-22 | 2 | 5 |
| Jan-22 | 3 | 5 |
| Jan-22 | 4 | 4 |
| Jan-22 | 5 | 2 |
Result
| Survey Period | Current | Previous | Difference |
| Jan-20 | 3.4 | null | null |
| Jul-20 | 3.5 | 3.4 | 0.1 |
| Jan-21 | 3 | 3.5 | -0.5 |
| Jul-21 | 3.25 | 3 | 0.25 |
| Jan-22 | 4 | 3.25 | 0.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
- amitchandakSuper User
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-msftCommunity 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