I am trying to add a new column in Spotfire so I can get the age of the person in years.
This is the function used:
Integer(DateDiff(Date(2018,3,31),Date([Y_BIRTH_DATE],[M_BIRTH_DATE], [D_BIRTH_DATE])))/365
However, the computed column returns as (Empty).
If I run the expression without the Integer() and the /365 the function returns the appropriate number of days.
Use the function Days :
Days(DateDiff(Date(2018,3,31),Date([Y_BIRTH_DATE],[M_BIRTH_DATE], [D_BIRTH_DATE])))/365
Explanation : your result is a timespan, so it cannot be converted to integer. The Days function will extract the integer value of the timespan.
I saw that the previous answer solved your problem, but I'm including an alternative solution for others with similar questions.
You're currently calculating the age of a person in years by taking the number of days that the person has been alive up to some point in time (3/31/2018 in your example), and then dividing the number of days by 365, which will result in a Real number, such as 28.6 years.
By dividing by 365, your example doesn't take Leap Years into account. This may not be an issue for you, but it might be for another user.
The following expression will
Return the number of years as a real number (e.g. 28.6), as does
yours
Account for leap years in its calculation
DateDiff("year",
Date([Y_BIRTH_DATE],[M_BIRTH_DATE], [D_BIRTH_DATE]),
Date(2018,3,31))
Note that I've formatted the expression so every one of the DateDiff function's arguments has its own line. Obviously, the expression could be collapsed to a single line, I just did it to improve clarity.
Also, the example provided in Spotfire for the DateDiff function has the earliest of the two Dates being passed as the first Date argument, and the later Date being passed as the second Date argument. It's become my habit to do so, as well, so that's the order in which I've placed your Date arguments in my example.
Related
I am trying to understand how the difference between DATEDIF(date1,date2,"d") and DATEDIF(date1,date2,"yd") in excel. It is very confusing when trying to deal with leap year dates.
for example
=DATEDIF("2/29/2020","3/1/2021","yd") gives 0.
but when I try to give
=DATEDIF("2/29/2020","3/1/2021","d") gives 1.
and one more thing is
=DATEDIF("2/1/2020","3/1/2021","yd") gives 29 and
=DATEDIF("2/1/2019","3/1/2021","yd") gives 28.
Some of the articles claim that year of start date is used for calculations so with that logic DATEDIF("2/29/2020","3/1/2021","yd") should give 1 instead of zero. Can someone explain how the calculations are being done and what year is being considered for the calculations?
From the docs you can see what is the difference.
To be explicit, the YD variant isn't taking year into account, thus showing different results in the case of leap year.
I have a set of dates, in total 7 periods spanning 6 months each with corresponding calculation factor.
I will have user input of period for which they'd want the calculation to be done, which can fall within one of the 6 months periods or encompass between two or more such periods wholly or partially.
Illustration:
The preset periods:
User input:
I've obtained decimal value (monthly basis) of the periods input by the user for calculation. For first instance (see user input), the decimal value for the period 01-01-2015 to 29-04-2015 will be 3.97 on monthly basis. The calculation for that period would be like:
n*3.97*113%
For the second instance, the decimal value for the period 30-04-2015 to 30-06-2015 would be 2.03, which would be used to do calculation at 113% and then the result will be added to the calculation done at 119% using the decimal value 5.65 for the period 01-07-2015 to 20-12-2015:
(n*2.03*113%)+(n*5.65*119)
Think I can handle the breaking up of periods since the revision event is bi-annual on particular dates but advice regards to that is welcome. More importantly, I need help tracing the preset calculation factor (say 132%) corresponding to the period input by the user as illustrated above. Is it feasible?
I will use the standard approach for finding the overlap between two dates and will split the task into three parts as in my answer to this recent question.
(1) The first part is finding the overlap between the user's range of dates and one or more revision periods in whole months and will need an array formula. I have chosen to use the Datedif function 1 to get the difference in months between the beginning and end of the overlaps. If there is no overlap, the start date fed in to Datedif will be after the end date, and it will return an error which can be trapped by Iferror. If the user's dates start in A2 and B2, this gives in C2:
=SUM(IFERROR((DATEDIF(IF(K$2:K$8>A2,K$2:K$8,A2),IF(L$2:L$8<B2,L$2:L$8,B2),"m")+1)*M$2:M$8,0))
which has to be entered as an array formula using CtrlShiftEnter
The above result includes the first and last months entered by the user even if they are incomplete months. It's then necessary to subtract any missing days in the first and last months.
(2) Missing days in first month as a fraction of the number of days in that month in D2:
=SUMIFS($M$2:$M$8,$K$2:$K$8,"<="&A2,$L$2:$L$8,">="&A2)*(A2-EOMONTH(A2,-1)-1)/(EOMONTH(A2,0)-EOMONTH(A2,-1))
A noted by OP, this could also have been done using sumproduct, vlookup or index/match.
(3) Missing days in last month as a fraction of days in that month in E2:
=SUMIFS($M$2:$M$8,$K$2:$K$8,"<="&B2,$L$2:$L$8,">="&B2)*(EOMONTH(B2,0)-B2)/(EOMONTH(B2,0)-EOMONTH(B2,-1))
The total is just (1)-(2)-(3) or
=C2-D2-E2
I have put the results of OP's two examples for comparison in H2 and H3: my results agree with them in the first 3 significant figures.
n*3.97*113%
(n*2.03*113%)+(n*5.65*119)
In all cases I have set n=1 and ignored the fact that the rate is a percentage.
This shows how the results would be calculated manually:
1 Pros of using Datedif:
(1) Works across year boundaries unlike just using Month function.
(2) Works conveniently with Iferror to identify non-matching date ranges.
Cons of using Datedif:
(1) It is an undocumented function and may be withdrawn in future.
(2) In this particular case, all date calculations are within the same year so Month would be useable.
Whenever I type a function into Microsoft Excel 2013, a small hint box is displayed which lists the function's arguments.
For some reason, this hint box doesn't appear when using DATEDIF(). I can't find it listed anywhere on the formula tab, nor am I shown any reminder of what arguments to use. Excel won't even autocomplete the formula name while typing.
Returns number of days between Nov 15-Nov 20
The formula works as expected once entered, but it's difficult to use properly when I need to have the arguments memorized.
Why isn't Excel displaying argument hints for DATEDIF()?
DATEDIF is an undocumented function in Excel
From Office Support:
Excel provides the DATEDIF function in order to support older
workbooks from Lotus 1-2-3. The DATEDIF function may calculate
incorrect results under certain scenarios.
From My Online Training Hub
For some reason Microsoft [doesn't] think the DATEDIF function is worthy
of any documentation since Excel 2000. In fact they say they only
include it in recent versions for backward compatibility.
From Happy LTD
Microsoft no longer supports this function in Excel. You can still use
it, but you don’t get any help with it.
A suggestion is: UDF replacement for Excel's DATEDIF function
Also visit:
Do not use the undocumented DATEDIF function
Introduction To The DATEDIF Function
The "DATEDIF" function has its origins in "LOTUS 1-2-3", and apparently Excel provides it for compatibility purposes. It has been available since Excel 5, but Excel 2000 is the only version that ever documented it in its Help system.
The function takes 3 arguments: start_date, end_date, and a code that represents the time unit of interest. I am including cells A1 & A2 for the purposes of the example. Substitute the cells you need to use.
The following code is an example:
=DATEDIF(A1, A2, "y") You must enclose the code in quotation marks.
The following table lists the codes available with the returns.
"y" The number of complete years in the period.
"m" The number of complete months in the period.
"d" The number of days in the period.
"md" The difference in days between the start_date and end_date. the
months
and years are ignored.
"ym" The difference in months between the start_date and end_date.
the days
and years are ignored.
"yd" The difference in days between the start_date and end_date. The
years
of the date are ignored.
NOTE, the start_date must be earlier than the end_date or the function will return an error.
I want to get the difference between two dates, but I want the result to be in decimal numbers, for example if the difference is (1 Year and 6 Months) I want the result to be (1.5) which means Year and a half!
There is a specific function which does this in excel - YEARFRAC function - it calculates slightly differently according to the 3rd argument, see excel help for more but this is the basic version
=YEARFRAC(A2,B2,1)
Where A2 is the start date and B2 the end date. This is accurate to the day, I assume that's what you want
Got formulas for figuring my differences between date periods in days as follows:
=IF(F7="","0",DAYS360(F6,F7)+1)
This gives the days result for each period that I am interested in, but I then need to add each period of days together and subtract them from a current date (like doing service computation). The issue is that I need to do this second calculation within a 360-day calendar as well. If I just try to do a days360() formula with one value being the current date and the other being the total number of days that I need to backtrack, then the "original" date that it comes up with is drastically off.
This seems to be the equivalent of the difference between NETWORKDAYS and WORKDAY functions. The former counts working days between two dates, the latter adds working days to a date, you essentially want the WORKDAY equivalent for DAYS360, which I don't think exists.
You can manipulate DAYS360, though, e.g. with a date in A2 and number of days to subtract (in 360 day mode) in B2 you can use this formula for the date
=A2-B2-MATCH(B2,INDEX(DAYS360(A2-B2-ROW(INDIRECT("1:"&CEILING(B2/30,1))),A2),0),0)