Forum Discussion
Matrix sub-total calculation?
- 5 years ago
Final PBIX File here: https://drive.google.com/file/d/12_k_tO8BdrXV73Lbho_whabGP-jOlasP/view?usp=sharing
Code to create the weighted average measure:
WAVG Measure = VAR DESIRED_ROWS = FILTER( 'Table', 'Table'[Column1] > 0 && 'Table'[Attribute (from a PowerQuery unpivot] = "attribute1 selected" || 'Table'[Attribute (from a PowerQuery unpivot] = "attribute2 selected" || 'Table'[Attribute (from a PowerQuery unpivot] = "attribute3 selected" || ) RETURN DIVIDE( SUMX ( DESIRED_ROWS, 'Table'[Denominator Column] * 'Table'[Value] ), SUMX ( DESIRED_ROWS, 'Table'[Denominator Column] ) )Code to generate Matrix values from a weighted average measure:
_A = // Note - below one can use any of the *X DAX functions, such as SUMX, AVERAGEX, MEDIANX, STDEVX.S, etc. VAR OUTPUT = AVERAGEX( SUMMARIZE ( ALLSELECTED('Table'), 'Table'[Row Column] ) , [WAVG Measure] ) RETURN OUTPUTCode to summarize the Matrix value [measure]
_A Summarized = // Choose AVERAGEX or SUMX below depending on one's needs VAR OUTPUT_CELL = AVERAGEX( SUMMARIZE('Table','Table'[Matrix Row Column]) , [WAVG measure] ) RETURN OUTPUT_CELL
Thank you so much for your response and super fantastic simple-to-understand explanation. I've had a hard time distinguishing between [column] (as opposed to [measure] or [table] and "column" as in of a pivtotable.
In this case, I'm trying to produce a measure that provides the average of the pivottable column for each Year of Sale where errors/NaNs are treated as BLANK(). That measure should also sub-total to the average of the rows when across rows, and should be the average of each of the individual cells as a Grand Total.
In the past few days, I've figured out that this formula works:
Average of pivoted column
VAR OUTPUT =
AVERAGEX(
ADDCOLUMNS (
SUMMARIZE (
ALLSELECTED ('Table'[Table column]),
'Table'[Table column],
"SUM", SUM ( 'Table'[Table row] )
),
"Percent", CALCULATE ( SUM ( 'Table'[Table row] ) )
),
[measure of the pivot column value]
)
RETURN
OUTPUT
But in some of my pivottable columns, where I use IFERROR({calc}, BLANK() ) the above-formula returns blank in all instances.
Is your error caused by a division by zero? Try to already apply the BLANK() substitution there.
- mrothschild5 years agoContinued Contributor
Some progress here. New PBIX file: https://drive.google.com/file/d/1pG3ua4BOWyIJhhh4dOwG9ac8UPWqUsYo/view?usp=sharing
This is a before screenshot
By changing the first above STDVX [measure] to the following by adding ALLNOBLANKROW first thing after summarize
VAR DEPRECIATION_VOL_OBSERVED = STDEVX.S( SUMMARIZE ( ALLNOBLANKROW('Helivalues Transaction History'), 'Helivalues Transaction History'[Sale Year] ) , [Average Annualized Price change (weighted by Model Year Units)] )I get **a** sub-total rather than NaN. What that subtotal represents isn't entirely apparent, and isn't dynamic across the pivoted columns. It's summarized as 5.76% for each column and the grand total, rather than the average of what I perceive to be the available data points.
The intended output is for the {A01} pivot tabled-column to produce the average of the pivot table column immediate to its left. Based on what's shown {A01} should = 5.38% for 2016, 5.73% for 2017, 11.13% for 2018, 6.14% for 2019, and 6.14% for 2020.
- lbendlin5 years agoSuper User
"produce the average of the pivot table column immediate to its left"
Your measure has no idea what that means. "right" and "left" are meaningless unless you can help with an index column, or a guaranteed sort order.
- mrothschild5 years agoContinued Contributor
apologies. My language was colloquial and imprecise. For A01, I want the average of A for each of 2016, 2017, 2018, etc. Visually on the Matrix it's physically oriented to the left, but I want it associated with sale year.