What is the correct order for substracting from a date - date

I just stumbled on an interesting problem when trying to calculate birthday from age. Basically the idea is to substract a number of years, months and days from the current date. However depending on the order of subtraction eg smaller to larger units or vice versa the result is different.
For example we need to substract 55 years 3 months and 14 days from 2021-01-13
If I subtract the years first the result will be 1965-09-29
If I subtract the days first the result will be 1965-09-30
It all comes from the difference between the number of days in a month. Now I'm wondering which is the generally accepted order.

Related

Checking days which are closest

This is going to be a bit hard to explain but
I have 2 dates, i'm interested in the day and month unless the 2 dates have the same year
2015/12/30 and 2019/01/04
ignoring the year part (kind of) the result i'm expecting 6 days difference for the 2 dates above
if i was to use date.DayOfYear i would get 364 (2015/12/30) and 4 (2019/01/04) respectively
however if the years are same then
2019/01/04 2019/12/30 then the result i'm expecting is 359 days difference
is there a clever way of doing this without peppering the code with if statements?
If the year difference was just 0 or 1, then you could just convert both dates to an absolute value (in seconds, ms, unix time or whatever), subtract the 2 values and convert it to full days. But if you have more than 1 year diff (as 2015 and 2019), then you have to have an "if" an think about what are you actually trying to do there. You shouldn't use DayOfYear, because some years have 366 days, so you can't be sure that 364 corresponds to 1 or 2 days left in that year.

Trying to Average number of accounts by hour, day of week, and month

I'm in healthcare and we're trying to assess the number of discharges we have per hour of day, but we'd also like to be able to filter them down by day of week, or specific month, or even a particular day of week in a particular month (e.g. " what is the average number of discharges per hour on Mondays in January?")
I'm confident that Tableau can do this, but haven't been able to make the averages show up in my line graph... every time that I convert it from COUNT to AVG, the line simply goes straight. I got close when I did a table calculation to find the Average (dividing the count per hour by the number of days captured in the report), but when I add a filter for either the month or day of week, selecting one of the options of the filter reduces the total number that is being counted, rather than re-averaging the non-filtered items. (i.e. if the average of the 7 days of the week is "10" for a particular hour, and I deselect the first three days of the week, it's now saying that my average for that hour is roughly 6, despite the fact that all of the days are very close to 10 at that hour.)
Currently, my data table has the following columns:
Account#/MonthYear/HourOfDay/DayOfWeek
ex.12345678/ Jan-17 / 12 /Sunday
I would just create a few calculated fields to differentiate the parts of the calendar you might want to filter/aggregate on. Mixing the month and day of the week with filtering is pretty straight forward with the calculated fields. Then do standard summing to get what you are looking for because an average count of records is always one unless you are throwing some other calculation into the mix. I threw a quick example up on Tableau Public for you to get the idea.

Rolling Count of Values BETWEEN two dates, 12 to 24 months ago (SPOTFIRE Custom Expression)

I am struggling to create this calculation.
I need to create a rolling count of all of a columns values BETWEEN two dates. 12 to 24 months ago.
I do not want to do this by limiting data, I need it in the custom expression due to other work.
Currently I have this equation. I thought this would at least calculate all the values since two years ago but it fails to do that as well. Does anyone have a simpler way to calculate 12 to 24 months ago?
(((Count(If(((Month([DATE])>=Month(DateAdd("mm",-24,DateTimeNow())))
and (Year([DATE])>=Year(DateAdd("yy",-2,DateTimeNow())))),
[EXTRAPOLATEDPRESSURE],null)))))
Solved. I was making it to complex with the Month and Year aspects.
Count(If(([DATE]>=dateadd("mm",-24,DateTimeNow())) and ([DATE]<=dateadd("mm",-12,DateTimeNow())),
[EXTRAPOLATEDPRESSURE],null))

Calculate how many years there's left on a debt and print it in date format

I'm using Google Docs Spreadsheet and I want to calculate how many years there is left on a debt from a fixed date (YYYY-MM-DD), into a date (same formation).
Example: I started to pay off on a debt which are on 20 000, 2014-03-31. Every month, I'll pay 200 and the debt will be payed off 2022-03-31. If I change the pay sum (200) to another sum, the date will be based on the new sum and change the final debt year.
How can I accomplish this?
If you are satisfied with a more or less exact solution, you can write:
=A1+(30*D1/D2)
where:
A1 is your starting date (in date format), such as: 2014-03-31
D1 is your total amount of debt, in this example: 20000
D2 is your monthly sum of debt payed, in this example: 200
The formula simply counts how many months you need to clear the debt (D1/D2, which gives 100), and then multiples it by the average days of a month (30). Then it adds this amount of days to the starting date. Actually this example gives the result: 2022-06-17 as the final clearance of the debt.
As I said it's not fully exact as it counts 30 days as an average month and it does not count loop years, but I think that it can be used for your purpose.
I hope it helps.
UPDATE:
You'll get a little closer result to the exact value, if you use this:
=A1+(365*quotient(D1/D2,12))+(30*MOD(D1/D2,12))
The QUOTIENT function will calculate how many years are needed (in the example D1/D2 divided by 12). We multiply this value with 365 (the number of days in a year).
Then the MOD function calculates how many more months are needed after the years, which is the modulo of "D1/D2 divided by 12".
This function will give 2022-07-27 as a result for this example.

How can I find out the number of days in between two dates in python

I have a problem where I need to find the number of days between any two given dates from a list of dates is there anywhere to find out the number of days between the given dates.
the given dates are:
06/06/2012
08/08/2012
10/10/2012
12/12/2012
and the last day of feburary
The thing you're looking for is deltatime; in particular, it is what type you get when you subtract 2 date(time)s.