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

喜你入骨 提交于 2020-02-04 03:34:45

问题


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.



来源:https://stackoverflow.com/questions/42605377/calculating-the-standard-deviation-from-columns-of-values-and-frequencies-in-pow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!