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

Data arrangement without using looping

Hi,

I've two tables as shown below.

   

Table1
ID1ID2Cycle Start DateCycle End DatePay Date
12317/14/188/15/189/1/18
12318/16/189/14/1810/1/18
12319/15/1810/15/1811/1/18
123110/16/1811/15/1812/1/18
123111/16/1812/14/181/1/19
123112/15/181/15/192/1/19

   

Table2
ID1ID2Flag Start DateFlag End Date
12317/18/20187/20/2018
12317/22/20188/5/2018
12318/11/20188/17/2018
12319/14/20189/14/2018
12319/15/20189/15/2018
12319/17/201812/1/2018
12319/17/201810/15/2018
12319/17/201810/16/2018
12319/17/201811/8/2018
12319/17/201811/15/2018
12317/18/201811/20/2018

The logic condition that will provide two data point for each line of Table 2 using fields from table 1 is provided below and it will provide values for each rows of Table2 as follows

   

First Pay Date ImpactedLast Pay Date Impacted
Flag Start Date >= Cycle Start Date
AND
Flag Start Date <= Cycle End Date
Flag End Date >= Cycle Start Date
AND
Flag End Date <= Cycle End Date
All Pay Date Months Between First and Last Pay Date Impacted
9/1/20189/1/20189/1/2018
9/1/20189/1/20189/1/2018
9/1/201810/1/20189/1/2018
10/1/2018
10/1/201810/1/201810/1/2018
11/1/201811/1/201811/1/2018
11/1/20181/1/201911/1/2018
12/1/2018
1/1/2019
11/1/201811/1/201811/1/2018
11/1/201812/1/201811/1/2018
12/1/2018
11/1/201812/1/201811/1/2018
12/1/2018
11/1/201812/1/201811/1/2018
12/1/2018
9/1/20181/1/20199/1/2018
10/1/2018
11/1/2018
12/1/2018
1/1/2019

Expected output

Values of "All Pay Date Months Between First and Last Pay Date Impacted" of each row from above table and table2 will provide Pay Date Column in output

   

ID1ID2Flag Start DateFlag End DatePay Date
12317/18/20187/20/20189/1/2018
12317/22/20188/5/20189/1/2018
12318/11/20188/17/20189/1/2018
12318/11/20188/17/201810/1/2018
12319/14/20189/14/201810/1/2018
12319/15/20189/15/201811/1/2018
12319/17/201812/1/201811/1/2018
12319/17/201812/1/201812/1/2018
12319/17/201812/1/20181/1/2019
12319/17/201810/15/201811/1/2018
12319/17/201810/16/201811/1/2018
12319/17/201810/16/201812/1/2018
12319/17/201811/8/201811/1/2018
12319/17/201811/8/201812/1/2018
12319/17/201811/15/201811/1/2018
12319/17/201811/15/201812/1/2018
12317/18/201811/20/20189/1/2018
12317/18/201811/20/201810/1/2018
12317/18/201811/20/201811/1/2018
12317/18/201811/20/201812/1/2018
12317/18/201811/20/20181/1/2019

I don't want to use for loop in the script.

Any help/guidance will be very much appreciated.

BR,

Vijay

1 Solution

Accepted Solutions
Peter_Cammaert
Partner - Champion III
Partner - Champion III

Something like this:

LEFT JOIN (Table2)

INTERVALMATCH ([Flag Start Date], ID1, ID2)

LOAD [Cycle Start Date], [Cycle End Date], ID1, ID2

RESIDENT Table1;

LEFT JOIN (Table2)

LOAD ID1, ID2, [Cycle Start Date], [Cycle End Date], [Pay Date] AS [First Pay Date Impacted]

RESIDENT Table1;

DROP Fields [Cycle Start Date], [Cycle End Date] FROM Table2;

LEFT JOIN (Table2)

INTERVALMATCH ([Flag End Date], ID1, ID2)

LOAD [Cycle Start Date], [Cycle End Date], ID1, ID2

RESIDENT Table1;

LEFT JOIN (Table2)

LOAD ID1, ID2, [Cycle Start Date], [Cycle End Date], [Pay Date] AS [Last Pay Date Impacted]

RESIDENT Table1;

DROP Fields [Cycle Start Date], [Cycle End Date] FROM Table2;

DROP Table Table1;

Table3:

LOAD ID1, ID2, [Flag Start Date], [Flag End Date],

     AddMonths([First Pay Date Impacted], IterNo()-1) AS [Pay Date]

RESIDENT Table2

WHILE AddMonths([First Pay Date Impacted], IterNo()-1) <= [Last Pay Date Impacted];

DROP Table Table2;

produces output like that:

Data arrangement without looping thread307221.jpg

View solution in original post

6 Replies
Peter_Cammaert
Partner - Champion III
Partner - Champion III

Something like this:

LEFT JOIN (Table2)

INTERVALMATCH ([Flag Start Date], ID1, ID2)

LOAD [Cycle Start Date], [Cycle End Date], ID1, ID2

RESIDENT Table1;

LEFT JOIN (Table2)

LOAD ID1, ID2, [Cycle Start Date], [Cycle End Date], [Pay Date] AS [First Pay Date Impacted]

RESIDENT Table1;

DROP Fields [Cycle Start Date], [Cycle End Date] FROM Table2;

LEFT JOIN (Table2)

INTERVALMATCH ([Flag End Date], ID1, ID2)

LOAD [Cycle Start Date], [Cycle End Date], ID1, ID2

RESIDENT Table1;

LEFT JOIN (Table2)

LOAD ID1, ID2, [Cycle Start Date], [Cycle End Date], [Pay Date] AS [Last Pay Date Impacted]

RESIDENT Table1;

DROP Fields [Cycle Start Date], [Cycle End Date] FROM Table2;

DROP Table Table1;

Table3:

LOAD ID1, ID2, [Flag Start Date], [Flag End Date],

     AddMonths([First Pay Date Impacted], IterNo()-1) AS [Pay Date]

RESIDENT Table2

WHILE AddMonths([First Pay Date Impacted], IterNo()-1) <= [Last Pay Date Impacted];

DROP Table Table2;

produces output like that:

Data arrangement without looping thread307221.jpg

vvira1316
Specialist II
Specialist II
Author

Hi Peter,

Thanks. I'll check it out.

BR,

Vijay

vvira1316
Specialist II
Specialist II
Author

Hi Peter,

It is mostly working but there are couple of data scenario where it is not providing records that I would expect. I'm attaching my sample data and qvw file for your reference.

In one of the two scenario not sure it is interval match or addmonths is causing the issue. This is when year of Flag Start Date and Flag End Date are different. (ID1 = 23106, 23107, 23108, 23109)

The other scenario is when Flag Start Date = Cycle End Date (ID1 = 24465) (Result Verification tab in the attached sample file)

Thanks & Regards,

Vijay

it is the interval match that is causing the problem

IntervalMatchIssue.PNG

I inserted a record with 12/1/2017 Pay Date for 23106, 23107, 23108, 23109 and it gave me missing records that I was looking for.

IntervalMatchIssue2.PNG

inserting records in table 1 is not the solution, so we need to see how we can resolve.

vvira1316
Specialist II
Specialist II
Author

Hi,

I would like to change the interval check (condition logic)

Cycle Start Date to fall within Flag Start Date and Flag End Date to retrieve each Pay Date and Similarly Cycle End Date to fall within Flag Start Date and Flag End Date to retrieve each Pay Date thus it will/should provide intermediate table as follows

   

Intermediate Table
ID1ID2ID3ID4Flag Start DateFlagged Start ByFlag End DateFlagged End ByPay DateComing From
Not a Column in the intermediate table
231067522223311/2/17 4:18 PME10244961/17/18 11:05 AME30692231/1/2018Cycle Start Date Check
231067522223311/2/17 4:18 PME10244961/17/18 11:05 AME30692232/1/2018Cycle Start Date Check
231067522223311/2/17 4:18 PME10244961/17/18 11:05 AME30692233/1/2018Cycle Start Date Check
231067522223311/2/17 4:18 PME10244961/17/18 11:05 AME30692231/1/2018Cycle End Date Check
231067522223311/2/17 4:18 PME10244961/17/18 11:05 AME30692232/1/2018Cycle End Date Check
23106752222331/17/18 11:27 AME30692231/18/18 11:59 AME30692233/1/2018Cycle Start Date Check
23106752222331/17/18 11:27 AME30692231/18/18 11:59 AME30692233/1/2018Cycle End Date Check

   

Final Table from Intermediate Table - This will be distinct records from Intermediate Table
ID1ID2ID3ID4Flag Start DateFlagged Start ByFlag End DateFlagged End ByPay Date
231067522223311/2/17 4:18 PME10244961/17/18 11:05 AME30692231/1/2018
231067522223311/2/17 4:18 PME10244961/17/18 11:05 AME30692232/1/2018
231067522223311/2/17 4:18 PME10244961/17/18 11:05 AME30692233/1/2018
23106752222331/17/18 11:27 AME30692231/18/18 11:59 AME30692233/1/2018
vvira1316
Specialist II
Specialist II
Author

Hi Peter,

Your code definitely helped me. I'm thankful for your help. The other issue was related to particular data scenario. I was able to code differently to create those records. Combining records from your code and other logic provided me all the records I was looking for.

I'll be marking your solution correct as it is providing the output as per expectation of original request.

BR,

Vijay

Peter_Cammaert
Partner - Champion III
Partner - Champion III

Well done!

Sorry for the delay, but I didn't have much free time to investigate your remarks. I'm glad you were able to adapt the generic logic to your situation.