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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Pawel_1990
Helper I
Helper I

DAX: Rolling average for defined months in past, not last "X" months

Hello Everyone,

This is my first post, so i hope I will match all requirements.

I have a data set with 4 columns:

Market: multiple countries

Date: 1st day of month, used mostly for Month & Year combination

KPI: multiple KPI names

Value: Value for KPI

Pawel_1990_0-1695994458430.png

I cannot edit thing in Power Query.

My aim is to get Rolling Average for each month, but not showing last "X" months but period of time from past.
As Example:
July 2023 - Averagex between March 2023 and May 2023
June 2023 - Averagex between February 2023 and April 2023

Moreover time period should be editable, so VAR is set in a way that I can change one number (let's say -2 to -3 and whole calculation changes)

Can you point me into right direction. Everything I found or wrote was for "X" last months.

Thank you in advance for help.

2 REPLIES 2
Greg_Deckler
Community Champion
Community Champion

@Pawel_1990 So you should be able to do something like the following (this assumes a bunch of stuff like you have your KPI and country in context, etc via slicer or within the visual or whatever.

Measure = 
  VAR __MonthsBack = -4
  VAR __Months = 3
  VAR __Date = MAX('Table'[Date])
  VAR __StartMonth = EOMONTH( __Date, __MonthsBack)
  VAR __MinDate = DATE( YEAR( __StartMonth ), MONTH( __StartMonth ), 1 )
  VAR __MaxDate = EOMONTH( __MinDate, 3 )
  VAR __Table = FILTER( 'Table', [Date] >= __MinDate && [Date] <= __MaxDate )
  VAR __Result = AVERAGEX( __Table, [Value] )
RETURN
  __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

thank you for help. You gave me correct approach for this one.
Two questions showed up.

#1. When execute this one line of code I get date in 19XX. When executing each part of code sepratly, for example: Year(__StartMonth) I get correct numbers. Any idea what's happening?

 

 

 VAR __MinDate = DATE( YEAR( __StartMonth ), MONTH( __StartMonth ), 1 )

 

 

#2- I think here VAR __Months should be used instead "3". Am I correct? 

  VAR __MaxDate = EOMONTH( __MinDate, 3 )

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.