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
Oros
Post Prodigy
Post Prodigy

Last X MONTHS based on selected MONTH

Hello,

 

I have a date table and a sales table.  What would be the correct measure if I would like to show the last X months based on the selected month.  For example, if I need to show the last 6 months, and I select December 2023, then the table should show the sales starting from July 2023. Thanks.

 

Oros_0-1721617191404.png

 

2 ACCEPTED SOLUTIONS
Shravan133
Super User
Super User

Hi,

 

Try this:

 

SalesLastXMonths =
VAR SelectedMonth = MAX('Date'[Month]) -- Get the selected month
VAR SelectedYear = MAX('Date'[Year]) -- Get the selected year
VAR StartDate =
EDATE(DATE(SelectedYear, SelectedMonth, 1),
-6 )-- Adjust this value to show the last X months (e.g., -6 for 6 months)

RETURN
CALCULATE([TotalSales],FILTER(ALL('DateTable'),'DateTable'[Date] >= StartDate &&'DateTable'[Date] <= DATE(SelectedYear, SelectedMonth, 1) - 1))

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

I have solved a similar question in the attached file.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

9 REPLIES 9
Ashish_Mathur
Super User
Super User

Hi,

I have solved a similar question in the attached file.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @Ashish_Mathur ,

 

Thank you for your reply.  Maybe I am missing something or a filter.  How do I show the last six months, including the month that is selected. It is NOT showing in this selection.

 

Oros_0-1721650380695.png

 

It works fine in the file that i shared with you.  Please study it carefully and then apply those formulas.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @Ashish_Mathur ,

 

It works!  Thanks a lot!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Shravan133
Super User
Super User

Hi,

 

Try this:

 

SalesLastXMonths =
VAR SelectedMonth = MAX('Date'[Month]) -- Get the selected month
VAR SelectedYear = MAX('Date'[Year]) -- Get the selected year
VAR StartDate =
EDATE(DATE(SelectedYear, SelectedMonth, 1),
-6 )-- Adjust this value to show the last X months (e.g., -6 for 6 months)

RETURN
CALCULATE([TotalSales],FILTER(ALL('DateTable'),'DateTable'[Date] >= StartDate &&'DateTable'[Date] <= DATE(SelectedYear, SelectedMonth, 1) - 1))

Hi @Shravan133,

 

Thank you for your reply.  I am having the following error.  Any idea?  Thanks again.

 

Oros_1-1721650725050.png

 

 

try using the month number instead of month from the date table in the measure.

Hi @Shravan133 ,

 

Thank you very much.  Your solution works very well!

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.