How to compare current period and previous period in MTM in Tableau? - tableau-api

For example: if we choose January 2020 then it should give the comparison of the previous month which is December 2019 and if we choose 2 months then the last 2 months need to be compared.

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.

Is it best practice to include current month or only previous completed months in monthly average equation?

I am trying to calculate a rolling "monthly average" equation for personal expenses and wondering if I should include the current month before it is over or if I should only include previous, fully completed months?
For instance, in mid-September, should I include January - August OR January - September and so on?

How to handle day of week adjustment

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

How to draw continues lines across panels in Tableau

I want to plot a couple of measures by months over the last three years. The plot would be divided by year. So I put year and month for columns and the measure values for rows. The year is defined as DATEPART('year', [Month Date]) and month is DATEPART('month', [Month Date]).
The plot looks like this.
The lines are broken between years. Both YEAR and MONTH are blue (discrete), but if I change them to green (continuous), the lines are connected but it looks weird.
I wonder how to make continuous lines across year?
What has happened is you have converted a date part (i.e. Month = March) to a continuous unit. What this does is simply changes March to 3. This looses the context of what the actual date is (i.e. March 2007). To change this right click on the date field on the column. You will see two separate areas to format the dates i.e. there are two years (2015, 2015), two quarters (Q2 and Q2 2015) etc. The first is a date part and the second is truncated date from the original date/timestamp.
You want to change the format to the second Month option (Month May 2015) and ensure the data type is continuous.
This should give you a continuous line across the year. If not screenshot me where you get to and I can assist from there.
Check if the last week and first of the next year is the same, I have a similar issue and solved it by passing the dates as Day-Month-Year.

Cumulative Days in Overlapping Date Ranges

I use Crystal Reports 11.
What I'd like to do is get a count of the unique days a student was enrolled in one of our many programs. If a student was enrolled in 3 programs in which the dates overlapped, I'd just want to count each day once and get a number.
Example using a student:
Algebra Jan 1 to Jan 10: 10 days
Science Jan 4 to Jan 11: 8 days
English Jan 9 to Jan 13: 4 days
I'd want the answer to be 13.
Good point. If they always over lap then this will work
Create a formula that finds maximum end date and the minimum start date based on patient. Then minus each formula.
i.e.: Maximum({xxx.enddate}, {xxx.patient}) - Minimum({xxx.startdate}, {xxx.patient})
If there are gaps between program dates, this won't work because it will include them.
Grouping the field by the patient name and Using
DistinctCount()
may be helpful.