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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.