Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
mtucholski
Creator
Creator

How to make a sub-group based on assumptions.

Hello,

I have a source table which looks like below (columns 1-6). But I need to add some data to it somehow (columns 7-9).

Here is some explanation:

I have an order (1) with plenty of documents (5) where 'Pw' stands for product made and 'Rw' for material used (4). In every order there is a sub order (9) which I'm aware of but there is no simple way to show it.

I know the start date (7) and the end date (8) of the sub order (9) because I know that after every group of documents marked as 'Pw' there will be at least one document marked as 'Rw' (4). So I know that the sub order starts with the first 'Pw' that occurs right after 'Rw' and it ends with the last 'Rw' that occurs right before another 'Pw'.

The question is how to do it in QlikView load script :-). I will apprecieate any help. Thank you.

        

123456789
order_nodatetimedocumentdocument_nomaterial_codefromtosub_order_no
2291852018-02-2622:07:28Pw  1863862224953472018-02-26 22:07:282018-02-26 22:08:371
2291852018-02-2622:08:00Rw  1863861924781982018-02-26 22:07:282018-02-26 22:08:371
2291852018-02-2622:08:18Rw  1863861624944522018-02-26 22:07:282018-02-26 22:08:371
2291852018-02-2622:08:37Rw  1863861024930082018-02-26 22:07:282018-02-26 22:08:371
2291852018-02-2622:23:40Pw  1863860724953542018-02-26 22:23:402018-02-26 22:24:282
2291852018-02-2622:24:09Rw  1863860424930082018-02-26 22:23:402018-02-26 22:24:282
2291852018-02-2622:24:28Rw  1863860124944522018-02-26 22:23:402018-02-26 22:24:282
2291852018-02-2623:50:23Pw  1863859824953902018-02-26 23:50:232018-02-26 23:51:473
2291852018-02-2623:51:43Pw  1863858624944522018-02-26 23:50:232018-02-26 23:51:473
2291852018-02-2623:51:44Rw  1863858324930082018-02-26 23:50:232018-02-26 23:51:473
2291852018-02-2623:51:47Rw  1863852824944482018-02-26 23:50:232018-02-26 23:51:473
2291852018-02-2700:22:17Pw  1863852524954162018-02-27 00:22:172018-02-27 00:23:454
2291852018-02-2700:23:40Rw  1863852224944482018-02-27 00:22:172018-02-27 00:23:454
2291852018-02-2700:23:41Rw  1863851924930082018-02-27 00:22:172018-02-27 00:23:454
2291852018-02-2700:23:45Rw  1863840524628512018-02-27 00:22:172018-02-27 00:23:454
2291852018-02-2700:57:17Pw  1863836824954402018-02-27 00:57:172018-02-27 00:59:475
2291852018-02-2700:58:36Pw  1863836524930082018-02-27 00:57:172018-02-27 00:59:475
2291852018-02-2700:58:55Pw  1863836224894732018-02-27 00:57:172018-02-27 00:59:475
2291852018-02-2700:59:47Rw  1863827224628512018-02-27 00:57:172018-02-27 00:59:475
1 Solution

Accepted Solutions
mtucholski
Creator
Creator
Author

Hi,

just add "and previous(document)<>document" in first "where" and we are almost there :-). So the final code is:

Submap:

Mapping Load 

order_no&'|'&date&'|'&time&'|'&document as key,

RowNo() as temp_sub_order_no

FROM 

[X]  (x)

where document='Pw' and previous(document)<>document

;

Table1:

Load

order_no,

date,

time,

document,

document_no,

material_code,

ApplyMap('Submap', key, null()) as sub_order_no

;

LOAD

order_no&'|'&date&'|'&time&'|'&document as key,

order_no,

date,

time,

document,

document_no,

material_code

FROM 

[X]  (x)

;

NoConcatenate

New:

Load

order_no,

date,

time,

document,

document_no,

material_code,

If( isnull(sub_order_no), peek(sub_order_no),sub_order_no )as sub_order_no

Resident Table1

order by date, time

;

Drop table Table1

;

There is some exceptions which I don't want to bring in here - just have to work it out by myself . Anyway, thank you for your help!

View solution in original post

10 Replies
Lisa_P
Employee
Employee

Hi Mateusz,

I have re-created your table in the load script as per below:

Original:     //Load original data

LOAD order_no,

     Date(date) as date,

     time,

     document,

     document_no,

     material_code,

     if(peek(sub_order_no)<>previous(sub_order_no), 1, sub_order_no) as sub_order_no, // create sub_order-no

     if(peek(sub_order_no)<>previous(sub_order_no), date&' '&time, from) as from, // create from

     order_no&'|'&sub_order_no as ordersub // create ordersub for aggregation purpose below

FROM

(txt, codepage is 1252, embedded labels, delimiter is '\t', msq, header is 1 lines);

Left Join(Original)

Load

     ordersub,

     Time(max(time)) as totime

Resident Original

Group by ordersub;

NewTable:

load *,

     date&' '&totime as to  // create to field

Resident Original;

Drop table Original;

Screenshot of the table box:

Thread308136.PNG

mtucholski
Creator
Creator
Author

Hi,

that's not exactly what I meant. The source table is columns 1-6 and I want to create columns 7-9.

So I can't use "sub_order_no" in first LOAD because it doesn't exist.

Also a question, isn't peek() and previos() equal in this scenario? There was no transform of original data. So the input and output will have the same data.

Peek() vs Previous() – When to Use Each

Anyway, thanks for introducing me to Peek() and Previous() functions. I have managed to mark the first "Pw" - which date will be te first date of the next sub_order and the end date of the previous one. This way, after few resident loads I can make an Interval for the sub_orders (IntervalMatch).

Lisa_P
Employee
Employee

As you can see if you read the script, only the first 6 columns are loaded directly, the others are derived.  I copied only these fields to work out the solution. I created the sub_order_no first as it was the simplest logic.

Peek will look at the output of the load statement, where previous will look at the output of the load statement.

mtucholski
Creator
Creator
Author

Hi,

isn't this true that when you are refeering to field before specific the output column name, that field should already exist and be specified before?

Like here: " if(peek(sub_order_no)<>previous(sub_order_no), 1, sub_order_no) as sub_order_no," your are refeering to the field that doesn't exist but it's currently defining.


Just to be sure I have created an empty .qvw, uploaded same data I wrote as an example and paste your code. The loaded data and result is as below.

Przechwytywanie.JPG

Lisa_P
Employee
Employee

You are right, I think I might have done a few iterations and lost my history and original logic.

Will need to take another look, sorry for the mistake.

mtucholski
Creator
Creator
Author

No problem, I really apprecieate your time .

I have already half a solution of my problem. I have created two seperate tables and now I need to join them together somehow. But let us wait for your solution because I'm not sure if mine is such efficient if it comes to performance. - and most of all - it doesn't resolve all the problems .Przechwytywanie.JPG

mtucholski
Creator
Creator
Author

Please don't tell me that you give up on me .

If you have no concept on this, maybe we can go further and try to join my tables? I have raw data and a range for sub orders, and now I need to join them together

Lisa_P
Employee
Employee

Hi Mateusz,

Sorry, been busy, you are not forgotten ..

Lisa_P
Employee
Employee

I've got he first part for you, a bit more convoluted before, but is working for me:

Submap:

Mapping Load  order_no&'|'&date&'|'&time&'|'&document as key,

RowNo() as temp_sub_order_no

FROM

(txt, codepage is 1252, embedded labels, delimiter is '\t', msq)

where document='Pw';

Table1:

Load order_no,

     date,

     time,

     document,

     document_no,

     material_code,

     ApplyMap('Submap', key, null()) as sub_order_no;

LOAD order_no&'|'&date&'|'&time&'|'&document as key,

order_no,

     date,

     time,

     document,

     document_no,

     material_code

FROM

(txt, codepage is 1252, embedded labels, delimiter is '\t', msq);


NoConcatenate

New:

Load order_no,

     date,

     time,

     document,

     document_no,

     material_code,

     If( isnull(sub_order_no), peek(sub_order_no),sub_order_no )as sub_order_no

     Resident Table1;

    

     Drop table Table1;