Forum Discussion
bartvana
Sep 13, 2021Iron Contributor
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...
- Sep 14, 2021
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
SergeiBaklan
Sep 14, 2021Diamond Contributor
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
bartvana
Sep 15, 2021Iron Contributor
Wow, fabulous! Thank you!
- SergeiBaklanSep 18, 2021Diamond Contributor
bartvana , you are welcome