# Calculating standard deviation of the calculated measure



## res.marcus (Oct 22, 2012)

Hi,

I´m trying to calculate the standard deviation of a calculated measure per variable.

I have a table like this:


DateSum of percentage in variable 1Sum of percentage in variable 2Sum of percentage in variable 320123083,0309414555,9727287892,2495992120123173,8448964751,9434139289,3782092620123282,1207347853,0219394685,3999325220123384,6875947248,8820623690,6768516920123482,7289816453,4993219490,9544911120123583,1678031561,9630392590,5306501420123683,022243650,0718819687,6474328220123787,8013821958,3082870887,856580720123869,6747889148,1774432789,6683571720123977,2310620848,5792640594,6437230320124075,8374342552,948909392,0052407820124198,6051999261,0706947188,33355519

<colgroup><col><col span="3"></colgroup><tbody>

</tbody>
And I would like to calculate the standard deviation of these values per vaiable. The requested result for variable 1 in this example is 7,067208565.

When I try to do standard deviation calculations I get one unique value for each week - see below.



DateStandard deviation in variable 1Standard deviation in variable 2Standard deviation in variable 32012309,3165252347,3521194928,0575472472012319,2075683118,42978240510,984182042012328,7504279617,5138741598,5093572092012337,4546981476,3400211679,5574556062012349,1831967677,645692518,43422908320123510,595978310,60208199,6220056732012368,3574640595,6136026748,5174302142012378,2337849736,4768031868,009560142012387,0563057716,84206796410,060659652012397,388074396,3086751598,9384624812012407,9404199737,76629740511,2585924820124110,605194288,0162904047,702836064

<colgroup><col><col span="3"></colgroup><tbody>

</tbody>

But what I really want is one value that I can use to identify large deviations in the calculated measure.


Does anyone know how to get it right?

Best,
Marcus


----------



## NickyvV (Oct 22, 2012)

Hi Marcus,

how are you calculating those SD's? Is it a formula / measure over the the other measures you have in place?
Could you maybe get rid of the Date column? Is it in the row labels? The by deleting it there you would come up with the total SD per variable I believe?

HTH


----------



## res.marcus (Oct 22, 2012)

Hi,

Thank you for your quick reply.

I´m currently calculating the values with the DAX standard deviation formula for a population. I cannot really get rid of the date column since i need it to display my other measure - also, even if get rid of it I don't get the results I´d expect - it seems to me that the standard deviation calculations are made on the underlying data (?). *What I´m really after is a way to do calculations on the values visible in the table.
*
Best,
Marcus


----------



## res.marcus (Oct 23, 2012)

Hi,

Could the SUMMARIZE function be used in combination with STDEV.P to generate the result I want?

Seems it should work, but I recieve the error message "Calculation error in measure 'Query'[004af71a-5719-4971-b309-0df239be4dcc]: The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value." when I try creating the measure: =SUMMARIZE(Query; Query[Account]; Query[Country]; Query[Industry]; Query[QuestionID]; Query[Question]; "grouped dev"; STDEV.P([Percentage]))

What am I doing wrong?

Best,
Marcus


----------



## res.marcus (Oct 23, 2012)

Hi,

I just wanted to tell you that I solved my problem using =STDEVX.P(summarize(query; query[date]; query[segment]; query[account]; query[country]; query [industry]; query[questionid]; query[question]; "sum percent"; Sum(query[percentage])); [sum percent])

That is creating a table of the summarized values that I calculate the standard deviations for.

Many thanks to the DAX studio developers - the add-in really helped me understand what was going on!

Best,
Marcus


----------



## MSchley (Jun 28, 2014)

I have a similar problem to the question above.  i want to calculate the standard deviation of the most recent 12 previous QUARTERLY amounts given monthly data.  My approach was to use an inner calculate along with the datesqtd function to first calculate the quarterly subtotals.  Then I think I would use an outer calculate to filter to the last 12 values and calculate the standard deviation.  My formula doesn't work.  I wonder if someone can look it over and tell me what it is doing wrong.  It looks like this:

CALCULATE (
    STDEVX.P (
        values(lnl[yyyymm]),
        CALCULATE (
            SUM ( lnl[amount] ),
            DATESQTD ( lnl[date] )
        )
    ),
    DATESINPERIOD (
        lnl[date],
        ENDOFQUARTER ( lnl[date] ),
        -12,
        QUARTER
    ),
    LNL[QtrMnth] <= 3
)

If  I use the summarize function, is there still a way to respect the filters that get applied externally from the pivot table?


----------



## scottsen (Jun 29, 2014)

Looking just at this part, it reads kinda weird to me:

STDEVX.P (
values(lnl[yyyymm]),
CALCULATE (
SUM ( lnl[amount] ),
DATESQTD ( lnl[date] )
)

It seems unlikely you mean QTD here, as that will give you a standard deviation over a weird series of running totals.  Jan, Jan+Feb, Jan+Feb+Mar, Apr, Apr+May, ...

That will be a lot of deviation 

You need to somehow pass a table of QUARTERLY values, so... probably... ?

CALCULATE(STDDEVX.P(VALUES(Calendar[QuarterNum]), [Total Amount]))


----------



## MSchley (Jun 29, 2014)

I don't really follow what the first parameter of the standard deviation calculation is supposed to do.  I tried changing the values in "YYYYMM" to actually be YYYYQQ, where the last two digits are the number of the quarter (01-04).  That doesn't seem to work either.  I also tried a simple formula like this:

=calculate(stdevx.p(values(lnl[YYYYQQ]),sum(lnl[amount])),all(lnl[date]))

and it didn't work either.  I'm not sure what happens if I have more than one record with the same YYYYQQ.  Does the standard deviation formula aggregate based on the first parameter?  My goal is to determine the total for each quarter, and then take the standard deviation of those amounts.  So just (Jan + Feb + Mar), (Apr + May + Jun), (Jul + Aug + Sept), and (Oct + Nov + Dec) for each year.  Do I really need to use the summarize function to limit myself to just these quarterly totals?


----------



## MSchley (Jun 29, 2014)

I think I have something working.  I found a good article on DAX grouping that explains the VALUES and SUMMARIZE functions.

calculate(
              stdevx.p(
                           values(table[YYYYQQ]),
                           calculate(sum(table[amount]))
              ),
              DATESINPERIOD (
table[date],
                                       ENDOFQUARTER ( table[date] ),
                                       -12,
                                       QUARTER
              )
)


----------

