Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Summarizing within a VAR expression

Hi all,

I'm struggling to find the answer, so hopefully someone here can help me. I feel like it's an easy fix within DAX for a trained eye. 😉

This is an example set I had.

StoreMatrixcodeAverage RevenueMatrixcode average
1A108
2B88
3A68


I used this formula to get the column 'Matrixcode average' in the table above:

Matrixcode average =
VAR __MatrixCode = MAX('Table'[Matrixcode])
VAR __Table = FILTER(ALL('Table'),[Matrixcode] = __MatrixCode)
VAR __Result = AVERAGEX(__Table, [Average Revenue)
RETURN
__Result

This worked well!

In a new (but similar) set, with an extra column (Productcategory), this new column causes that the store column is now repeated.
StoreMatrixcodeProductcategoryAverage RevenueMatrixcode average
1AMilk817,2
1AEggs1217,2
2BBread13,5
2BMilk63,5
3ACheese2017,2
3AMilk1617,2
3ABread3017,2

 

The issue is that the formula I have averages every single row value.
Example: For stores with matrixcode value A, it add: (8 +12 + 20 + 16 + 30)/5 = 17,2.
What I now want to formula to do is: sum each stores total revenue, and make an average of it.
So: Store 1 = 20, store 3 = 66. The average should get to (20 + 66) / 2 = 43.

How do I alter the formula below to make it so? Is that even possible within this formula?

Matrixcode average =
VAR __MatrixCode = MAX('Table'[Matrixcode])
VAR __Table = FILTER(ALL('Table'),[Matrixcode] = __MatrixCode)
VAR __Result = AVERAGEX(__Table, [Average Revenue)
RETURN
__Result

Many thanks for anyone still with me at this point. Any help is greatly appreciated 🙂
Thanks, 

Frans
1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@Anonymous 

Measure = 
VAR numerator =
    CALCULATE (
        SUMX (
            ALL ( 'table'[Store] ),
            CALCULATE (
                SUM ( 'table'[Average Revenue] ),
                ALLEXCEPT ( 'table', 'table'[Store], 'table'[Matrixcode] )
            )
        )
    )
VAR denominator =
    CALCULATE (
        DISTINCTCOUNT ( 'table'[Store] ),
        ALLEXCEPT ( 'table', 'table'[Matrixcode] )
    )
RETURN
    DIVIDE ( numerator, denominator )
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

4 REPLIES 4
tamerj1
Super User
Super User

Hi @Anonymous 

please try

Matrixcode average =
AVERAGEX (
CALCULATETABLE (
'Table',
ALLEXCEPT ( 'Table', 'Table'[Store], 'Table'[Matrixcode] )
),
[Average Revenue]
)

Anonymous
Not applicable

Thank you for replying!
This unfortunately did not succeed, it resulted in similar results.

smpa01
Super User
Super User

@Anonymous 

Measure = 
VAR numerator =
    CALCULATE (
        SUMX (
            ALL ( 'table'[Store] ),
            CALCULATE (
                SUM ( 'table'[Average Revenue] ),
                ALLEXCEPT ( 'table', 'table'[Store], 'table'[Matrixcode] )
            )
        )
    )
VAR denominator =
    CALCULATE (
        DISTINCTCOUNT ( 'table'[Store] ),
        ALLEXCEPT ( 'table', 'table'[Matrixcode] )
    )
RETURN
    DIVIDE ( numerator, denominator )
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

Perfect, this worked! Thanks for the reply. I will study the formula to fully understand what you've written 🙂

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.