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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors