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
Metricbits
Frequent Visitor

Sum of averages if no filter

Hello,

 

I have a table of data that looks like this

 

HouseResidentNumbersMTDPeriod
GR930/6/2020
BR730/6/2020
AL7.830/6/2020
VS830/6/2020
RB5.530/6/2020
CR730/6/2020
CL7.530/6/2020
FL9.730/6/2020
EL8.730/6/2020
IR6.330/6/2020
CS1.930/6/2020
KL5.331/5/2020
GR931/5/2020
BR731/5/2020
AL831/5/2020
VS831/5/2020
RB431/5/2020
CR731/5/2020
CL831/5/2020
FL9.531/5/2020
EL931/5/2020
IR7.331/5/2020

 

I want to calculate the YTD average Number of residents. When house filter applied, it should be just the average number of residents for this house, however, when no house filter applied, I want Power Bi to calculate the sum of averages for all houses.

 

The formula I tried is:

 

YTD_No of Residents (MTD) =
VAR AvgMonth =
    CALCULATE (
        AVERAGEX ( Manual_Data, [No of Residents (MTD)] ),
        ALL ( 'Calendar' ),
        DATESYTD ( 'Calendar'[Date], "06-30" )
    )
VAR SumTable =
    SUMMARIZE (
        Manual_Data,
        Manual_Data[House],
        "SumTableAvg",
            CALCULATE (
                AVERAGE ( Manual_Data[ResidentNumbersMTD] ),
                ALL ( Manual_Data[Period] ),
                DATESYTD ( Manual_Data[Period], "30-6" )
            )
    )
RETURN
    IF (
        HASONEFILTER ( Houses[ShortName] ),
        AvgMonth,
        SUMX ( SumTable, [SumTableAvg] )
    )

 

 

It works ok when house filter is applied, however, when no house is selected, I get a total average for one month only (that is selected in another slicer).

 

Please help

1 ACCEPTED SOLUTION

 

[Your Measure] =
SUMX(
    SUMMARIZE(
        Manual_Data,
        Houses[ShortName]
    ),
    CALCULATE(
        AVERAGE( Manual_Data[ResidentNumbersMTD] ),
        // You might need to remove this ALLEXCEPT
        // if you want the measure to respond
        // to other filters.
        ALLEXCEPT( Houses[ShortName] ),
        DATESYTD(
            // Calendar must be the date table
            // marked as such in the model
            // for this to work correctly.
            'Calendar'[Date],
            "06-30"
        )            
    )
)

 

Please note that the above is written for a model where:

  1. Houses is a dimension. Each row is a different house.
  2. Calendar is a date dimension.
  3. Manual_Data is a fact table that should be hidden.

All relationships are standard Dim 1-one-way-* Fact. Slicing in a star schema as the above should be done only through dimensions. Then the above will work the way it's intended to.

View solution in original post

5 REPLIES 5
Metricbits
Frequent Visitor

Another perspective on the same issue,

Here is the table I have got bu using below formula. Line numbers are correct, but the total is wrong. It should be 83.31, not 88.46. Total must be a sum of lines

Screenshot_1.png

 

Formula I use is

 

xxx =
VAR one =
    SUMMARIZE (
        Manual_Data,
        Manual_Data[House],
        "SumTableAvg",
            CALCULATE (
                AVERAGE ( Manual_Data[ResidentNumbersMTD] ),
                ALL ( Manual_Data ),
                DATESYTD ( 'Calendar'[Date], "06-30" )
            )
    )
VAR two =
    SUMX ( one, [SumTableAvg] )
RETURN
    IF ( HASONEFILTER ( Houses[ShortName] ), two, SUMX ( one, [SumTableAvg] ) )

 

[Your Measure] =
SUMX(
    SUMMARIZE(
        Manual_Data,
        Houses[ShortName]
    ),
    CALCULATE(
        AVERAGE( Manual_Data[ResidentNumbersMTD] ),
        // You might need to remove this ALLEXCEPT
        // if you want the measure to respond
        // to other filters.
        ALLEXCEPT( Houses[ShortName] ),
        DATESYTD(
            // Calendar must be the date table
            // marked as such in the model
            // for this to work correctly.
            'Calendar'[Date],
            "06-30"
        )            
    )
)

 

Please note that the above is written for a model where:

  1. Houses is a dimension. Each row is a different house.
  2. Calendar is a date dimension.
  3. Manual_Data is a fact table that should be hidden.

All relationships are standard Dim 1-one-way-* Fact. Slicing in a star schema as the above should be done only through dimensions. Then the above will work the way it's intended to.

Thank you, this worked like magic

Greg_Deckler
Super User
Super User

@Metricbits This looks like a measure aggregation problem. See my blog article about that here: https://community.powerbi.com/t5/Community-Blog/Design-Pattern-Groups-and-Super-Groups/ba-p/138149

The pattern is:
MinScoreMeasure = MINX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
MaxScoreMeasure = MAXX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
AvgScoreMeasure = AVERAGEX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
etc.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Thanks for the reply, but this doesn't solve my problem, unfortunately 😞

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.