Calculating the standard deviation from columns of

2019-07-20 13:21发布

问题:

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.

回答1:

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

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

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:

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.