chrisw76
New Member
- Joined
- Sep 23, 2016
- Messages
- 7
- Office Version
- 365
- Platform
- Windows
- MacOS
- Mobile
- Web
Hello!
Recently I was using the YEARFRAC() function again for the first time in a while and again noticed that it doesn't quite work as expected with the actual / actual basis (majority of my use cases). I'm not sure of the underlying logic to it, but it just doesn't seem to handle leap years very well or it is doing it to a much higher level of precision than required. That is, is may be taking into account the century / 400 not being a leap year. Whatever the case I'd expect that a formula like:
I'd expect it to return 4 but it returns 3.998357964 instead.
So, this is the formula I came up with to give me what I want. What it does calculate the beginning year fraction, the ending year fraction and any whole years in the middle and add them together. Here it is:
It probably could be a bit less verbose, but it's my style . Are there any mistakes / optimisations I missed?
Cheers, Chris W.
Recently I was using the YEARFRAC() function again for the first time in a while and again noticed that it doesn't quite work as expected with the actual / actual basis (majority of my use cases). I'm not sure of the underlying logic to it, but it just doesn't seem to handle leap years very well or it is doing it to a much higher level of precision than required. That is, is may be taking into account the century / 400 not being a leap year. Whatever the case I'd expect that a formula like:
Excel Formula:
=YEARFRAC( DATE(2020, 1, 1), YEARFRAC( DATE(2023, 12, 31) ) + 1, 1)
I'd expect it to return 4 but it returns 3.998357964 instead.
So, this is the formula I came up with to give me what I want. What it does calculate the beginning year fraction, the ending year fraction and any whole years in the middle and add them together. Here it is:
Excel Formula:
=LET(
_sd, MIN(<cell_1>, <cell_2>),
_ed, MAX(<cell_1>, <cell_2>),
_yr1Days, DATE(YEAR(_sd), 12, 31) - DATE(YEAR(_sd), 1, 1) + 1,
_yr2Days, DATE(YEAR(_ed), 12, 31) - DATE(YEAR(_ed), 1, 1) + 1,
_midYrs, YEAR(_ed) - YEAR(_sd) - 1,
_yr1Frac, (DATE(YEAR(_sd), 12, 31) - _sd + 1) / _yr1Days,
_yr2Frac, (_ed - DATE(YEAR(_ed), 1, 1) + 1) / _yr2Days,
IF(V25 > V26, -1, 1) * (_yr1Frac + _midYrs + _yr2Frac)
)
It probably could be a bit less verbose, but it's my style . Are there any mistakes / optimisations I missed?
Cheers, Chris W.