Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

Luminary
Luminary

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
Honored Contributor III

Re: Scripting - Check Last Working Day

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
Esteemed Contributor

Re: Scripting - Check Last Working Day

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

Partner
Partner

Re: Scripting - Check Last Working Day

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

Luminary
Luminary

Re: Scripting - Check Last Working Day

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;

Luminary
Luminary

Re: Scripting - Check Last Working Day

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))))

Luminary
Luminary

Re: Re: Scripting - Check Last Working Day

Thanks for your response Sunil,

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

anbu1984
Honored Contributor III

Re: Scripting - Check Last Working Day

Replace Previous by Peek

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

View solution in original post

Luminary
Luminary

Re: Scripting - Check Last Working Day

Great, thanks Anbu. works perfectly!

Richard

Partner
Partner

Re: Scripting - Check Last Working Day

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

Luminary
Luminary

Re: Scripting - Check Last Working Day

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

Richard