Forum Discussion
Moving Average -> Standard Deviation
Good Morning,
Reaching out to the DAX experts out there. I am trying to calculate the Standard Deviation of a Measure based on a 3 Day Moving Average (i.e. 3DayStdDev_EquipIDRatio). My sample data looks like this:
| Date | GSIndex | EquipmentIDCount | TotalVisits | EquipmentIDRatio |
| 8/1/2019 | 1 | 3079 | 13555 | 0.227 |
| 8/2/2019 | 2 | 2931 | 13524 | 0.217 |
| 8/3/2019 | 3 | 1037 | 4997 | 0.208 |
| 8/4/2019 | 4 | 637 | 3326 | 0.192 |
| 8/5/2019 | 5 | 2326 | 8619 | 0.270 |
| 8/6/2019 | 6 | 2908 | 14599 | 0.199 |
| 8/7/2019 | 7 | 2994 | 14383 | 0.208 |
| Date | GSIndex | 3DayEquipID | 3DayVisits | 3DayStdDev_EquipIDRatio | Std. Dev Excel |
| 8/1/2019 | 1 | ||||
| 8/2/2019 | 2 | ||||
| 8/3/2019 | 3 | 7047 | 32076 | 0 | 0.0080 |
| 8/4/2019 | 4 | 4605 | 21847 | 0 | 0.0104 |
| 8/5/2019 | 5 | 4000 | 16942 | 0 | 0.0338 |
| 8/6/2019 | 6 | 5871 | 26544 | 0 | 0.0353 |
| 8/7/2019 | 7 | 8228 | 37601 | 0 | 0.0314 |
EquipIDCount , TotalVisits and Equipment IDRatio are all Measures.
EquipIDRatio is a measure - simply (EquipIDCount / TotalVisits).
3DayVisits and 3DayEquipID are moving average calculations wherein I use the Index to Sumx the 3 values (Current Day +2 prior days).
I am able to calculate Standard Deviation on my database as a whole using this DAX provided by Greg_Deckler and az38
StdDev_EquipIDRatio =
VAR _table = SUMMARIZE(FILTER(GeneralStatistics, GeneralStatistics[TotalVisits] <> 0),[Date],"_EquipIDRatio",[EquipmentIDRatio])
RETURN STDEVX.P(_table,[_EquipIDRatio])
The Standard Deviation in the last column was calculated in Excel. This is the number I am trying to replicate in DAX. This number is the Standard Deviation of the EquipmentIDRatio of the current day + the previous 2 days.
So on day 3, I need to take the Ratio values from Day 1 - 3
On day 4, I need to take the Ratio values from Day 2 - 4. And so on.....
Since EquipIDRatio is a measure, to calculate Std Dev., I need to construct a virtual table as I have done above, but containing only the 3 relevant values for each Date or Index. I have hit a wall and unable to figure out the correct way to do this in DAX.
Hoping one of you can point me in the right direction.
Thanks in advance and best regards,
Hi rsbin ,
How about using ALLSELECTED in the expression?
3DayStdDev_EquipIDRatio = VAR CurrentIndex = MAX ( GeneralStatistics[GSIndex] ) VAR PreviousIndex = CurrentIndex - 2 VAR _table = SUMMARIZE ( FILTER ( ALLSELECTED (GeneralStatistics ), ------------------edited GeneralStatistics[TotalVisits] <> 0 && GeneralStatistics[GSIndex] >= PreviousIndex && GeneralStatistics[GSIndex] <= CurrentIndex ), [Date], "_EquipIDRatio", [EquipmentIDRatio] ) RETURN STDEVX.P ( _table, [_EquipIDRatio] )Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
4 Replies
- IceyCommunity Support
Hi rsbin ,
How about this?
3DayStdDev_EquipIDRatio = VAR CurrentIndex = MAX ( GeneralStatistics[GSIndex] ) VAR PreviousIndex = CurrentIndex - 2 VAR _table = SUMMARIZE ( FILTER ( GeneralStatistics, GeneralStatistics[TotalVisits] <> 0 && GeneralStatistics[GSIndex] >= PreviousIndex && GeneralStatistics[GSIndex] <= CurrentIndex ), [Date], "_EquipIDRatio", [EquipmentIDRatio] ) RETURN STDEVX.P ( _table, [_EquipIDRatio] )Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- rsbinCommunity Champion
Good Morning Icey
Thanks much for the reply. I have attempted that code as as Solution, but it returns only an answer for the last date.
The ".03142" is the correct answer for Index = 7. I too thought it would cycle through and provide an answer for each Index, but as you can from the above, it Returns just a single value.
I got up this morning thinking "CALCULATETABLE" might be the way to proceed. Really appreciate you taking the time to respond. If you have further suggestions I would be grateful to hear them. Kudos for the attempt.
Kind Regards,
- IceyCommunity Support
Hi rsbin ,
How about using ALLSELECTED in the expression?
3DayStdDev_EquipIDRatio = VAR CurrentIndex = MAX ( GeneralStatistics[GSIndex] ) VAR PreviousIndex = CurrentIndex - 2 VAR _table = SUMMARIZE ( FILTER ( ALLSELECTED (GeneralStatistics ), ------------------edited GeneralStatistics[TotalVisits] <> 0 && GeneralStatistics[GSIndex] >= PreviousIndex && GeneralStatistics[GSIndex] <= CurrentIndex ), [Date], "_EquipIDRatio", [EquipmentIDRatio] ) RETURN STDEVX.P ( _table, [_EquipIDRatio] )Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.