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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
vivran22
Community Champion
Community Champion

Displaying results for Moving Average

Hello Community members,

 

I am using the following measure for calculating a monthly moving average:

 

Moving Average _M = 
VAR _CurrentDate = MIN(ftCalendar[Date])-1
VAR _FilterDate = DATESINPERIOD(ftCalendar[Date],_CurrentDate,-3,MONTH)
VAR _Monthly = 
    CALCULATE([Revenue],_FilterDate)
VAR _MonthCount = CALCULATE(DISTINCTCOUNT(ftCalendar[Month]),_FilterDate)
VAR _Average = 
DIVIDE(_Monthly,_MonthCount)

RETURN
_Average

 

 

How can I:

a) omit the moving average values where Revenue for current month is blank

 

 image.png

b) show moving average till the point I have 3 months historical values. In the example above, the moving average should show value for January 2020.

 

Appreciate your support

 

Cheers!

Vivek

1 ACCEPTED SOLUTION

@vivran22 - Couple minor things. One, didn't realize you were subtracting a da from "_CurrentDate". Second, missed an ALL.

Updated PBIX attached.

Moving Average _M2 = 
VAR _CurrentDate =
    MIN ( ftCalendar[Date] ) - 1
//GJD
VAR _MyCurrentDate = MIN(ftCalendar[Date])
VAR _PreviousMonth =
    EOMONTH ( _MyCurrentDate, -1 )
VAR _MonthStart =
    DATE ( YEAR ( _MyCurrentDate ), MONTH ( _MyCurrentDate ), 1 )
VAR _MonthEnd =
    EOMONTH ( _MyCurrentDate, 0 )
VAR _CurrentRevenue =
    CALCULATE (
        [Revenue],
        FILTER (
            ftCalendar,
            ftCalendar[Date] >= _MonthStart
                && ftCalendar[Date] <= _MonthEnd
        )
    )
VAR _PreviousMonthStart =
    DATE ( YEAR ( _PreviousMonth ), MONTH ( _PreviousMonth ), 1 )
VAR _PreviousMonthEnd =
    EOMONTH ( _PreviousMonth, 0 )
VAR _PreviousRevenue =
    CALCULATE (
        [Revenue],
        FILTER (
            ALL(ftCalendar),
            ftCalendar[Date] >= _PreviousMonthStart
                && ftCalendar[Date] <= _PreviousMonthEnd
        )
    ) //GJD
VAR _FilterDate =
    DATESINPERIOD ( ftCalendar[Date], _CurrentDate, -3, MONTH )
VAR _Monthly =
    CALCULATE ( [Revenue], _FilterDate )
VAR _MonthCount =
    CALCULATE ( DISTINCTCOUNT ( ftCalendar[Month] ), _FilterDate )
VAR _Average =
    DIVIDE ( _Monthly, _MonthCount )
RETURN
    SWITCH (
        TRUE (),
        NOT ( ISBLANK ( _CurrentRevenue ) ), _Average,
        NOT ( ISBLANK ( _PreviousRevenue ) ), _Average,
        BLANK ()
    )
//_Average


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

@vivran22 - It's really difficult to decipher this without sample source data but I will try to make some suggestions

 

Moving Average _M = 
VAR _CurrentDate = MIN(ftCalendar[Date])-1
//GJD
VAR _PreviousMonth = EOMONTH(_CurrentDate,-1)
VAR _MonthStart = DATE(YEAR(_CurrentDate),MONTH(_CurrentDate),1)
VAR _MonthEnd = EOMONTH(_CurrentDate,0)
VAR _CurrentRevenue = CALCULATE([Revenue],FILTER(ftCalendar[Date]>=_MonthStart && ftCalendar[Date]<=_MonthEnd))
VAR _PreviousMonthStart = DATE(YEAR(_PreviousMonth),MONTH(_PreviousMonth),1)
VAR _PreviousMonthEnd = EOMONTH(_PreviousMonth,0)
VAR _PreviousRevenue = CALCULATE([Revenue],FILTER(ftCalendar[Date]>=_PreviousMonthStart && ftCalendar[Date]<=_PreviousMonthEnd))
//GJD
VAR _FilterDate = DATESINPERIOD(ftCalendar[Date],_CurrentDate,-3,MONTH)
VAR _Monthly = 
    CALCULATE([Revenue],_FilterDate)
VAR _MonthCount = CALCULATE(DISTINCTCOUNT(ftCalendar[Month]),_FilterDate)
VAR _Average = 
DIVIDE(_Monthly,_MonthCount)

RETURN
//GJD
  SWITCH(TRUE(),
    NOT(ISBLANK(_CurrentRevenue)),_Average,
    NOT(ISBLANK(_PreviousRevenue)),_Average,
    BLANK()
  )
//GJD
//_Average

 



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Thanks @Greg_Deckler  for your prompt response.

 

I have used your measure  suggested by you (with minor required modifications) and it is returning blank:

image.png

 

Enclosing the pbix file for your reference.

 

Thanks!

Vivek

@vivran22 - Couple minor things. One, didn't realize you were subtracting a da from "_CurrentDate". Second, missed an ALL.

Updated PBIX attached.

Moving Average _M2 = 
VAR _CurrentDate =
    MIN ( ftCalendar[Date] ) - 1
//GJD
VAR _MyCurrentDate = MIN(ftCalendar[Date])
VAR _PreviousMonth =
    EOMONTH ( _MyCurrentDate, -1 )
VAR _MonthStart =
    DATE ( YEAR ( _MyCurrentDate ), MONTH ( _MyCurrentDate ), 1 )
VAR _MonthEnd =
    EOMONTH ( _MyCurrentDate, 0 )
VAR _CurrentRevenue =
    CALCULATE (
        [Revenue],
        FILTER (
            ftCalendar,
            ftCalendar[Date] >= _MonthStart
                && ftCalendar[Date] <= _MonthEnd
        )
    )
VAR _PreviousMonthStart =
    DATE ( YEAR ( _PreviousMonth ), MONTH ( _PreviousMonth ), 1 )
VAR _PreviousMonthEnd =
    EOMONTH ( _PreviousMonth, 0 )
VAR _PreviousRevenue =
    CALCULATE (
        [Revenue],
        FILTER (
            ALL(ftCalendar),
            ftCalendar[Date] >= _PreviousMonthStart
                && ftCalendar[Date] <= _PreviousMonthEnd
        )
    ) //GJD
VAR _FilterDate =
    DATESINPERIOD ( ftCalendar[Date], _CurrentDate, -3, MONTH )
VAR _Monthly =
    CALCULATE ( [Revenue], _FilterDate )
VAR _MonthCount =
    CALCULATE ( DISTINCTCOUNT ( ftCalendar[Month] ), _FilterDate )
VAR _Average =
    DIVIDE ( _Monthly, _MonthCount )
RETURN
    SWITCH (
        TRUE (),
        NOT ( ISBLANK ( _CurrentRevenue ) ), _Average,
        NOT ( ISBLANK ( _PreviousRevenue ) ), _Average,
        BLANK ()
    )
//_Average


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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