Calculate total of averages

Hi there,

I have a dataset starting from 2012 until now, following this pattern :


For each month of each year I do the following :
Budget January 2013 = Production January 2012
Budget January 2014 = (Production January 2012 + Production January 2013) / 2
Budget January 2015 = (Production January 2012 + Production January 2013 + Production January 2014) / 3

and I need the total to be the sum of the monthly averages i.e. :
average of January + average of Febuary + …

Production Budget =
VAR _min = 2012
VAR monthlyBudget= CALCULATE(AVERAGEX(‘Table’,[Production]]),FILTER(ALL(Dates),(Dates[Year]>=_min && Dates[Year]<MAX(Dates[Year])) && Dates[MonthName]=MAX(Dates[MonthName])))

The average for each month works fine, however the total I get is wrong and I don’t know what to change in the formula to fix it.
Can somebody help me ?

Thanks !

Hello @DawnLs,

Thank you for posting your qury onto the Forum.

Well it’s always a tough task to carry out a formula in the absence of a PBIX file.

But you can try out the formula given below -

Production Budget =
VAR _min = 2012

VAR monthlyBudget = CALCULATE(
                      AVERAGEX( ‘Table’,[Production] ),
                         FILTER( ALL( Dates ) , 
                         ( Dates[Year] >= _min && Dates[Year] < MAX( Dates[Year] ) ) && 
                            Dates[MonthName] = MAX( Dates[MonthName] ) ) )

IF( HASONEVALUE( Dates[MonthName] ) ,
       SUMMARIZE( Dates[MonthName] , "Averages" , monthlyBudget ) , [Averages] ) )

Hoping this formula works out for you. :slightly_smiling_face:

Thanks & Warm Regards,

Hi @Harsh,

Thanks a lot for your answer.
I tried the formula you suggested but didn’t get the result I expected for the total.

Here is the pbix file ( I just kept 2 years of data to simply). Hope it will help.
Thanks !

avg-problem.pbix (139.4 KB)

Hello @DawnLs,

This happened due to the variables which were set inside the Budget Production DAX formula.

So this will be calculated in two different parts -

Part 1: Where you’ll just calculate your Averages and the formula will be as follows -

Averages = 
VAR _min = 2018

VAR monthlyBudget = CALCULATE(
                        AVERAGEX('Production' , Production[Production] ) , 
                            FILTER( ALL( Dates ) ,  Dates[Year] >= _min && Dates[Year] <= MAX(Dates[Year] )
                            && Dates[MonthName] = MAX( Dates[MonthName] ) ) )

IF( HASONEVALUE( Dates[MonthName] ),
    BLANK() )    

Part 2: Budget Production (Average Total)

 Budget Production (Average Total) = 
    SUMMARIZE( Dates , Dates[MonthName] , "Averages" , [Averages] ) , [Averages] )

By this way you’ll achieve the desired result that you’re looking for.

Sam has explained many a times where to use our variables wisely or else our Totals can differ widely.

I’m attaching the screenshot as well as the PBIX file of my working for the reference.

Hoping you find this useful and helps you in your analysis.

Thanks & Warm Regards,

avg-problem.pbix (136.8 KB)

1 Like

Fixed it.
avg-problem.pbix (130.9 KB)

Production Budget =
    ALLSELECTED ( Dates[MonthName] ),
        AVERAGEX ( 'Production', [Production] ),
        SAMEPERIODLASTYEAR ( Dates[Date] )

Thanks a lot.
Indeed it was helpful !

Thanks for your answer but this solution only works when there is only one year of historical, and in this case I have to take into account several years…

Hello @DawnLs,

You’re Welcome. :slightly_smiling_face:

I’m glad I was able to help you.

If it’s OK to you I would really like to recommend you one tip which may prove helpful to you that is always one should always go for the “Measure Branching” technique first and once that technique provides you the desired results then you can combine those formulas in the form of “Variables” and put it in one formula.

Because by following the “Measure Branching” one can analyze the results and the steps about where they went wrong or have faltered and if we directly go for “Variables” we may get lost and may also find it difficult to analyze where we went wrong.

I’ve learned this technique from Sam where he has always recommended to built - up or start with small measures so that we know what process we are following and whether that process will provide the fruitful results or not.

Hoping you will find this useful and helps you in your future analysis. Best of luck. :+1:

Thanks & Warm Regards,