cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
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
Highlighted
Super User IV
Super User IV

Re: Get number of months in current year between two dates

@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!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




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

Proud to be a Super User!




Highlighted
Helper III
Helper III

Re: Get number of months in current year between two dates

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?

Highlighted
Super User IV
Super User IV

Re: Get number of months in current year between two dates

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!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




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

Proud to be a Super User!




Highlighted
Helper III
Helper III

Re: Get number of months in current year between two dates

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
Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

Mark your calendars and join us for our next Power BI Dev Camp!.

Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Tech Marathon

Maratón de Soluciones de Negocio Microsoft

Una semana de contenido con +100 sesiones educativas, consultorios, +10 workshops Premium, Hackaton, EXPO, Networking Hall y mucho más!

Top Solution Authors
Top Kudoed Authors