Forum Discussion

mrothschild's avatar
mrothschild
Continued Contributor
5 years ago
Solved

Matrix sub-total calculation?

Sample Excel data: https://docs.google.com/spreadsheets/d/1o55R8MKW263heweuKcZPIvEBgyBoC5hL/edit?usp=sharing&ouid=115111367427670973715&rtpof=true&sd=true   The attached file has a pivot table on i...
  • mrothschild's avatar
    mrothschild
    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
        OUTPUT
           

     

    Code 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