Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
richard_pearce6
Luminary Alumni
Luminary Alumni

Scripting - Check Last Working Day

Hi,

I have a challenge and I'd like to know if there's a elegant solution.

My Script loads a calendar and then applies a flag for working days (i.e. Monday - Friday less and Public Holidays). So I end up with something like this:

Date                         ,Working Day Flag

3-Apr-2014               ,1

4-Apr-2014               ,1

5-Apr-2014               ,0

6-Apr-2014               ,0

7-Apr-2014               ,1

8-Apr-2014               ,1

I want to add another column [Last Working Day] which gives the values of the last date where the [Working Day Flag] was 1 or the [Date] value if it is a working day. For example:

Date                         ,Working Day Flag     ,Last Working Day

3-Apr-2014               ,1                               ,3-Apr-2014

4-Apr-2014               ,1                               ,4-Apr-2014

5-Apr-2014               ,0                               ,4-Apr-2014

6-Apr-2014               ,0                               ,4-Apr-2014

7-Apr-2014               ,1                               ,7-Apr-2014

8-Apr-2014               ,1                               ,8-Apr-2014

I can do this using previous() although I want it to be able to work where there are several consecutive none working days, as Previous() is limited to three iterations I'd like to know if there's another technique for this.

Many thanks

Richard

1 Solution

Accepted Solutions
anbu1984
Master III
Master III

Replace Previous by Peek

,if([Working Day Flag] =1 ,Date,Peek([Last Working Day])) AS [Last Working Day]

View solution in original post

9 Replies
SunilChauhan
Champion II
Champion II

If(Date=Monthend(Date) and weekday(Date)<>'Sat' and weekday(Date)='Sun',Date) as last working day

Sunil Chauhan
lironbaram
Partner - Master III
Partner - Master III

hi

as far as i know previous is not limited so if your expression will look like this

if([Working Day Flag] =1 ,Date,previous([Last Working Day])) AS Last Working Day

should work for as many dates as needed

richard_pearce6
Luminary Alumni
Luminary Alumni
Author

Hi Liron,

Thanks for your response, that was an approach I was taking. The issue being you first need to create the field [Last Working Day] in order to reference it.

So, looking at some simple code it won't work (unless I'm miss interpreting your answer?)

Load * Inline[

Date                         ,Working Day Flag

3-Apr-2014               ,1

4-Apr-2014               ,1

5-Apr-2014               ,0

6-Apr-2014               ,0

7-Apr-2014               ,1

8-Apr-2014               ,1

]
;





Calendar:

Load

Date

,
[Working Day Flag]

,
if([Working Day Flag] =1 ,Date,previous([Last Working Day])) AS [Last Working Day]

Resident Data;



Drop Table Data;

richard_pearce6
Luminary Alumni
Luminary Alumni
Author

My understanding is you're limited to three nested Previous() statement

I did try using Date as the field referenced above and nesting If and Previous statements. Works for three days which would cover a standard weekend but I got an error when I tried:

Previous(Previous(Previous(Previous(Date))))

richard_pearce6
Luminary Alumni
Luminary Alumni
Author

Thanks for your response Sunil,

Your reply has me a bit lost, the condition Date=Monthend(Date) means it would only evaluate Monthends?

anbu1984
Master III
Master III

Replace Previous by Peek

,if([Working Day Flag] =1 ,Date,Peek([Last Working Day])) AS [Last Working Day]

richard_pearce6
Luminary Alumni
Luminary Alumni
Author

Great, thanks Anbu. works perfectly!

Richard

lironbaram
Partner - Master III
Partner - Master III

yes this is my mistake you need to use peek and not previous

that way you can relate to the field himself in the function

so it should look like this

if([Working Day Flag] =1 ,Date,peek('[Last Working Day]')) AS Last Working Day

richard_pearce6
Luminary Alumni
Luminary Alumni
Author

Thanks Liron, I appreciate your response and time spent on this,

Richard