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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
donaldo
Helper III
Helper III

Get number of months in current year between two dates

Hi all,

 

I'm trying to figure out a way to get the number of months in the current year between two dates. So these dates can start in this and the previous year and can end in this or the next year. The two dates are in columns like below. Ideally I would need a DAX statement that would just give me a number and not need to create another table or multiple rows.


Any ideas?

 

Open	          Close	     # months in 2019
31/03/2018	31/04/2019	
01/01/2019	01/11/2019	
03/04/2019	01/03/2020	
4 REPLIES 4
Greg_Deckler
Super User
Super User

@donaldo  - Perhaps try something like:

 

Measure 12 = 
VAR __start = IF(MAX('Table16'[Open])<DATE(2019,1,1),DATE(2019,1,1),MAX('Table16'[Open]))
VAR __end = IF(MAX('Table16'[Close])>DATE(2019,12,31),DATE(2019,12,31),MAX('Table16'[Close]))
VAR __table = GENERATESERIES(MONTH(__start),MONTH(__end))
RETURN
COUNTX(__table,[Value])

@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Hi, I am unable to try it out right now.

 

Won't this create a table or at the very least rows with GENERATESERIES? 

Is 2019 hardcoded here? As in this won't work upon entering 2020?

No, this just creates a temp table as a VAR in the measure itself, which it then uses to count all of the month numbers generated. It's probably overkill, you could just subtract the __start from the __end and add one but apparently I was feeling fancy. In the example, the 2019 is hard coded because I have no idea how you are actually planning on using it other than what you had in your post. If you had it in a matrix with a Year as columns, then you could just do something like DATE(MAX([Year]),1,1) for example. Those kinds of specifics are going to depend on your data and how you plan on using it. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490


@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Okay, cool. Right now I just need the it as part of a few other calculations I'm going to do. So it's not going into a visual. Sorry for not specifying. 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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