How to handle day of week adjustment - date

I am trying to resolve an issue involving comparing Year Over Year data results by day, and cant seem to quite figure it out.
I want to be able to accurately compare Year Over Year results by a specific day - Historically we have been calculating this as [Years Back] * 364 to obtain the number of days between the two dates, but as the yearly difference becomes greater than 7 it becomes incorrectly calculated.
I attached a picture here showing the anomaly where there is not a constant difference YoY and between two dates. 2015 is not a leap year.
Labor Day vs Memorial Day - YoY:
What is the best way to calculate any given dates past equivalent date? If I have two calendar tables, what do I match on? (I can get any of the following fields to match Date, Day of Week Number, Day of Month November, Week Number of Month, Week number of Calendar Year etc.)
For example, need to input the date, and have an output of past year equivalent
Like So:
Given Date | Past Year equivalent
20190902 | 20180903
20190902 | 20170904
20190902 | 20160905
20190902 | 20150907
20190902 | 20140901

Related

Is there a recommendation or norm on how to add months to a date when there is carry?

ISO-8601 defines time intervals, for example P1M is one month.
However it seems that is does not mandate how to determine what day is one month from a given date.
I looked up the documentation of sqlite and in their implementation, given YYYY-MM-DD, adding one month is adding 1 to MM, and then normalizing (if MM is greater than 12, then increment years, then if DD is greater than the number of days of the resulting month, then carry to next month).
However this can produce inconsistencies:
2020-01-29 + P1M = 2020-02-29
2020-01-30 + P1M = 2020-03-01
2020-01-31 + P1M = 2020-03-02
2020-02-01 + P1M = 2020-03-01 ‽, note that this this is sooner than previously
Moreover, with this method, if I specify an interval of one month and one day, should I first add one month, then one day, or should I add first one day, then one month?
2020-01-30 + P3D + P1M = 2020-02-02 + P1M = 2020-03-02
2020-01-30 + P1M + P3D = 2020-03-01 + P3D = 2020-03-04, so later if we add months first
The question is: is there anywhere a canonical way to proceed when adding an interval to a date, when the interval specifies years or months, which are variable durations?
The actual implementation of the term month in interval form is left mostly unspecified in most, if not all, standards on purpose.
The loosely accepted definition is about 30 days. The integer rounding of 365.25 / 12.
The method used varies on several factors:
Simplicity and Convenience:
It is easier to remember common days.
My electric bill is due on the 11th of every month. This is problematic when day is greater than 28.
I get paid the last day of the month.
The meeting is on the second Tuesday of each month.
Ease of calculation:
Fixed Month definition.
30 day grace period for new purchases.
Astronomical:
Lunar Based:
My apologies for the poorly condensed descriptions.
Synodic: Based on phases of the Moon: 29.18 to about 29.93 days(formed the basis of our modern system)
Sidereal: Based on "fixed" star passing. 27.321 days
Tropical: Based on celestial bodies at the Spring(northern hemisphere) Equinox: 27.321 days
Anomalistic: based on angle off of the elliptic orbit: 27.554 days
Draconic: based on angle off of the elliptic plane: 27.212 days
An even solar month would be:
About 365.2422 / 12 ~= 30.43685 days
An even calendar month would be:
Non-leap years: 30.4166666667 days
Leap years: 30.5 days
Or in terms of Weeks:
Just over 4 Weeks.
This is not meant to be an exhaustive list. There are many more historic or esoteric definitions not included here.
If I have missed one in use today, please let me know.
The main take away is that no single definition fits all purposes.
Be consistent and transparent:
Pick one and stick with it.
Let everyone know.

What is the correct order for substracting from a 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.

Dates, Days, Weeks, Difference, Training&shift start formula

Data shared in Google sheet found here:
I am trying to solve a quick issue to calculate the number of training days and working shifts days spent in a small range of calendar week based on a given 'training start date' and the below criteria:
Weeks are Sunday through Saturday
Each new agency staff should complete 4 days of training before being planned to work for a max of 6 days in and one calendar week
When less than 4 training days are completed during the first calendar week, the remaining roll over to the following, immediately followed by working days but not exceeding 6 days of work or training
To exclude Christmas day
Calculated from the given date for "Training start' calculate how many training shifts and working shifts for a period of calendar weeks
Calendar Weeks based on start/finish dates in the "dates' tab
Any advice, feedback or assistance in any way is greatly appreciated.

Finding difference between two dates in Tableau in terms of days, rounded to two decimals

I want to subtract two date fields in Tableau and find the difference in terms of days, rounded to two decimal places. I have already created a calculated field, however the result is rounded down to the nearest whole number.
The current calculated field I have is such:
DATEDIFF('day', [Reg Time], [Valid Time])
Which returns a result as such:
Reg Time | Valid Time | Datediff
11/1/2018 12:00 AM 11/1/2018 1:00 PM 0
What I want is this:
Reg Time | Valid Time | Datediff
11/1/2018 12:00 AM 11/1/2018 1:00 PM .5
The datediff would return a result of 0.50 because the difference is 12 hours(half a day)
All help is greatly appreciated.
I assume you are working with fields whose data type is datetime instead of date. Otherwise, a result in whole number of days is as good as it is going to get :-)
Dates are compound data types, with internal structure. Every date has three component parts: a year, a month and day of the month. Date fields have a calendar icon beside the field name in the sidebar data pane.
Datetimes are also compound data types, and add three additional components: hour, minute and second. Datetimes add a little watch to the calendar symbol in their icons.
So if your data source truly has datetime data, and the Tableau datatype is set to datetime, then the following calculations get the effect you requested -- showing the difference or duration measured in units of days.
DATEDIFF('hour', [Reg Time], [Valid Time]) / 24
or
DATEDIFF('minute', [Reg Time], [Valid Time]) / (24 * 60)
This calculation is useful when making Gantt bars since the field on the size shelf in that case is assumed to be expressed in days.
The DATEDIFF function will work for you however, the 'day' date part is going to round up. Working around this use the 'hour' date part in your DATEDIFF function.
Then you'll want to divide the result of this calculation by 24 (hours in the day) to get the fraction of a day.
The last thing you need to do is make sure not to aggregate these values, which Tableau will try to do by default.
Hope this helps.

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.