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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Kleine285
New Member

Moving average

Hello everyone, 

 

I've already looked at and tried some of your examples here, but they don't really help me.

I need to calculate an average that keeps changing/shifting.

 

Example: Average of January '22 = (December 2021 + January 2021)/2)
average February '22 = (December 2021 + February 2021)/2)
March '22 average = (December 2021 + March 2021)/2).

 

So I need to have the average of each month with December of the previous year.

 

PBI.PNG

I hope the attached table gives an idea of my data. 

1 ACCEPTED SOLUTION

Hi , @Kleine285 

I download this .pbix file , the filed in the visual is wrong, youo need to use the [Maand] filed in the 'Measures Mndst' table and [14.0] measure you created before not in the 'Table2'.

For your need , you should update the measure to this :

Moving average = var _date =SELECTEDVALUE('Mndst'[Maand])
var _this_year_month =MAXX( FILTER( 'Table 2' , [Maand] = DATE( YEAR( _date)  ,MONTH(_date),1)) , [14.0])
var _last_year_dec = MAXX( FILTER( 'Table 2' , [Maand] = DATE( YEAR( _date) -1 ,12,1)) , [14.0])
 return
IF(_last_year_dec=BLANK(),BLANK(),DIVIDE(_last_year_dec+_this_year_month,2))

The result is follows , i think that is your need :

vyueyunzhmsft_0-1666338875184.png

 

Best Regards,

Aniya Zhang

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

View solution in original post

9 REPLIES 9
tackytechtom
Super User
Super User

Hi @Kleine285 ,

 

Please, could you share some sample data as well as the your measures? 🙂

 

Thanks,

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Hi Tom, 

 

Is there any possibility for me to send the files for you? Maybe by email.

data.PNG

Measure 1..PNG

Measure 2.0.PNG

Measure 14.0.PNG

  

Please find attacted examples of my data and measures. 

Hi , @Kleine285 

Here are the steps you can refer to :

(1)This is my test data :

vyueyunzhmsft_0-1666330671295.png

(2)We need to click "New Table" to create a table because you have a measure [14.0]:

Table 2 = ADDCOLUMNS( SUMMARIZE( ALLSELECTED('Table') , 'Table'[Mannd]) , "14.0" , [14.0])

(3)Then we need to create a measure :

Moving average = var _date =SELECTEDVALUE('Table'[Mannd])
var _last_year_month =MAXX( FILTER( 'Table 2' , [Mannd] = DATE( YEAR( _date) -1 ,MONTH(_date),1)) , [14.0])
var _last_year_dec = MAXX( FILTER( 'Table 2' , [Mannd] = DATE( YEAR( _date) -1 ,12,1)) , [14.0])
 return
DIVIDE( _last_year_month + _last_year_dec , 2)

(4)We can put the filed we need and the measure on the visual and we will meet your need :

vyueyunzhmsft_1-1666330775402.png

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.(You can upload your file to OneDrive , then you can share the OneDrive link to share your file)

 

Best Regards,

Aniya Zhang

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

Hi Aniya Zhang,

 

I'm still getting an emty result for the measure.

I've added the link to the PBI.file for you to try and work your magic and a screenshot of my desired output. Please don't mind the blank cells in the sample.

 

Desired sample.PNG

https://drive.google.com/file/d/17oW-dq_GRX5fzv0RP7clAv5MdvqQCgYx/view?usp=sharing

Hi , @Kleine285 

I download this .pbix file , the filed in the visual is wrong, youo need to use the [Maand] filed in the 'Measures Mndst' table and [14.0] measure you created before not in the 'Table2'.

For your need , you should update the measure to this :

Moving average = var _date =SELECTEDVALUE('Mndst'[Maand])
var _this_year_month =MAXX( FILTER( 'Table 2' , [Maand] = DATE( YEAR( _date)  ,MONTH(_date),1)) , [14.0])
var _last_year_dec = MAXX( FILTER( 'Table 2' , [Maand] = DATE( YEAR( _date) -1 ,12,1)) , [14.0])
 return
IF(_last_year_dec=BLANK(),BLANK(),DIVIDE(_last_year_dec+_this_year_month,2))

The result is follows , i think that is your need :

vyueyunzhmsft_0-1666338875184.png

 

Best Regards,

Aniya Zhang

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

Hi Aniya Zhang,

 

I would like your help regarding the measure you provided. It seems that I can't filter on institution "Instelling". 

 

How can I make the "Moving Average" measure also work when filtering by insitution.

 

Thanks in advance for your help.

 

Isstvan

Thank you all, especially Aniya Zhang for your help.

tackytechtom
Super User
Super User

Hi @Kleine285 ,

 

How about this:

tackytechtom_0-1666121056836.png

 

And here the code:

RollingMeasure = 
VAR _prevYear = YEAR ( SELECTEDVALUE ( 'Table'[Date] ) ) - 1
RETURN
( 
    CALCULATE ( 
        SELECTEDVALUE ( 'Table'[Value] ), 
        ALL   ( 'Table' ),
        YEAR  ( 'Table'[Date] ) = _prevYear, 
        MONTH ( 'Table'[Date] ) = 12
    ) + 
    SELECTEDVALUE ( 'Table'[Value] ) 
) / 2

 

Let me know if this solves your issue 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/ 



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

I think it isn't working form me because the value for me is already a measure. In my example 14.0 = SUMX( Table., table col1 + table col 2 + ....).

 How can the solution be amended?

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors