Get Inspire insights from former attendees in our AMA discussion thread on Inspire Buzz. ACEs and other community members are on call all week to answer!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

How to generate number of months for a specific year from a range of dates?

AyxLearner
5 - Atom
Start DateEnd DateMonths in 2021

2017-12-01

2021-08-318
2018-11-182021-11-1711
2019-01-012023-12-3112

 

I'm trying to generate the number of months for a particular year from a given range of dates.

 

Ex: I want the number of months in 2021 between the "Start Date" and "End Date" for each record to be shown in the third column "Months in 2021". What are the ways I can achieve this?

5 REPLIES 5
Luke_C
17 - Castor

Hi @AyxLearner 

 

Here's one approach using the generate rows tool + summarize tool. Essentially we make one row for each day in the range, figure out what year/month they fall into, and then summarize it to get the count you're looking for. 

 

Luke_C_0-1615834810865.png

 

 

 

Emil_Kos
17 - Castor
17 - Castor

Hi @AyxLearner,

 

What i have done is I checked if the 4 first sings are equal to or higher 2021 and if yes I showed the middle values from the end date

Emil_Kos_0-1615835011167.png

 

Luke_C
17 - Castor

@Emil_Kos 

 

That's a great formula approach, one potential issue I see is with the last record, what if the end date was 2023-09-31? The formula would return 9, despite the range covering all 12 months of 2021. I think the logic could be tweaked slightly to check if the end date is after 2021 to fix.

Emil_Kos
17 - Castor
17 - Castor

Hi @Luke_C,

 

That is accurate. I made some changes to make it work as expected:

 

IF [End Date]>='2022-01-01' THEN 12 ELSEIF [End Date]>='2021-01-01' THEN DateTimeDiff([End Date],'2021-01-01','month')+1 ELSE 12 ENDIF

 

Emil_Kos_0-1615844973194.png


Now it should work perfectly!

AngelosPachis
16 - Nebula

Hi @AyxLearner ,

 

Here's a different expression you can use for this (all text following a // are comments and can be ignored)

 

 

IF DateTimeYear([Start Date])=2021 AND DateTimeYear([End Date])=2021
THEN DateTimeDiff([End Date],[Start Date],"months")+1

// If both your start date and end date are within 2021 then you count the months 
//between those two dates

ELSEIF DateTimeYear([Start Date])<2021 AND DateTimeYear([End Date])=2021 THEN
DateTimeDiff([End Date],"2021-01-01","months")+1

// If the start date is not within 2021 but your end date is, you want to find the 
//difference between your end date and the start of the year (2021-01-01)

ELSE 12
// Else both start date and end date will fall outside 2021, so your months count 
//will be 12
ENDIF

 

 

 

 

AngelosPachis_0-1615881101408.png

 

 

Hope that helps.

 

Regards,

 

Angelos

 

Labels