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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
JJha75
Frequent Visitor

Moving average of last 6 months excluding current month for just last 12 months

Hi all,
I need your help.
I am stuck in a DAX problem, my scenairio is
I have a date table and I have a project table with some score. I want to calculate moving average of 6 months of that score. The problem is in project table period first doesn't have data from past, so from start period upto month 5 there will be average of only available data period. i.e. in first period average will be just that month, in 2nd period average will be just for 2 months that is period one and two, that will go up to period 5. So there will be only complete six month average in period 6. Currently, my calculation is doing average even in first peried it is dividing first period score by 6, which is wrong and the average score get two low. Also I want only last twelve month display in the chart, currently I can't control it. Required correct data sample is below.

 

FISCAL period from Project tableScore6 months moving average
1/01/20241010
1/02/20241211
1/03/2024810
1/04/20241210.5
1/05/20241411.2
1/06/2024910.83
1/07/20241211.17
1/08/20241111
1/09/2024811
1/10/20241311.17
1/11/2024910.33
1/12/20241010.5
1/01/20251110.33


I have deadline 
Please help in this regard. 
Regards,
Jag

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @JJha75 ,

 

There are many ways to produce your required output, and one of them is by creating a calculated column like below: 

Moving average (6 months) = 
VAR EndDate = 'Table'[FISCAL period from Project table]
VAR StartDate = EOMONTH(EndDate, -6) + 1

VAR Cumulative_Score = 
    CALCULATE(
        SUM('Table'[Score]),
        'Calendar'[Date] <= EndDate &&
        'Calendar'[Date] >= StartDate
    )

VAR Number_of_Months = 
    CALCULATE(
        COUNTROWS('Table'),
        'Calendar'[Date] <= EndDate &&
        'Calendar'[Date] >= StartDate
    )

RETURN
IF(
    Number_of_Months > 0,
    divide(Cumulative_Score , Number_of_Months),
    BLANK()
)

The resultant output matches with your required output:  

 

DataNinja777_0-1723432973597.png

Best regards,

View solution in original post

3 REPLIES 3
DataNinja777
Super User
Super User

Hi @JJha75 ,

 

There are many ways to produce your required output, and one of them is by creating a calculated column like below: 

Moving average (6 months) = 
VAR EndDate = 'Table'[FISCAL period from Project table]
VAR StartDate = EOMONTH(EndDate, -6) + 1

VAR Cumulative_Score = 
    CALCULATE(
        SUM('Table'[Score]),
        'Calendar'[Date] <= EndDate &&
        'Calendar'[Date] >= StartDate
    )

VAR Number_of_Months = 
    CALCULATE(
        COUNTROWS('Table'),
        'Calendar'[Date] <= EndDate &&
        'Calendar'[Date] >= StartDate
    )

RETURN
IF(
    Number_of_Months > 0,
    divide(Cumulative_Score , Number_of_Months),
    BLANK()
)

The resultant output matches with your required output:  

 

DataNinja777_0-1723432973597.png

Best regards,

mickey64
Super User
Super User

For your reference.

 

MOVINGAVERAGE function (DAX) - DAX | Microsoft Learn

 

Using visual calculations in Power BI Desktop - Power BI | Microsoft Learn

 

Calculation = MOVINGAVERAGE([Sum of Score],6,ROWS)

 

mickey64_0-1723431056585.png

 

Thanks Mickey.
This is exactly the calculation I am looking for but not in the table, I want the same calculation in measure to use in chart. If you could find me the logic how that moving average works then I can use it in the Line & clustered column chart.

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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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