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
Axiomite
Resolver II
Resolver II

Dynamically giving a value to Last N months calculation

Hi there, 

 

What I am trying to achieve is to only show the values for the months smaller and equal to the selected month (slicer) but still but still only months in my financial year. The measure works if I manually add the number (in this case no. 6 below). 

But I would like write the measure to dynamically give the month number selected so I can be applied to the measure. Using the following measure I can calculate the number:


N months number =
VAR _start = STARTOFYEAR( dimDates[Date]) -1
VAR _end = ENDOFMONTH( dimDates[Date] )
RETURN
DATEDIFF( _start, _end, MONTH ) - 2
 
However, when adding it as a varialbe and using the variable for the N value in this case 6 it does not seem to work. The graph reverts to only the selected month.

 

PBIScreendump.jpg

 

Any suggestions? 
Kind regards,

Niel

4 REPLIES 4
Anonymous
Not applicable

Hi @Axiomite ,

 

Could you please share me with your pbix file after removing sensitive data?

 

Best Regards,
Eyelyn Qin

Hi @Anonymous ,

Let me know if you are able to access the pbix. 

 

Pbix File 

 

Many thanks for your reply.

 

Best Regards,

Niel

Hi @Anonymous ,

Let me know if you are able to access the pbix. 

 

Pbix File 

 

Many thanks for your reply.

 

Best Regards,

Niel

Greg_Deckler
Community Champion
Community Champion

@Axiomite Maybe:

N months number =
VAR _start = STARTOFYEAR( ALL(dimDates[Date])) -1
VAR _end = ENDOFMONTH( ALL(dimDates[Date]) )
RETURN
DATEDIFF( _start, _end, MONTH ) - 2

That said, I wouldn't recommend TI functions.

You may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000


Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...

 



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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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.