How do I filter my data for the same time last year - Power Query - filtering

I'm making an excel file that will be used for a report on sickness absence.
We are looking at the last 100 days (which I've been able to do with a simple filter in my Applied Steps) and we want to compare to the same time last year plus 100 days before that date AND look at 50 days after that date.
How can I filter for this in Power Query? Or do I have to write a custom formula? (In which case, does anyone know what and where to write it)
Idea is to be able to run this report every day/week and give updates on how we're doing in the winter in comparison to last year.
Any help on this will be greatly appreciated, thank you in advance!

Use the drop down filter atop the date column ... Date filters ... between ... and enter some random set of dates like 1/30/2019 through 1/30/2020
That generates this code
= Table.SelectRows(#"Changed Type", each [date] >= #date(2019, 1, 30) and [date] <= #date(2020, 1, 30))
edit it for your desired date range which could be like this
= Table.SelectRows(#"Changed Type", each [date] >= Date.AddDays(targetdate, -465) and [date] <= Date.AddDays(targetdate, -315))
I assume -465 which is last year (365 days prior) plus 100 days back, and -315 which is -365+50
As for what to use instead of targetdate ... you didn't tell us .. perhaps hardcode a date #date(2019,10,31), use whatever you had in your original filter, or maybe the maximum of the current date column
List.Max(#"priorstepname"[date])

Related

Tableau: Same Day Last Year Auto Filter

I am trying to compare yesterday's data to the same day the year before. For example, yesterday is 11 November 2018. I want to compare to 12 November 2017 (same day but the year before). I am wanting this to be applied automatically on the filter so all I need to do is open the file and verify the numbers are correct before sending off the report.
Any help would be appreciated.
Thanks
There are many Tableau functions that manipulate dates. A couple in particular are relevant to your problem:
Today() - returns the current date
DateAdd() - adds or subtracts an interval from a date. For instance, DateAdd('year', Today(), -1) gives the date one year prior to today. The first argument to DateAdd is the level of granularity or date part.
DateDiff() - determines the difference of the interval between two dates. DateDiff('day', [Start Date], [End Date]) returns the number of days separating the two date arguments.
The functions are well documented in the online help. Construct the formulas you need and filter accordingly.
Isolate yesterday's date as its own field. For instance if that is the max date in your data, then {max([Date])} would create an LOD of the maximum date.
Then make a calculation that will display the same date last year:
year([Date]) = year([max_date])-1
and datepart('week',[Date]) = datepart('week',[max_date])
and datepart('weekday',[Date]) = datepart('weekday',[max_date])

Extract highest date per month from a list of dates

I have a date column which I am trying to query to return only the largest date per month.
What I currently have, albeit very simple, returns 99% of what I am looking for. For example, If I list the column in ascending order the first entry is 2016-10-17 and ranges up to 2017-10-06.
A point to note is that the last day of every month may not be present in the data, so I'm really just looking to pull back whatever is the "largest" date present for any existing month.
The query I'm running at the moment looks like
SELECT MAX(date_col)
FROM schema_name.table_name
WHERE <condition1>
AND <condition2>
GROUP BY EXTRACT (MONTH FROM date_col)
ORDER BY max;
This does actually return most of what I'm looking for - what I'm actually getting back is
"2016-11-30"
"2016-12-30"
"2017-01-31"
"2017-02-28"
"2017-03-31"
"2017-04-28"
"2017-05-31"
"2017-06-30"
"2017-07-31"
"2017-08-31"
"2017-09-29"
"2017-10-06"
which are indeed the maximal values present for every month in the column. However, the result set doesn't seem to include the maximum date value from October 2016 (The first months worth of data in the column). There are multiple values in the column for that month, ranging up to 2016-10-31.
If anyone could point out why the max value for this month isn't being returned, I'd much appreciate it.
You are grouping by month (1 to 12) rather than by month and year. Since 2017-10-06 is greater than any day in October 2016, that's what you get for the "October" group.
You should
GROUP BY date_trunc('month', date_col)

Compare DB2 Date with RPG date

I have this doubt on how to compare DB2 date, time fields with RPG date and time fields
If (Zpschdt < CurDat or (ZPschdt = Curdat and
(%Time() - ZPschtm) > 30));
For example in the above piece of code, Curdat is a character field which contains date value populated using below line
CurDat = %CHAR(%DATE():*MDY);
%Time() that is the current time needs to be subtracted from ZPschtm which is a DB2 time field and needs to be checked if the difference is greater that 30 minutes. How can this be achieved?
You can't compare a character variable to a date variable.
Generally, you have to either convert the character to date, or the date to character.
Since you character variable is formatted *MDY, it'd be easier to convert it to date; otherwise you'd need to change the format to YYYYMMDD in order to compare it as a numeric or character.
If Zpschdt < %Date(CurDat:*MDY)...
If you want to find schedule dates and times that are more than 30 minutes old, and the timeframe needs to cross days, you are going to have to use timestamps rather than dates and times. To deal with your situation, define a timestamp field, assign the schedule date and time, add 30 minutes to the timestamp, and compare that to the current timestamp. Like this:
dcl-s ts Timestamp;
ts = zschdt + zschtm + %minutes(30);
if ts < %timestamp();
// do something here
endif;
Think I have found a good enough solution for this as below:
If ( %Diff(%Date():Zpschdt:*Days) > 1 or
(%Diff(%Date():Zpschdt:*Days) = 0 and
%Diff(%Time():ZPschtm:*Minutes) > 30));
The first condition of Or - %Diff(%Date():Zpschdt:*Days) > 1 checks if Current date is ahead of the change date at least by one day.
The Second condition :
(%Diff(%Date():Zpschdt:*Days) = 0 and
%Diff(%Time():ZPschtm:*Minutes) > 30))
checks if the Change date is same as current day and if Current time is ahead of change time by at least 30 minutes..
Does this look good enough? Seems to do the job..

Date filter to compare 2 last full weeks on Tableau dashboard

I need to compare data from periods of 2 weeks each time.
For example - week 10-17 OCT to week 17-24 OCT
I want to create a dashboard
When access the dashboard I want to choose each time a period of LAST FULL 2 WEEKS (Monday to Monday) regardless to today's date.
Tried to make a calculated filed that adds and reduces days according to current date with DATEPART. maybe there is a better way? because this did not work for me as well.
One issue you may want to consider with the above approach, if you have multiple years of data, you'll also need a year filter to keep to the current year, otherwise it will add previous years into your view. Alternatively, you can modify the filter calc as follows:
IF (DATEPART('year', [Date]) = DATEPART('year', today())
AND DATEPART('week', [Date]) = (DATEPART('week', today()) -2)
OR DATEPART('year', [Date]) = DATEPART('year', today())
AND DATEPART('week', [Date]) = (DATEPART('week', today()) - 1))
THEN 'Include'
ELSE 'Exclude'
END
There is probably a better way to do this, but for the sake of a quick answer that works...
Create a calculated field with this logic
if (datepart('week',[Order Date])= (datepart('week',today())) -2)
or (datepart('week',[Order Date])= (datepart('week',today())) -1)
then 'Include'
else 'Exclude'
end
There are two things happening in this formula: first, we are retrieving the week number (1 through 52) from the date field ([order date]). Next we retrieve the week number for the current week and subtract 2 from the result. If the record ([Order Date]) is = to the current week minus 2, OR the current week minus 1, then "Include", otherwise exclude.
Then, you can add this field to the filter pane and set to only include 'Include'

Q (KDB) selecting today's date within date range

I am trying to set up an dynamic threshold by different user, but only return result from today's date. I was able to return all the records from past 30 days, but I am having trouble only outputting today's date based on the calculation from past 30 days.. I am new to q language and really having a trouble with this simple statement :( (have tried and/or statement but not executing..) Thank you for all the help in advance!
select user, date, real*110 from table where date >= .z.D - 30, real> (3*(dev;real) fby user)+((avg;real) fby user)
Are you saying that you want to determine if any of todays "real" values are greater than 3 sigma based on the past 30 days? If so (without knowing much about your table structure) I'm guessing you could use something like this:
q)t:t,update user:`user2,real+(.0,39#10.0) from t:([] date:.z.D-til 40;user:`user1;real:20.1,10.0+39?.1 .0 -.1);
q)sigma:{avg[y]+x*dev y};
q)select from t where date>=.z.D-30, ({(.z.D=x`date)&x[`real]>sigma[3]exec real from x where date<>.z.D};([]date;real)) fby user
date user real
---------------------
2016.03.21 user1 20.1