Forum Discussion

bartvana's avatar
bartvana
Iron Contributor
Sep 13, 2021
Solved

DAX: how to use measures instead of calculated columns?

Hello, In DAX I need to calculate some statistics on the following kind of data (see also attached workbook). I can get it done using calculated columns and ALLEXCEPT to filter by Product, but I hav...
  • SergeiBaklan's avatar
    Sep 14, 2021

    bartvana 

    You calculate average of average here. The pattern for such calculation is using of AVERAGEX / SUMMARIZE.

    Measures could be as

    Avr Of Mean Measure:=VAR sumCat=
        SUMMARIZE (
            tFacts,
            tFacts[Category],
            tFacts[Product],
            "sumQtyOnMonth", SUMX ( tFacts, tFacts[AvgQty] * tFacts[Month] ),
            "sumQty",    SUM ( tFacts[AvgQty] )
        )
    VAR Result=
        AVERAGEX ( 
           sumCat,
           DIVIDE ( [sumQtyOnMonth], [sumQty] )
        )
    RETURN Result
    

    and

    Avr Of StDev Measure:=AVERAGEX (
        SUMMARIZE (
            tFacts,
            tFacts[Category],
            tFacts[Product],
            "variance",
                SUMX (
                    tFacts,
                    (
                         tFacts[AvgQty]
                            - CALCULATE ( [Avr Of Mean Measure], ALLEXCEPT ( tFacts, tFacts[Product] ) )
                    ) ^ 2
               )
                   / SUM ( tFacts[AvgQty] )
         ),
         SQRT ( [variance] )
    )
    

    Compare to current measures result is

Resources