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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Rickstor
Frequent Visitor

Moving Average 5 periods

Hello. Please help me.

I need a moving average, 5 periods, in Power BI, using a measure.

This is so easy in Microsoft Excel. I need this:

moving-average-excel.jpg

 

The column 'year' has a text format.

 

After search in a lot of messages here, I couldn't find something that apply to my problem.

 

Thank you. 

 

1 ACCEPTED SOLUTION

pls try this

5 step 3 = 
VAR _CurentRx = MAX('data'[Rx])
VAR _Results = CALCULATE(SUM(data[value]), FILTER(ALL(data),'data'[Rx]<=_CurentRx&&'data'[Rx]>=_CurentRx-4), VALUES(data[cod_region])) /5
RETURN
IF(_CurentRx< 6 , BLANK(),_Results)

View solution in original post

21 REPLIES 21
Ahmedx
Super User
Super User

Sample PBIX file attached
https://1drv.ms/u/s!AiUZ0Ws7G26Rh1b1ePIQvpqZlbVu?e=qdEXjW

1) You need to create two calculated columns

1.pngScreen Capture #979.png3.png

2) write measures

3.png

Amazing! Hi @Ahmedx , your solution works. How can I change de measure to use the current 'SE' in measure? Example: 'SE' = 6, the moving average will be (36+37+24+41+42) / 5. Like this image: 

moving-average.jpg

 

Your measure works great but I add another column (I have a column with region codes, using a relationship with a table dimension of region codes) and the measure don't work. Like this: 

moving-average-complete.jpg

The dimension table is like this:
dimension-table-codes.jpg

How can I change the measure to work's with the region codes?

Thank you. 

Amazing! Hi @Ahmedx , your solution works. How can I change de measure to use the current 'SE' in measure? Example: 'SE' = 6, the moving average will be (36+37+24+41+42) / 5. Like this image: 

-----

write like thisА:

WINDOW(-4,REL,0,REL

this works: 

WINDOW(-4,REL,0,REL

But, when I add the column 'cod_region' doesn't work (the moving average values is wrong). Even adding the column 'cod_region' to the measure '5 step', didn't work. What I'm doing wrong? 

I attached your .pbix file with the data. Please, could you help me again?
Link: pbix moving average

 

 

 

pls try this

Screen Capture #1028.pngScreen Capture #1030.pngScreen Capture #1031.png

Hi @Ahmedx , almost done! Works great whitout 'code_region'. When I add this column, the values are the same for all regions. Like this:
moving-average-almost.jpg

using only 'year' it's great, it's working!
I tried change the ALL() to ALLSELECTED(), but doesn't work. 

What's missing?

Thank you for your help. 

pls try this

5 step 3 = 
VAR _CurentRx = MAX('data'[Rx])
VAR _Results = CALCULATE(SUM(data[value]), FILTER(ALL(data),'data'[Rx]<=_CurentRx&&'data'[Rx]>=_CurentRx-4), VALUES(data[cod_region])) /5
RETURN
IF(_CurentRx< 6 , BLANK(),_Results)

Hi. Your solution was amazing. Thank you. You helped me a lot. I apologize for the lack of response. I've been dealing with several health issues over the past few months, but I'm back now.

always happy to help!

you need to add this column to the virtual table, like this:

 

step = if( MAX('Table'[Rx])<6 , BLANK(),
CALCULATE(SUM('Table'[Value])
 , WINDOW(-5,REL,-1,REL,SUMMARIZE(ALLSELECTED('Table'),'Table'[year],'Table'[SE],'Table'[Rx],'Table'[Region]),ORDERBY('Table'[Rx],ASC))
)/5)+0

 

 

Ashish_Mathur
Super User
Super User

Hi,

What do the numbers in the SE column represent?  Are they week numbers?  If yes, then if you have a Calendar Table with week numbers as well, there is a fairly straight forward solution.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Yes, they are week numbers. Like this:

SE-START-END.jpg

It's a epidemiological calendar. 
Maybe can I use the column "End", using relationship between column 'SE' and my other dataset?

Hi,

Share the download link of the MS Excel file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

my previous post, that is a problem, I don't have the epidemiological calendar from years 2022 and 2021. In my project, I only need 2023 calendar.

Ahmedx
Super User
Super User

what doesn't work, write how it doesn't work

your solution almost work. Look:

In a excel I have this:
moving-average-excel-2.jpg

The moving average just go on through the years. 

 

Using your solution in DAX I have this (I think that is because effect of evaluation context):
moving-average-solution-dax.jpg

Sorry. Was my fault. In my original post I didn't show the other years. 

How to fix? 

Rickstor
Frequent Visitor

the evaluation context in DAX helps a lot sometimes, but, sometimes no

Ahmedx
Super User
Super User

pls try this

Sample PBIX file attached

https://1drv.ms/u/s!AiUZ0Ws7G26Rh1QdLQru4D5aoxXC?e=FUL9ap

Screen Capture #968.pngScreen Capture #969.png

Thank you.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.