Calculate age from year born
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Solved! Go to Solution.
- Labels:
- Date Time
- Preparation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi @Art1
what you need is to convert your string into a date, in order to use a date/time calculation. YYYY is not a date, it's only a date part.
You could add a small formula to your current formula
DateTimeParse([DEMO.L4], "%yyyy")
Let's say DEMO.L4 is 2000 the date/time value converted will be 2000-01-01, Alteryx adds some "fake" data (day and month) in order to create a real date format. But this is not supposed to be a problem for you, since your unit in the datetimediff formula is year.
Your entire formula should look like that:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
@Art1 Alteryx can be picky about dates. I think you need a month and day, like
IF [DEMO.L4]== 9999 THEN 99 ELSEDateTimeDiff(DateTimeToday(),DateTimeParse(tostring([DEMO.L4])+"-01-01","%Y-%m-%d"),'years') ENDIF
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
@Federica_FF
Thank you for your reply. I pasted your formula into mine but the formula is still all black and I get the following error - Invalid type in operator ==.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
If the field you have is a string field then try a slight adjustment to @patrick_digan formula;
IF [DEMO.L4] == '9999'
THEN 99 ELSE DateTimeDiff(DateTimeToday(),[DEMO.L4] + "-01-01",'years')
ENDIF
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
@jdunkerley79
The formula you suggest gives me the following error message - Invalid ISO Date/Time "-01-01" for DateTimeDiff.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
@Federica_FF
Your formula worked. I just had to put 9999 in quotes since DEMOL.4 is a string variable. Thanks much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I tried the formula and it works great! Truly helpful. Thank you very much!
DateTimeDiff(DateTimeToday(),DateTimeParse(tostring([DEMO.L4])+"-01-01","%Y-%m-%d"),'years')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I used this to determine how old my patient was at time of procedure. Thank you!
DateTimeDiff([Service Date],[DOB],'years')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
The Formula DateTimeDiff([Service Date],[DOB],'years') is inaccurate in that it rounds the age up.
A better formula to account for the person's birth-date is:
Floor(DateTimeDiff(DateTimeToday(),[Date of Birth],"Months")/12) This rounds down to the calendar month, so if the birthday has not happened yet, the age is correctly reflected. As shown below, I am 64 until the 7th of July.
