Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
anwarbham
Contributor III
Contributor III

Peek Function multiple rows

 

Hi has anyone used the Peek function to compare multiple rows.

i have the table below i am trying to generate the StatusFlag column with 1 for each row that has the week number greater than the MIN(WeekNum) in a series. where all the ID1, ID2, ID3 are the same .

it works if there are only 2 rows but if the number of rows is more than 2 in a group it does not work .

 

this is an example where i get the result i want 

ID1Date1WeekNumID2ID3ID4StatusFlag
a3G0W0000045boaUAA03-feb-206005d0000002dP4qAAE001d0000021DjpNAASa040W00001tZNdEQAW0
a3G0W0000045boaUAA06-mar-2010005d0000002dP4qAAE001d0000021DjpNAASa040W00001u5VL6QAM1

  

in this group because we have multiple rows in the group the result is incorrect week 26 should have StatusFlag 0 a the rest should be 1

ID1Date1WeekNumID2ID3ID4StatusFlag
a3G0W000004KeYpUAK2020-06-2626005d00000057VbrAAE001d000001wKV8HAAWa040W00001vCXzXQAW1
a3G0W000004KeYpUAK2020-07-0127005d00000057VbrAAE001d000001wKV8HAAWa040W00001vChDhQAK1
a3G0W000004KeYpUAK2020-07-1329005d00000057VbrAAE001d000001wKV8HAAWa040W00001vEef1QAC1
a3G0W000004KeYpUAK2020-07-2230005d00000057VbrAAE001d000001wKV8HAAWa040W00001vEZxBQAW0
a3G0W000004KeYpUAK2020-08-0332005d00000057VbrAAE001d000001wKV8HAAWa040W00001tHFaaQAG0
a3G0W000004KeYpUAK2020-08-0732005d00000057VbrAAE001d000001wKV8HAAWa040W00001tHoctQAC0
a3G0W000004KeYpUAK2020-08-1033005d00000057VbrAAE001d000001wKV8HAAWa040W00001tHDpyQAG1
a3G0W000004KeYpUAK2020-08-2635005d00000057VbrAAE001d000001wKV8HAAWa040W00001vpnb9QAA1

 

 

$(vTable)_Tmp2:
LOAD
ID1,
Date1,
WeekNum ,
ID2,
ID3,
ID4,
if( WeekNum > peek(WeekNum, RowNo(), 'Adherance_Tmp1') , 1,0) as StatusFlag
Resident
$(vTable)_Tmp1
Order By
WeekNum 
;

 

 

Labels (2)
1 Solution

Accepted Solutions
Ksrinivasan
Specialist
Specialist

hi,

try this

hhh:
LOAD
ID1,
Date1,
WeekNum,
ID2,
ID3,
ID4
FROM [lib:/SSSS1.xlsx]
(ooxml, embedded labels, table is ddiiss);
Join
Load ID1,
Max(Date1) as Date1,
1 as flag
Resident hhh
Group by ID1,;
NOConcatenate
TEST:
Load
ID1,
Date1,
WeekNum,
ID2,
ID3,
ID4,
if(flag = '1' , '1' ,'0') as statys
Resident hhh
Order by ID1,WeekNum;
drop Table hhh;

 

ksrinivasan.

View solution in original post

2 Replies
Ksrinivasan
Specialist
Specialist

hi,

try this

hhh:
LOAD
ID1,
Date1,
WeekNum,
ID2,
ID3,
ID4
FROM [lib:/SSSS1.xlsx]
(ooxml, embedded labels, table is ddiiss);
Join
Load ID1,
Max(Date1) as Date1,
1 as flag
Resident hhh
Group by ID1,;
NOConcatenate
TEST:
Load
ID1,
Date1,
WeekNum,
ID2,
ID3,
ID4,
if(flag = '1' , '1' ,'0') as statys
Resident hhh
Order by ID1,WeekNum;
drop Table hhh;

 

ksrinivasan.

anwarbham
Contributor III
Contributor III
Author

Thanks @Ksrinivasan 

i managed use your code like this and got the result i needed

 

 

$(vTable)_Tmp2:
LOAD
ID1,
Date1,
WeekNum,
ID2,
ID3,
ID4
Resident
$(vTable)_Tmp1;

Join
Load 
ID1,
ID2,
ID3,
Min(WeekNum) as WeekNum,
1 as flag
Resident $(vTable)_Tmp2
Group by ID1,ID2,
ID3;

NOConcatenate
$(vTable)_Tmp3:
Load
ID1,
Date1,
WeekNum,
ID2,
ID3,
ID4,
if(flag = '1' , '0' ,'1') as statys
Resident $(vTable)_Tmp2
Order by ID1,WeekNum;
drop Table $(vTable)_Tmp2;