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

Announcements
Qlik GA: Multivariate Time Series in Qlik Predict: Get Details
cancel
Showing results for 
Search instead for 
Did you mean: 
Sai33
Partner - Creator
Partner - Creator

Next Row as New Column and Last of a Column

Hello All,

I have my Source data in the following format

IDLocEntryExit
1A12.01.202013.01.2020
1B14.01.202016.01.2020
1C17.01.202019.01.2020

 

Expected Result

IDLocEntryExitNext LocLast Loc
1A12.01.202013.01.2020BC
1B14.01.202016.01.2020CC
1C17.01.202019.01.2020 C

 

I have tried using Peek and Previous functions for the Next Loc. But, wasn't somehow getting the desired result. And not sure how to deal with the last Value.

Any pointers would be highly useful.

TIA!

Labels (1)
1 Solution

Accepted Solutions
Taoufiq_Zarra

I just took a break 😁

 

Data0:
LOAD * INLINE [
ID,Loc,Entry,Exit
1,A,12.01.2020,13.01.2020
1,B,14.01.2020,16.01.2020
2,R,12.01.2020,13.01.2020
1,C,17.01.2020,19.01.2020
2 ,T,14.01.2020,16.01.2020
3 ,K,14.01.2020,16.01.2020
];


Data:
noconcatenate
LOAD rowNo() as IDtmp,* resident Data0 order by ID;

drop table Data0;
tmp:
noconcatenate
LOAD ID,FirstSortedValue(Loc,-IDtmp) as LastLoc Resident Data Group By ID; 

join load * resident Data order by ID;
drop table Data ;

tmp2:
noconcatenate
load * resident tmp  order by IDtmp DESC;

drop table tmp;

output:
load
*,
if(ID=Previous(ID),Previous(Loc)) as "Next Loc"
resident tmp2;
right join 
load * resident tmp2;

drop table tmp2;

 

 

output:

Capture.JPG

 

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉

View solution in original post

10 Replies
Taoufiq_Zarra

maye be :

 

Data:
LOAD rowNo() as IDtmp,* INLINE [
    ID, Loc, Entry, Exit
    1, A, 12.01.2020, 13.01.2020
    1, B, 14.01.2020, 16.01.2020
    1, C, 17.01.2020, 19.01.2020
];

FirstSortedValue:

LOAD FirstSortedValue(Loc,-IDtmp) as LastLoc Resident Data Group By ID; 

LET LastLoc = peek('LastLoc');

drop table FirstSortedValue;

temps2:
noconcatenate
load *,'$(LastLoc)' as "Last Loc" resident Data order by IDtmp DESC;

drop table Data;

Result:
noconcatenate
load 
*,
Previous(Loc) as "Next Loc"
resident temps2;

drop table temps2;
drop field IDtmp;

 

output:

Capture.JPG

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
Sai33
Partner - Creator
Partner - Creator
Author

Hi Taoufiq

This is working fine when there is only one ID as in my Example data. But, when there are multiple IDs it's always condisdering the Last LOC it should instead consider the Last LOC of the particular ID.

Sai33
Partner - Creator
Partner - Creator
Author

IDLocEntryExit
1A12.01.202013.01.2020
1B14.01.202016.01.2020
1C17.01.202019.01.2020
2R12.01.202013.01.2020
T14.01.202016.01.2020

 

IDLocEntryExitNext LocLast Loc
1A12.01.202013.01.2020BC
1B14.01.202016.01.2020CC
1C17.01.202019.01.2020 C
2R12.01.202013.01.2020TT
2T14.01.202016.01.2020 T
Taoufiq_Zarra

Yes, I'm waiting for that remark 😁  You're right

can you share a sample data with # ID and the result

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
Sai33
Partner - Creator
Partner - Creator
Author

Updated my Source data and Required Solution

Sai33
Partner - Creator
Partner - Creator
Author

Thanks for your prompt reply. Let me know if the updated Sample data is enough for you to test. Or else i will create a test qvw and attach here.

But, the example data i have updated is exactly how my data looks like.

Sai33
Partner - Creator
Partner - Creator
Author

Please, find the attached qvf along with Sample data and Expected result

Taoufiq_Zarra

I just took a break 😁

 

Data0:
LOAD * INLINE [
ID,Loc,Entry,Exit
1,A,12.01.2020,13.01.2020
1,B,14.01.2020,16.01.2020
2,R,12.01.2020,13.01.2020
1,C,17.01.2020,19.01.2020
2 ,T,14.01.2020,16.01.2020
3 ,K,14.01.2020,16.01.2020
];


Data:
noconcatenate
LOAD rowNo() as IDtmp,* resident Data0 order by ID;

drop table Data0;
tmp:
noconcatenate
LOAD ID,FirstSortedValue(Loc,-IDtmp) as LastLoc Resident Data Group By ID; 

join load * resident Data order by ID;
drop table Data ;

tmp2:
noconcatenate
load * resident tmp  order by IDtmp DESC;

drop table tmp;

output:
load
*,
if(ID=Previous(ID),Previous(Loc)) as "Next Loc"
resident tmp2;
right join 
load * resident tmp2;

drop table tmp2;

 

 

output:

Capture.JPG

 

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
Taoufiq_Zarra

Have you tested the script ?

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉