Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in NYC Sept 4th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Create flag based on data from multiple prior dates?

Hello -

I am trying to create a FLAG (0 or 1) within my LOAD script based on several prior weeks of data. Here is a sample data set:

 

   

GroupDateMeasure
A12/1/20151
A12/2/20150
A12/3/20151
A12/4/20151
A12/5/20150
A12/6/20151
A12/7/20151
A12/8/20151
A12/9/20150
A12/10/20150
A12/11/20150
A12/12/20151
A12/13/20150
A12/14/20151
A12/15/20150
A12/16/20151
A12/17/20151
A12/18/20150
A12/19/20151
A12/20/20151
A12/21/20151
A12/22/20150

I want to add a field called FLAG which, for every date, will look at itself and also back at the prior two weeks and if all three MEASUREs are 1 then the FLAG will be 1. If any measure if not 1, then the FLAG will be 0.

For example, on 12/22/2015, the FLAG would be 0 because 12/22/2015 is 0, 12/15/2015 is 0 and 12/8/2015 is 1.

The FLAG on 12/21/2015 would be 1 because: 12/21/2015 is 1, 12/14/2015 is 1 and 12/7/2015 is 1.

I want to do this in the LOAD script because we have hundreds of millions of records and I'd like to not bog down the application, if possible. Otherwise, if you also know a solution via set analysis (or some other method/expression), I would gladly be open to suggestions .

1 Solution

Accepted Solutions
sunny_talwar

Try this:

Table:

LOAD Group,

    Date,

    Measure

FROM

[https://community.qlik.com/thread/197633]

(html, codepage is 1252, embedded labels, table is @1);

FinalTable:

LOAD *,

  If(Sum = 3, 1, 0) as Flag;

LOAD *,

  RangeSum(Measure, Previous(Measure), Previous(Previous(Measure))) as Sum

Resident Table

Order By Date;

DROP Table Table;


Capture.PNG

View solution in original post

6 Replies
sunny_talwar

Try this:

Table:

LOAD Group,

    Date,

    Measure

FROM

[https://community.qlik.com/thread/197633]

(html, codepage is 1252, embedded labels, table is @1);

FinalTable:

LOAD *,

  If(Sum = 3, 1, 0) as Flag;

LOAD *,

  RangeSum(Measure, Previous(Measure), Previous(Previous(Measure))) as Sum

Resident Table

Order By Date;

DROP Table Table;


Capture.PNG

maxgro
MVP
MVP

Source:

load * inline [  

Group Date Measure

A 12/1/2015 1

A 12/2/2015 0

A 12/3/2015 1

A 12/4/2015 1

A 12/5/2015 0

A 12/6/2015 1

A 12/7/2015 1

A 12/8/2015 1

A 12/9/2015 0

A 12/10/2015 0

A 12/11/2015 0

A 12/12/2015 1

A 12/13/2015 0

A 12/14/2015 1

A 12/15/2015 0

A 12/16/2015 1

A 12/17/2015 1

A 12/18/2015 0

A 12/19/2015 1

A 12/20/2015 1

A 12/21/2015 1

A 12/22/2015 0

] (delimiter is spaces);

Left Join (Source) load Date+7 as Date, Group, Measure as Measure_1W, Date as Date_1W Resident Source;

Left Join (Source) load Date+14 as Date, Group, Measure as Measure_2W, Date as Date_2W Resident Source;

Left Join (Source) LOAD Date, Group, if(RangeSum(Measure, Measure_1W, Measure_2W)=3,1,0) as Flag  Resident Source;

Not applicable
Author

Bingo! You're the man, SunnyT. Thanks so much for the quick reply and your expertise!!

Out of curiosity, do you know a way to do something similar via expression?

sunny_talwar

This expression:

=If(RangeSum(Above(TOTAL Measure, 0, 3)) = 3, 1, 0)

and Group, Date, Measure as your dimension

Note: Both these solutions assume that the accumulation won't restart, meaning that if for instance you want to look at Group A and Group B separately then you will need to alter the back-end and front-end solution as per the change in the requirement.

Capture.PNG

Not applicable
Author

Can this be changed to look back at previous weeks, rather than previous days?

sunny_talwar

It can be, what is the exact requirement?