Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I need to count days of Sat and Sun units that happen consecutively. If ID has unit of either Sat or Sunday, not both days, then I shouldn't count it because the analysis aims to see who works over weekend without break.
So first, I reshaped my dat by ID, then assign 1 to Sat and Sun if both days have units. But I got errors as below
ID B of 10/10/2021 flag has to be 0, since previous day(Sat) 10/09/2021 doesn't have unit.
ID F of 10/16/2021 flag has to be 1, since both days(Sat and Sun) have unit.
I Paste the code and dat as below.
reshape_dat:
LOAD
ID,
Date,
Week,
Sum(Unit) as Unit2
Resident dat
Group by ID,Date, Week;
Drop table dat;
WeekEnds:
LOAD *,
if (Unit2 > 0 and previous(Unit2) > 0 and Date - Previous(Date) = 1 and (WeekDay(Date) = 'Sat' or WeekDay(Date) = 'Sun'), 1,0) as WeekEnd_Flag
Resident reshape_dat;
drop Table reshape_dat;
Hi @nezuko_kamado ,
The trickiest thing I find with these kinds of use cases is placing flags against both the Saturday and Sunday records as the use of "Previous" functions only place flags against the latter row. I've used an approach below that uses a join criteria with a key to overcome this.
The pick match function sets Saturday's date for the weekends regardless of it being Saturday (6) or Sunday (0). This column, along with the ID, will be used for linking the table below
dat:
LOAD
ID,
date(date#(Date,'MM/DD/YYYY'),'DD/MM/YYYY') as Date,
pick(match(num(weekday(date#(Date,'MM/DD/YYYY'))), 6, 0),
date(date#(Date,'MM/DD/YYYY'),'DD/MM/YYYY') ,
date(date#(Date,'MM/DD/YYYY')-1 ,'DD/MM/YYYY')) as link,
Week,
Sum(Unit) as Unit2
resident dat
Group by ID,Date, Week
;
// The ID and link are a composite key in the join ensuring the flag is placed against both the Saturday and Sunday record.
// The "Previous" functions as you used in your code ensure that both the ID and link values have to match to load in these values
left join(dat)
Load
ID,
link,
1 as WeekendFlag
Resident dat
where ID = Previous(ID)
and link = previous(link)
;
// The last step is to clean up the column and place the "0" in a weekend column.
Output:
load
*,
alt(WeekendFlag,0) as Weekend_Flag
Resident dat;
drop Table dat;
I hope this helps with your query.
Thanks
Anthony
Hi @nezuko_kamado ,
The trickiest thing I find with these kinds of use cases is placing flags against both the Saturday and Sunday records as the use of "Previous" functions only place flags against the latter row. I've used an approach below that uses a join criteria with a key to overcome this.
The pick match function sets Saturday's date for the weekends regardless of it being Saturday (6) or Sunday (0). This column, along with the ID, will be used for linking the table below
dat:
LOAD
ID,
date(date#(Date,'MM/DD/YYYY'),'DD/MM/YYYY') as Date,
pick(match(num(weekday(date#(Date,'MM/DD/YYYY'))), 6, 0),
date(date#(Date,'MM/DD/YYYY'),'DD/MM/YYYY') ,
date(date#(Date,'MM/DD/YYYY')-1 ,'DD/MM/YYYY')) as link,
Week,
Sum(Unit) as Unit2
resident dat
Group by ID,Date, Week
;
// The ID and link are a composite key in the join ensuring the flag is placed against both the Saturday and Sunday record.
// The "Previous" functions as you used in your code ensure that both the ID and link values have to match to load in these values
left join(dat)
Load
ID,
link,
1 as WeekendFlag
Resident dat
where ID = Previous(ID)
and link = previous(link)
;
// The last step is to clean up the column and place the "0" in a weekend column.
Output:
load
*,
alt(WeekendFlag,0) as Weekend_Flag
Resident dat;
drop Table dat;
I hope this helps with your query.
Thanks
Anthony