cancel
Showing results for
Did you mean:
Post Partisan

## 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,

1 ACCEPTED SOLUTION
Community 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.

4 REPLIES 4
Community Support

Hi @rsbin ,

``````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.

Post Partisan

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,

Community 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.

Post Partisan

Good Morning @Icey

Yes, the edit works.  I finally am able to get the Std. Dev values for each Index.   Thank you very much for the assistance.

Unfortunately, still having some issues having it display properly in my line charts.   I have to figure out why it is behaving like that.

But thanks again, it is a relief to finally solve the first part of this.

Kind Regards and all the best,

Announcements

#### 2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

#### Microsoft named a Leader in The Forrester Wave

Microsoft received the highest score of any vendor in both the strategy and current offering categories.

#### Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Top Solution Authors
Top Kudoed Authors