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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Saniat
Helper V
Helper V

Rolling average Last Month and Current Month

Hi,

I have two tables:

BLUE: Table used for Date slider/slicer
RED: Table used for all other dimensions/measures


temp.PNG



I am plotting 'sum of flag' in a timeline.

I wish to plot rolling average of 'this month and last month' in the timeline.

That means for January: 5 is okay.
For February: it should be (5+4) / 2 = 4.5
For March: (4+3)/2 = 3.5

Can someone please help me with that.


Thanks !

1 ACCEPTED SOLUTION

Hi @Saniat ,

Sorry about that, try this:

myMeasure = 
VAR _monthNumberCurrent =    MAX ( mytable[monthNumber] ) //get the month number from this row

var _thisMonthFlag = MAX(myTable[Flag])
VAR _prevMonthNumber =
    CALCULATE (
        MAX ( mytable[monthNumber] ),
        FILTER ( ALL ( myTable ), myTable[monthNumber] < _monthNumberCurrent )
    ) //filter the table to all month numbers less than this month number, and get the max value which is the month before

var _lastMonthFlag = CALCULATE(MAX(myTable[Flag]),FILTER(ALL(myTable),myTable[MonthNumber]=_prevMonthNumber))

RETURN
    DIVIDE ( _thisMonthFlag + _lastMonthFlag, IF(_lastMonthFlag = 0,1,2 ))

Nathaniel_C_0-1690227407911.png


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
Nathaniel_C
Community Champion
Community Champion

Hi @Saniat  @JohnShepherdAPD ,
If I understand your issue correctly, please try this measure:

myMeasure =
VAR _monthNumberCurrent =
    MAX ( mytable[monthNumber] ) //get the month number from this row
VAR _prevMonthNumber =
    CALCULATE (
        MAX ( mytable[monthNumber] ),
        FILTER ( ALL ( myTable ), myTable[monthNumber] < _monthNumberCurrent )
    ) //filter the table to all month numbers less than this month number, and get the max value which is the month before
RETURN
    DIVIDE ( _monthNumberCurrent + _prevMonthNumber, 2 )


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @Nathaniel_C ,

Thanks for this.

I am actually looking for average of [Flag] on those months ...

In the timeline on the right bottom corner ...
For February: it should be (5+4) / 2 = 4.5
For March: (4+3)/2 = 3.5

The final measure will be 2 months rolling average ...

Many thanks

Hi @Saniat ,

Sorry about that, try this:

myMeasure = 
VAR _monthNumberCurrent =    MAX ( mytable[monthNumber] ) //get the month number from this row

var _thisMonthFlag = MAX(myTable[Flag])
VAR _prevMonthNumber =
    CALCULATE (
        MAX ( mytable[monthNumber] ),
        FILTER ( ALL ( myTable ), myTable[monthNumber] < _monthNumberCurrent )
    ) //filter the table to all month numbers less than this month number, and get the max value which is the month before

var _lastMonthFlag = CALCULATE(MAX(myTable[Flag]),FILTER(ALL(myTable),myTable[MonthNumber]=_prevMonthNumber))

RETURN
    DIVIDE ( _thisMonthFlag + _lastMonthFlag, IF(_lastMonthFlag = 0,1,2 ))

Nathaniel_C_0-1690227407911.png


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




JohnShepherdAPD
Helper II
Helper II

Hi Saniat, take a look at the window function, some explanation is here:

https://www.sqlbi.com/articles/introducing-window-functions-in-dax/

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.