Tableau - Running Total for Previous 24 Months - tableau-api

How can I make something like this? Note that ordering of Month was dependent in Parameter(Display Date). Parameter(Display Date) presents the last month to display.
Those line represent the following:
Will this be possible for tableau?

I'm going to assume that your date field is called Date and that the value you're measuring is called Value.
You just need two calculated fields:
Last Year / Current Year:
IF DATETRUNC('month', [Display Date]) >= DATETRUNC('month', [Date])
AND DATEADD('month', -11, DATETRUNC('month', [Display Date])) <= DATETRUNC('month', [Date])
THEN 'Current Year'
ELSEIF DATEADD('year', -1, DATETRUNC('month', [Display Date])) >= DATETRUNC('month', [Date])
AND DATEADD('month', -23, DATETRUNC('month', [Display Date])) <= DATETRUNC('month', [Date])
THEN 'Last Year'
END
There's a solid chance that it's possible to shorten that, but it gets the job done. The DATETRUNCs are to make sure that dates from the entire month are considered. Note that if a date is not in the current year or last year, then this field will be null.
Toss MONTH(Date) in the Columns shelf (make sure it's discrete), and toss SUM(Value) in the Rows shelf. Add Last Year / Current Year to the Color shelf to split the line into two, and then filter out the nulls for your Last Year / Current Year field.
At this point, everything should be working except that your chart starts with January (or the beginning of your fiscal year). We'll need to make a sort field.
Month Sort Field:
(MONTH([Date]) + (11 - MONTH([Display Date]))) % 12
This will assign a number from 1 - 12 to each date, with 12 being the month of Display Date and 1 being the month after Display Date.
Now right click on the MONTH(Date) in the Columns shelf and hit Sort.... Go to Field, choose Month Sort Field, and your months will sort such that Display Date is the final month on the axis.

Related

How to group data from 26th of a month to 25th of the next month using postgresql?

I want to define the start of a “month” as the 26th day of the previous calendar month (and of course ending on 25th).
How can I group by this definition of month using date_trunc()?
This expression gives the month you want:
date_trunc(
'month',
date_add(
day,
case
when extract(day from date) > 25 then 7
else 0
end),
my_date_col
)
)
Select it and group by it.
The logic is: If the day of the month is greater than 25, then add some days to bump it into the next month before truncating it to the month.
I would use an INTERVAL to calculate the correct dates. Here an example using generate_series():
SELECT d::date as reference_date
, (d + interval '25 days')::date AS first_day
, (d + interval '1 month' + interval '24 days')::date as last_day
FROM generate_series('2020-01-01'::timestamp, '2021-01-01'::timestamp, '1 month') g(d);

DB2: Bi-monthly query for a DB2 report

I am currently writing a Crystal Report that has a DB2 query as its backend. I have finished the query but am stuck on the date portion of it. I am going to be running it twice a month - once on the 16th, and once on the 1st of the next month. Here's how it should work:
If I run it on the 16th of the month, it will give me results from the 1st of that same month to the 15th of that month.
If I run it on the 1st of the next month, it will give me results from the 16th of the previous month to the last day of the previous month.
This comes down a basic bi-monthly report. I've found plenty of hints to do this in T-SQL, but no efficient ways on how to accomplish this in DB2. I'm having a hard time wrapping my head around the logic to get this to consistently work, taking into account differences in month lengths and such.
There are 2 expressions for start and end date of an interval depending on the report date passed, which you may use in your where clause.
The logic is as follows:
1) If the report date is the 1-st day of a month, then:
DATE_START is 16-th of the previous month
DATE_END is the last day of the previous month
2) Otherwise:
DATE_START is 1-st of the current month
DATE_END is 15-th of the current month
SELECT
REPORT_DATE
, CASE DAY(REPORT_DATE) WHEN 1 THEN REPORT_DATE - 1 MONTH + 15 ELSE REPORT_DATE - DAY(REPORT_DATE) + 1 END AS DATE_START
, CASE DAY(REPORT_DATE) WHEN 1 THEN REPORT_DATE - 1 ELSE REPORT_DATE - DAY(REPORT_DATE) + 15 END AS DATE_END
FROM
(
VALUES
DATE('2020-02-01')
, DATE('2020-02-05')
, DATE('2020-02-16')
) T (REPORT_DATE);
The result is:
|REPORT_DATE|DATE_START|DATE_END |
|-----------|----------|----------|
|2020-02-01 |2020-01-16|2020-01-31|
|2020-02-05 |2020-02-01|2020-02-15|
|2020-02-16 |2020-02-01|2020-02-15|
In Db2 (for Unix, Linux and Windows) it could be a WHERE Condition like
WHERE
(CASE WHEN date_part('days', CURRENT date) > 15 THEN yourdatecolum >= this_month(CURRENT date) AND yourdatecolum < this_month(CURRENT date) + 15 days
ELSE yourdatecolum > this_month(CURRENT date) - 1 month + 15 DAYS AND yourdatecolum < this_month(CURRENT date)
END)
Check out the THIS_MONTH function - there are multiple ways to do it. Also DAYS_TO_END_OF_MONTH might be helpful

Assessing the closest future date to today from 4 columns with dates in SQL

I have a table which features 4 columns of dates. I need to calculate in column 5 the date which is the closest future date to today and display this within the same row e.g. is the 20th anniv closer that the 85th birthday or is theh 10th anniv closer than the 85th birthday. NB the 85th birthday will alway be the maximum date. Column 6 needs to then display the appriopriate column heading
Really appreciate any help that any one can offer.
Column names / sample values
strt date - 01/01/2010
85th birthday - 11/12/2047
10th anniv - 01/01/2020
20th anniv - 01/01/2030
next date - 01/01/2030
anniv_type - 20th anniv
The following query uses CROSS APPLY to UNPIVOT the 4 date columns and then get the one closest to current date. TOP 1 is to get only one row ie the nearest date
select *
from yourtable t
cross apply
(
select top 1 *
from
(
values ([strt date], 'strt date'),
([85th birthday], '85th birthday'),
([10th anniv], '10th anniv'),
([20th anniv], '20th anniv')
) d ([next date], [anniv_type])
where [next date] > getdate()
order by datediff(day, getdate(), [next date])
) n

Add X days to a Received Date but Exclude Weekends/Holidays from a Date Table

I hope someone can help with a calculation that I am having trouble developing.
I am developing a report in a DB2 database that I need to add "X" number of days to a "RECEIVED" date/time when an order comes in between X and Y; but exclude Weekends and Holidays to add to the received date. I have created a [TBLCALENDAR] that lists the Weekends and Holidays (Example below); and from this, I want to ADD X number of days to a "DUEDATE"
tblCalendar]
DATE DAYOFWK DAY HOLIDAY
1/19/2019 7 Saturday
1/20/2019 1 Sunday
1/21/2019 2 Monday YES
So, for example 1, if I have an order that is placed on 1/18/2019 at 4:01pm; the due date should be 1/23/2019 at 11:00am.
Example 2: if I have an order that is placed on 1/18/2019 at
Conditions are:
Previous Date 4:01pm to Current Date 11:00am = Due Date should be + "X" business days by 11:00am
If order received Current day by 4:00pm = Due Date should be + "X" business days by 4:00pm
I have tried to reference the tblCalendar to get the [Received] date/time and add X number of days based off of an order, but it's not functioning the way I have hoped.
I have used the following code...but it doesn't exclude Weekends or Holidays when adding the specified number of days or have my order time requirement to take into account previous day after 4:00pm to current date of 11:00am:
RECEIVEDDATETIME + 2 days as DUEDATE;
I have also used the below code to reference TBLCALENDAR to find the # of holidays and weekend days in a date range:
( SELECT COUNT (*) FROM TBLCALENDAR AS C WHERE C.HOLIDAY = 'YES'
AND C.DATE BETWEEN TBLORDERS.RECEIVEDDATETIME
AND TBLORDERS.DUEDATETIME) +
(SELECT COUNT (*) FROM TBLCALENDAR
WHERE DAYOFWK IN (1,7)
AND DATE BETWEEN TBLORDERS.RECEIVEDDATETIME
AND TBLORDERS.UPLOADTIME) AS NONWORKINGDAYS
Expected field output
If order was received between 1/17/2019 4:01pm to 1/18/2019 10:59am = 1/23/2019 11:00am
If order received Current day by 4:00pm 1/18/2019 3:59am= 1/23/2019 by 4:00pm.
RECEIVEDDATETIME DUEDATE
1/17/2019 4:01pm 1/23/2019 11:00am
1/18/2019 10:00am 1/23/2019 4:00pm
Here is a solution without the time logic.
with tblCalendar(DATE, DAYOFWK, DAY, HOLIDAY) as (values
(date('2019-01-19'), 7, 'Saturday', '')
, (date('2019-01-20'), 1, 'Sunday', '')
, (date('2019-01-21'), 2, 'Monday', 'YES')
, (date('2019-01-22'), 3, 'Tuesday', '')
, (date('2019-01-23'), 4, 'Wednesday', 'YES')
, (date('2019-01-24'), 5, 'Thursday', '')
, (date('2019-01-25'), 6, 'Friday', '')
, (date('2019-01-26'), 7, 'Saturday', '')
)
, mytab (RECEIVEDDATE, DAYS2ADD) as (values
(date('2019-01-19'), 2)
, (date('2019-01-20'), 2)
, (date('2019-01-21'), 2)
, (date('2019-01-22'), 2)
)
select m.*, t.date as DUEDATE
--, dayofweek(date) as DAYOFWK, dayname(date) as DAY
from mytab m
, table
(
select date
from table
(
select
date
, sum(case when HOLIDAY='YES' or dayofweek(date) in (7,1) then 0 else 1 end) over (order by date) as dn_
from tblCalendar t
where t.date > m.RECEIVEDDATE
)
where dn_ = m.DAYS2ADD
fetch first 1 row only
) t;
The idea is to enumerate each day of the calendar after the RECEIVEDDATE (1-st parameter) starting from 1 with the following logic: the number of each day increases by 1 if it's non-holiday non-weekend day (the sum(...) over(...) expression).
Finally, we select a date with the corresponding number of days needed to add (2-nd parameter).
Solution idea:
Your tblCalendar is a good idea but I recommend to add the working day information instead of (only) flagging the holidays and weekends. The problem with the "off days" are that after you have figured out how many of them are in the period from your receive date to the receive date + X days you cannot easily add them because there could be other "off dates" in that perios again.
By numbering all the work days you could identify the workday which is closest (equal or bigger) to the receive date. Retrieve its number and add the X days to that number. Retrieve the date that has this work day number and you are fine.
The time logic should be built before that all because it could add another day to the X days.

Selecting the current week of data and reseting from week to week

I want to just select data for the current week. So...
If the current date is a Monday just select Monday
If the current date is a Tuesday select Monday and Tuesday's data
If the current date is Wednesday select Monday, Tuesday and Wednesday
...and so on. I want it to reset on Sunday and I believe it's some kind of "where" clause just don't know what. As you can see below I'm just counting the number of pieces into the oven and want it to accumulate as the week goes on and then reset on Sunday.
select
count(*) as PiecesIntoOven
from ovenfeederfloat
where...??
Thanks for the help.
If you're looking to do this in Sql Server, see below. Essentially this converts the current date to its numeric (0-6) value, then finds the 0th date for that week and uses it to set the lower bound of the where clause.
select sum(numberofpieces)
from Test
where dateofwork <= getdate()
and dateofwork >= (DATEADD(DAY, DATEPART(WEEKDAY,getdate()) * -1, getdate()) + 1)
Note that the '0' value is impacted by DATEFIRST. https://stackoverflow.com/a/1113891/4824030
I'm not certain how to do this in Oracle. Something like the below should work, but it's being finicky in sqlfiddle.
select sum(numberofpieces)
from Test
where dateofwork <= current_timestamp
and dateofwork >= (((to_char(level+trunc(current_timestamp,'D'),'Day') * -1) + current_timestamp) + 1)