Python relativedelta in Power Query/VBA

Tony 0 Reputation points
2023-06-04T11:05:58.3833333+00:00

For anyone who has knowledge about Python and either Power Query or VBA

Hi everyone, because of work task, I need to calculate the date difference in the following ways

= delta.years12 + delta.months1 + delta.days/(365.25/12)

Since I don’t have Python knowledge, based on my observation, Python releativedelta calculates date difference like that,

Relativedelta (endDate, startDate)

For example, 19 Jan 2023 and 16 Mar 2023

delta.months = startDate + i <= endDate (how many i months can be added but not larger than endDate)

delta.years = startDate + i <= endDate

delta.days = endDate - the adjusted startDate (startDate + i)

delta.months = 19 Jan 2023 —> 19 Feb 2023 <= 16 Mar 2023 = 1 month

delta.years = 0 year

delta.days = 16 Mar 2023 – 19 Feb 2023 (adjusted startDate) = 25 days

=1.8213552361396304

Based on this illustration, anyone who knows Python can write a VBA/Power Query version

Please note that relativedelta seems can identify end date of the month, like 31 Jan 2023 and 1 Mar 2023 is 1 month 1 day instead of 29 days

More examples and results as follow,

User's image

Appreciate if you can advise a code, I have used ChatGPT many times since Oct 2022 but still cannot find the solution

Microsoft 365 and Office Development Other
Microsoft 365 and Office Install, redeem, activate For business Windows
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.