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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

How to exclude/Ignore Blanks in Moving average calculation

Hi,

 

I have 4 months moving average calculation

 

Moving Avg 4 months = AVERAGEX(
DATESINPERIOD( 'sheet'[All Date], MAX('sheet'[All Date]), -4,MONTH),
[Calc Measure])
 
where [Calc Measure] is the calculated measure
All Date = STARTOFMONTH('sheet'[Dates])
 
the current output is
All DateCalc MeasureMoving Avg 4 months
31/07/2016 0:001.9618551.961855
28/08/2016 0:001.6497991.805827
25/09/2016 0:001.2469261.619526
30/10/2016 0:001.125591.496042
27/11/2016 0:001.0635951.271477
25/12/2016 0:000.93021.091578
08/01/2017 0:000.8128640.983062
05/02/2017 0:000.9126090.929817
05/03/2017 0:000.8944770.887538
02/04/2017 0:000.9220330.885496
07/05/2017 0:001.0120210.935285
04/06/2017 0:001.0391910.966931
02/07/2017 0:001.0856091.014714
06/08/2017 0:001.021021.03946
03/09/2017 0:001.0426631.047121
08/10/2017 0:00
 
1.049764
05/11/2017 0:00
 
1.031841
03/12/2017 0:00
 
1.042663
02/09/2018 0:000.8110460.811046
07/10/2018 0:001.0121280.911587
04/11/2018 0:000.8292270.884134

 

My expectation is to ignore the blanks while calculating the 4 months average,

For example,

Oct, Nov, and Dec 2017, which is blank should be ignored

For Sep 2018 the last 4 nonblank data points should be considered - ( July 2017+ Aug 2017 + Sep 2017 + Sep 2018)/4

 

Can anyone help, Please!!

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @Anonymous ,

 

You need to pick up the last 4 date values from your table instead of making the calculation based on the DATESINPERIOD:

 

Average_Last_4Months = 
AVERAGEX (
    TOPN (
        4,
        FILTER (
            SUMMARIZE ( ALL ( 'Table'[All Date] ), 'Table'[All Date], "@CalcMeasure", [Calc_Measure] ),
            [@CalcMeasure] <> BLANK ()
                && 'Table'[All Date] <= MAX ( 'Table'[All Date] )
        ),
        'Table'[All Date], DESC
    ),
    [@CalcMeasure]
)

Using the TOPN you will pickup the last rows you need for each month.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

4 REPLIES 4
v-robertq-msft
Community Support
Community Support

Hi, @Anonymous 

According to your description, I can understand your problem, I think you can create a calculated column to help you to get the correct measure value, you can try this method:

  1. Create a calculated column like this:

 

Index =

var _rank=RANKX(FILTER('Table',[Calc Measure]<>BLANK()),[All Date],,ASC,Dense)

return

IF([Calc Measure]=BLANK(),BLANK(),_rank)

 

This is the output of the calculated column:

v-robertq-msft_0-1619491308589.png

 

  1. Create a measure:

 

Moving Avg 4 months =

CALCULATE(AVERAGE([Calc Measure]),FILTER(ALL('Table'),[Index]<=MAX([Index])&&[Index]>=MAX([Index])-3&&[Index]<>0))

 

  1. Then go to the table chart to place it like this:

v-robertq-msft_1-1619491308596.png

 

And you can get the correct value as what you want.

You can download my test pbix file below

Thank you very much!

 

Best Regards,

Community Support Team _Robert Qin

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thanks for suggesting this method. But the thing is Calc_Measure is not the column value. It's a calculated measure so Indexing doesn't work. The other solution posted by MFelix worked well.

Thanks for your help!!

 

 

 

MFelix
Super User
Super User

Hi @Anonymous ,

 

You need to pick up the last 4 date values from your table instead of making the calculation based on the DATESINPERIOD:

 

Average_Last_4Months = 
AVERAGEX (
    TOPN (
        4,
        FILTER (
            SUMMARIZE ( ALL ( 'Table'[All Date] ), 'Table'[All Date], "@CalcMeasure", [Calc_Measure] ),
            [@CalcMeasure] <> BLANK ()
                && 'Table'[All Date] <= MAX ( 'Table'[All Date] )
        ),
        'Table'[All Date], DESC
    ),
    [@CalcMeasure]
)

Using the TOPN you will pickup the last rows you need for each month.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Anonymous
Not applicable

Thank you so much for providing the solution!! This is what I was looking for.

 

For the first 4 months, it didn't calculate properly. So I wrote an "if" condition and Index column to pick my old calculation if the index < 4 else the solution provided by you(Average_Last_4Months). It worked really well.

 

Once again thanks a ton. 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.