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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
YO_CO
Frequent Visitor

Dynamic rolling previous N months

Hi all,

 

I'm looking for a way to show the rolling N months sum, and have the matrix table respond dynamically.

 

For example if I choose Aug 2024 and 3 months, the matrix should only show JUN, JUL, AUG and a calculated 3 months sum for each (just to clarify - JUN should be the sum of APR + MAY + JUN).

If I choose 13 months, the matrix should show AUG 2023 till AUG 2024, with each month showing the sum of all previous 12 months + itself.

 

The caveat:

Page shouldn't have a date slicer. The page should always show from previous N months till current month (determined by calculated Calendar column "IsCurrentMonth".

 

I saw a video explaining how to do this:

https://youtu.be/JUUXGw86wUY?si=izFXfJ4lRk8Kx7-y

 

But no matter what I do it only adds months and shows future N months And even then it doesn't calculate the sums correctly, instead showing same value for all months. I don't need future, only past. Tried everything I could think of, but still only gets future.

Plus I'm not 100% sure the adjustment I made to rely on "Is Current Month" instead of actual dates slicer is correct.

 

Anyone had a similar issue and found a solution, or can suggest a different way to achieve this?

 

Thanks a lot!

4 REPLIES 4
YO_CO
Frequent Visitor

Thank you both for your detailed solutions.

Unfortunately this did not solve the issue, the months were either non-cumulative or the filters didn't work.

Using Rank_YM to rank the [Year Month] column also resulted in circular depandancy.

 

Eventually I managed to get it working using the mentioned video and some added transformations, though I'm not sure why the second calendar table is required.

Thanks again!

Anonymous
Not applicable

Hi @YO_CO ,

We're sorry that our ideas didn't help you solve your problem, and we're glad to hear that you finally found a solution. Could you please explain your solution so that everyone can learn from it, please?

Best Regards,
Dino Tao

Anonymous
Not applicable

Hi @YO_CO ,

Here is my sample data:

vjunyantmsft_0-1727403775011.png

I don't know how your calculated Calendar column is created, so I use this instead:

vjunyantmsft_1-1727403829258.png

Then I create a parameter for choose month numbers:

vjunyantmsft_2-1727403893392.png

vjunyantmsft_3-1727403914464.png

 

Use this DAX to create a measure and put the measure into the matrix visual:

 

 

SUM_VALUE = 
VAR _Parameter = SELECTEDVALUE(Parameter[Parameter])
VAR _CurrentMonth = MAX('Table'[Date])
VAR _PreviousMonth = EOMONTH(_CurrentMonth, -_Parameter)
RETURN
CALCULATE(
    SUM('Table'[Value]),
    ALL('Table'),
    'Table'[Date] > _PreviousMonth && 'Table'[Date] <= _CurrentMonth
)

 

 

vjunyantmsft_4-1727404003522.png


Then use this DAX to create another measure as a filter:

 

 

Measure = 
VAR _Parameter = SELECTEDVALUE(Parameter[Parameter])
VAR _CurrentMonth = MAXX(FILTER(ALL('Table'), 'Table'[Flag] = "IsCurrentMonth"), 'Table'[Date])
VAR _PreviousMonth = EOMONTH(_CurrentMonth, -_Parameter)
RETURN
IF(
    MAX('Table'[Date]) > _PreviousMonth && MAX('Table'[Date]) <= _CurrentMonth,
    1,
    0
)

 

 

Make the settings as shown in the figure below:

vjunyantmsft_5-1727404086544.png

vjunyantmsft_6-1727404109554.png

 

Output:
If I choose 3 months:

vjunyantmsft_8-1727404197786.png

If I choose 13 months:

vjunyantmsft_9-1727404212870.png

 

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Selva-Salimi
Super User
Super User

Hi @YO_CO 

 

First of all you need to go modeling tab and choose new table and create a table that end user can select "N" for N-previous month. this table can be crarted by 

 

Month_number = GENERATESERIES(1,100,1)  (*100 is the maximum value you want to have for your calculation)

SelvaSalimi_0-1727364802433.png

then you should create a column in your Date table as follows:

 

Year_Month = FORMAT('Date_table'[Date] , "YYYYMM")

 

and write another column to rank your month_date, as follows:

 

rank_YM = calculate(DISTINCTCOUNT('Date_table)'[Year_Month]) , filter ('Date_table' , 'Date_table'[Year_Month]>= EARLIER('Date_table'[Year_Month]) && 'Date_table'[Date] <= today()))
 
this column has blank values for future month.
 
Now, lets write a measure to calculate N_month prior, to cover this expectation you can write a measure as follows:
 
Measure N_Previous_Month := var selected_Number = selectedvalue ('month_Number' [Value] )
return
calculate (Sum(your_table [amount] ) , filter (all( your_table) , your_table [rank_YM] <= selectedvalue (Date [Rank_YM]) && your_table [rank_YM] >= selectedvalue (Date [rank_YM]) - selected_Number ))
 
** keep in mind that yor slicer should be set to single selection option
SelvaSalimi_1-1727366824225.png

 

and also, in the table that you want to show the sum, you should selecet the dates from your Date table.
 
 
If it does not work, tell me about your tables and the relationship between them. because this can play an important role!
 
If this post helps, then I would appreciate a thumbs up 👍 and mark it as the solution to help the other members find it more quickly.  
 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.