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: 
sharankv
Contributor
Contributor

Finding the first occurrence of a keyword after an occurrence of another keyword in a list of values in a column

Hi Folks,

I want to find the difference between the order datetime for each order when there is an order for Department "Bags" for first time and followed by the first order in the Department "Apparels". Provided sample dataset below and also attached. The expected output for this dataset is given below. The answer for this question in both script level or chart level are welcomed.

Dataset:

sharankv_0-1706842078023.png

 

Output:

sharankv_1-1706843069295.png

 

Thanks in advance for your time and ansers.

 

 

 

Labels (5)
1 Solution

Accepted Solutions
Pierrick
Partner - Contributor III
Partner - Contributor III

Hi @sharankv,

Here's a script solution to obtain a table equivalent to the one you want

//Find the first order for Bags by Order Number
NoConcatenate
TEMP:
Load
	[Order Number],
    Timestamp(min([Order Date])) as [First Order Date],
    'Bags' as [First Department]
Resident your_data
where [Department]='Bags'
group by [Order Number];

//Include orders for Apparels
left join(TEMP)
Load
	[Order Number],
    [Order Date] as [Second Order Date],
    'Apparels' as [Second Department]
Resident your_data
where [Department]='Apparels';

//Keep orders where Apparels>Bags and take the minimum, you can calculate your time interval at the same time.
NoConcatenate
newDATA:
Load
	[Order Number],
    [First Order Date],
    [First Department],
    Timestamp(min([Second Order Date])) as [Second Order Date],
    [Second Department],
    interval(Timestamp(min([Second Order Date]))-[First Order Date],'mm:ss') as [Time difference]
Resident TEMP
where [Second Order Date]>[First Order Date]
group by [Order Number],[First Order Date],[First Department],[Second Department];
drop table TEMP;

 

Pierrick

View solution in original post

1 Reply
Pierrick
Partner - Contributor III
Partner - Contributor III

Hi @sharankv,

Here's a script solution to obtain a table equivalent to the one you want

//Find the first order for Bags by Order Number
NoConcatenate
TEMP:
Load
	[Order Number],
    Timestamp(min([Order Date])) as [First Order Date],
    'Bags' as [First Department]
Resident your_data
where [Department]='Bags'
group by [Order Number];

//Include orders for Apparels
left join(TEMP)
Load
	[Order Number],
    [Order Date] as [Second Order Date],
    'Apparels' as [Second Department]
Resident your_data
where [Department]='Apparels';

//Keep orders where Apparels>Bags and take the minimum, you can calculate your time interval at the same time.
NoConcatenate
newDATA:
Load
	[Order Number],
    [First Order Date],
    [First Department],
    Timestamp(min([Second Order Date])) as [Second Order Date],
    [Second Department],
    interval(Timestamp(min([Second Order Date]))-[First Order Date],'mm:ss') as [Time difference]
Resident TEMP
where [Second Order Date]>[First Order Date]
group by [Order Number],[First Order Date],[First Department],[Second Department];
drop table TEMP;

 

Pierrick