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

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

If condition in LOAD statement...Clarification Needed!

Hi all,

Consider the following data:

WeekIDWeekSeqPOSQty
201034-3947
201035-3883
201036-3774
201037-367
201038-357
201039-3453
201040-3393
201041-329
201042-3136
201043-3029
201044-2948
201045-2868
201046-2772
201047-2632
201048-2560
201049-2444
201050-2366
201051-2267
201052-2130
201101-2074
201102-1968
201103-1869
201104-1789
201105-166
201106-1568
201107-143
201108-1331
201109-1275
201110-1189
201111-1087
201112-969
201113-837
201114-771
201115-643
201116-544
201117-479
201118-397
201119-238
201120-188

I have a Load statement as follows:

---------------------- CODE START -------------------------------------

STORE_SALES:

LOAD *,

    0                                                                                                    as STORE_SALES.Input,

   if(STORE_SALES.RetailerMaxYear = STORE_SALES.Year, STORE_SALES.QtyPOS, null())                         as STORE_SALES.QtyPOS_YTD,

    if(STORE_SALES.RetailerMaxYear -1 = STORE_SALES.Year

    AND STORE_SALES.RetailerMaxWeek >= STORE_SALES.Week, STORE_SALES.QtyPOS, null())                     as STORE_SALES.QtyPOS_LYTD,

    if(STORE_SALES.WeekSequence =  -1, STORE_SALES.QtyPOS, null())                                         as STORE_SALES.QtyPOS_LastWeek,

    if(STORE_SALES.WeekSequence =  -2, STORE_SALES.QtyPOS, null())                                         as STORE_SALES.QtyPOS_PreviousWeek,

    if(STORE_SALES.WeekSequence >= -4, STORE_SALES.QtyPOS, null())                                         as STORE_SALES.QtyPOS_Last4Weeks,

    if(STORE_SALES.WeekSequence >= -8, STORE_SALES.QtyPOS, null())                                         as STORE_SALES.QtyPOS_Last8Weeks

;

LOAD

     WeekID,

     WeekSeq,

     POSQty as STORE_SALES.QtyPOS,

   //---------------------------------TRENDS------------------------------

    num(left(STORE_SALES.WeekID, 4))                                                             as STORE_SALES.Year,

    num(right(STORE_SALES.WeekID, 2))                                                            as STORE_SALES.Week,

RESIDENT TEMP_STORE_SALES;

DROP TABLE TEMP_STORE_SALES;

----------------------- CODE END ------------------------

I understand what the if condition is attempting to do. Depending on the weekseq number it calculates the LastWeek, PrevWeek etc.

My question is how does the IF condition examine all the loaded data in order to calculate the LastWeek, PrevWeek, Last4Week for a given record. Can any one provide an insight since I don't quite understand how the dataset is processed for each of the record since there are no loop conditions.

Best regards,

T.

1 Solution

Accepted Solutions
swuehl
MVP
MVP

I assume you want to aggregate the last 2 / last 4 Weeks per UPC then?

If so, you could just use the same approach, you just need to sort by UPC, WeekID accordingly and take care of the segment borders (change of UPC):

INPUT:

LOAD WeekID,

           UPC,

     WeekSeq,

     POSQty as QtyPOS

FROM

[http://community.qlik.com/message/183312]

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

Result:

LOAD *,

recno() as RecID,

rangesum(QtyPOS, if(peek(UPC)=UPC,peek(QtyPOS))) as Last2Weeks,

rangesum(QtyPOS, if(peek(UPC)=UPC,peek(QtyPOS)),

           if(peek(UPC,recno()-3)=UPC and recno()>2,peek(QtyPOS,recno()-3)),

           if(peek(UPC,recno()-4)=UPC and recno()>3,peek(QtyPOS,recno()-4))) as Last4Weeks

Resident INPUT order by UPC, WeekID asc;

DROP TABLE INPUT;

View solution in original post

8 Replies
swuehl
MVP
MVP

The code you posted does not aggregate the values for the given periods, it just introduce fields that contain that data, but still on a weekly, most granular base.

So you would need to e.g. use sum(STORE_SALES.QtyPOS_Last8Weeks) to get the aggregated values for the last 8 weeks.

It is similar to use flags to indicate the records that belong to certain periods, but instead of using binary flags, the above code copies the actual values, so you could just sum the field.

Hope this helps,

Stefan

Not applicable
Author

You are absolutely correct. I believe the application uses the sum of Last8Weeks field. I was wondering how does the IF condition work.

For example if I look at the data for 201120. I know that I can use the weekseq to find the prev week etc but how does the LOAD condition does the look up for the Prev Week when I'm loading the record for 201120. It's almost as if I do a load with using a WHERE condition on the WeekSeq.

I'm having difficulty understanding the looping thats done buy the load statement.

Bottom line is I would like to know what is the best way to look up data based on a condition from loaded dataset. As I said for the record for weekID 201120, I would like to add a field for previous week POSQty and Last4Weeks POSQty.

So in the end my data would look similar to

WeekID, WeekSeq, POSQty, PrevWeekPOSQty, Last4WeeksPOSQty

201120, -1, 88, 38, 302

I appologize if I'm not being clear.

T

swuehl
MVP
MVP

You could use peek() or previous() function to access records that have already been read in.

For example, to sum up the last 2 or read in POSQty:

...

rangesum(QtyPOS, peek(QtyPOS)) as Last2Weeks,

rangesum(QtyPOS, peek(QtyPOS),peek(QtyPOS,RecNo()-3),peek(QtyPOS,RecNo()-4)) as Last4Weeks

...

// recno() starts indexing with 1, peek() with zero //

Hope this helps,

Stefan

Not applicable
Author

This makes a lot more sense. Thanks!

Can the same peek() and previous() techniques be used for more complex data. For example:

WeekIDUPCWeekSeqPOSQty
20111011−19
20111022−189
20111033−165
20111044−136
20110911−262
20110922−287
20110933−224
20110944−290
20110811−350
20110822−37
20110833−365
20110844−342
20110711−436
20110722−470
20110733−456
20110744−460
20110611−559
20110622−59
20110633−562
20110644−522
swuehl
MVP
MVP

I assume you want to aggregate the last 2 / last 4 Weeks per UPC then?

If so, you could just use the same approach, you just need to sort by UPC, WeekID accordingly and take care of the segment borders (change of UPC):

INPUT:

LOAD WeekID,

           UPC,

     WeekSeq,

     POSQty as QtyPOS

FROM

[http://community.qlik.com/message/183312]

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

Result:

LOAD *,

recno() as RecID,

rangesum(QtyPOS, if(peek(UPC)=UPC,peek(QtyPOS))) as Last2Weeks,

rangesum(QtyPOS, if(peek(UPC)=UPC,peek(QtyPOS)),

           if(peek(UPC,recno()-3)=UPC and recno()>2,peek(QtyPOS,recno()-3)),

           if(peek(UPC,recno()-4)=UPC and recno()>3,peek(QtyPOS,recno()-4))) as Last4Weeks

Resident INPUT order by UPC, WeekID asc;

DROP TABLE INPUT;

Not applicable
Author

Thank you good sir! This works perfectly.

Now we continue to add more dimensions such as for each UPC we add Stores and so on what is the best aproach to sum POS values for past 2 weeks or past 4 weeks?

Also is there a way that we can create a summary table based on input table?

T

swuehl
MVP
MVP

You could create a new field for grouping, like

autonumber(UPC&Stores) as GroupingID

(add more fields to concatenation as you need). Then replace UPC in above Result load by GroupingID.

Not sure what you mean by 'create a summary table based on input table'. Could you explain that.

Honestly, I wouldn't recommend creating lot of precalculated summaries. QlikView is quite good in summing up values on the fly. You will probably lose some flexibilty if you focus too much on precalculation, since you won't do that for all combinations of dimension groupings / slicing.

Sometimes it is good to precalculate stuff though for performance reason.

Not applicable
Author

Works! I realize that in order to use my initial method I needed to group the dimensions. sum function only worked when I grouped the WeekID along with UPC etc.

T