Calculating the standard deviation from columns of values and frequencies in Power BI

Roy Masrani picture Roy Masrani · Mar 5, 2017 · Viewed 9.3k times · Source

I am trying to calculate the standard deviation of a set of values in PowerBI and I am stuck. There are two columns in a table (days and count). This is a frequency distribution of a transportation lane. Days goes from 1 to 100, count is the number of shipments that took those number of days.

The formula to calculate the standard deviation of a frequency distribution is pretty straight forward: sqrt(sum(fx * (x - avgx)^2))/sum(fx)) But the Dax is giving me a massive headache. Any help would be much appreciated. Thanks.

Answer

Foxan Ng picture Foxan Ng · Mar 5, 2017

I took the example from the Standard deviation Wikipedia page as sample data.

wiki data

Converted to Power BI equivalent and fit your requirement as days and count:

power bi data

And the measure is created as follows, the tricky part is to make use of the SUMX function. I deliberately break down the intermediate steps with VAR to make it more clear.

st_dev = 
VAR x_sum = SUMX(Lane, Lane[Days] * Lane[Count])
VAR x_count = SUM(Lane[Count])
VAR mean = x_sum / x_count
VAR dev_sq_sum = SUMX(Lane, POWER(Lane[Days] - mean, 2) * Lane[Count])
RETURN SQRT(dev_sq_sum / x_count)

Result:

result

P.S. Power BI actually has some built-in functions for calculating standard deviation, e.g. STDEVX.P, but it's not that useful in this case. Feel free to check it out though.