Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
poortman
Contributor II
Contributor II

Group By

Hi all,

 

I've got a problem with a group by-statement in my script. I want to know the advancement from employees to a cost center. The source-table is as follows:

WorkerCost centerStartdateEnddate
000175055110210-10-201331-03-2014
000175055110201-04-201431-12-2014
000175055110201-01-201530-04-2015
000175055110201-05-201530-04-2016
000175055113701-05-201630-06-2020
000175055110201-07-202031-12-2020
000175055110201-01-2021-

 

TMP:
Load distinct
Medewerker_code                                                                             As Worker,
Kostenplaats                                                                                        As Cost_center,
Date(Date#(Begindatum_functie),'DD-MM-YYYY')                As Startdate,
Date(Date#(Einddatum_functie),'DD-MM-YYYY')                   As Enddate
From [lib://qs_data/HR/HR-app/QVD/PURPLE_AFAS_FUNCTIE_FORMATIEVERDELING.qvd] (qvd)
Where Medewerker_code = '00017505';

I want the output to be like the following table, 

where the field [Days between ]is calculated when the cost center changes, enddate former cost center minus startdate current cost center.

WorkerCost centerStartdateEnddateFormer Cost centerDays between
000175055110210-10-201330-04-2016-1
000175055113701-05-201630-06-2020511021
000175055110201-07-2020-511371

 

 

1 Solution

Accepted Solutions
Taoufiq_Zarra

@poortman  in Script you can use :

Input:
LOAD * INLINE [
    Worker, Cost center, Startdate, Enddate
    00017505, 51102, 10-10-2013, 31-03-2014
    00017505, 51102, 01-04-2014, 31-12-2014
    00017505, 51102, 01-01-2015, 30-04-2015
    00017505, 51102, 01-05-2015, 30-04-2016
    00017505, 51137, 01-05-2016, 30-06-2020
    00017505, 51102, 01-07-2020, 31-12-2020
    00017505, 51102, 01-01-2021, 
];


Tmp:
noconcatenate

load *,rowno() as rowtmp,if(rowno()=1,1,if(peek([Cost center])=[Cost center],peek(IDtmp),peek(IDtmp)+1)) as IDtmp,
if(peek([Cost center])=[Cost center],peek([Former Cost center]),peek([Cost center])) as [Former Cost center],
if(peek([Cost center])<>[Cost center],peek(Enddate),peek(Enddatetmp)) as Enddatetmp
 resident Input;

drop table Input;


output:
noconcatenate
load *,Startdate-Enddatetmp as [Days between];
load Worker,[Cost center],Date(FirstSortedValue(Startdate,rowtmp)) as Startdate, Date(FirstSortedValue(Enddate,-rowtmp)) as Enddate,[Former Cost center],Enddatetmp resident Tmp group by Worker,IDtmp,[Cost center],[Former Cost center],Enddatetmp;

drop table Tmp;
drop fields Enddatetmp;

 

output:

Taoufiq_Zarra_0-1628170667258.png

 

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

3 Replies
Taoufiq_Zarra

@poortman  in Script you can use :

Input:
LOAD * INLINE [
    Worker, Cost center, Startdate, Enddate
    00017505, 51102, 10-10-2013, 31-03-2014
    00017505, 51102, 01-04-2014, 31-12-2014
    00017505, 51102, 01-01-2015, 30-04-2015
    00017505, 51102, 01-05-2015, 30-04-2016
    00017505, 51137, 01-05-2016, 30-06-2020
    00017505, 51102, 01-07-2020, 31-12-2020
    00017505, 51102, 01-01-2021, 
];


Tmp:
noconcatenate

load *,rowno() as rowtmp,if(rowno()=1,1,if(peek([Cost center])=[Cost center],peek(IDtmp),peek(IDtmp)+1)) as IDtmp,
if(peek([Cost center])=[Cost center],peek([Former Cost center]),peek([Cost center])) as [Former Cost center],
if(peek([Cost center])<>[Cost center],peek(Enddate),peek(Enddatetmp)) as Enddatetmp
 resident Input;

drop table Input;


output:
noconcatenate
load *,Startdate-Enddatetmp as [Days between];
load Worker,[Cost center],Date(FirstSortedValue(Startdate,rowtmp)) as Startdate, Date(FirstSortedValue(Enddate,-rowtmp)) as Enddate,[Former Cost center],Enddatetmp resident Tmp group by Worker,IDtmp,[Cost center],[Former Cost center],Enddatetmp;

drop table Tmp;
drop fields Enddatetmp;

 

output:

Taoufiq_Zarra_0-1628170667258.png

 

Regards,
Taoufiq ZARRA

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

(you can mark up to 3 "solutions") 😉
poortman
Contributor II
Contributor II
Author

Thanks Taoufiq, seems to work! 

poortman
Contributor II
Contributor II
Author

@Taoufiq_Zarra Hi Taoufiq (and others), 

Still have a problem when the input is like this:

Input:
LOAD * INLINE [
Worker, Cost center, Startdate, Enddate
00018847, 51104, 01-01-2017, 31-12-2017
00018847, 51104, 01-01-2018, 31-12-2018
00018847, 51104, 01-01-2019, 31-10-2019
00018847, 51104, 01-11-2019, 31-12-2019
00018847, 51102, 01-01-2020, 31-12-2020
00018847, 51104, 01-01-2020, 31-12-2020
00018847, 51102, 01-01-2021, 31-05-2021
00018847, 51104, 01-01-2021, 31-05-2021
00018847, 51102, 01-06-2021,
00018847, 51104, 01-06-2021,
00018847, 51120, 01-06-2021,
];

 

The output is like this:

Worker Cost center Startdate Enddate Former Cost center Days between
00018847 51104 01-01-2017 31-12-2019 - -
00018847 51102 01-01-2020 31-12-2020 51104 1,00000000000000
00018847 51104 01-01-2020 31-12-2020 51102 -365,00000000000000
00018847 51102 01-01-2021 31-05-2021 51104 1,00000000000000
00018847 51104 01-01-2021 31-05-2021 51102 -150,00000000000000
00018847 51102 01-06-2021 - 51104 1,00000000000000
00018847 51104 01-06-2021 - 51102 -
00018847 51120 01-06-2021 - 51104 -

 

But I want it to be like this:

Worker Cost center Startdate Enddate Former Cost center Days between
00018847 51104 01-01-2017 31-12-2019 -  
00018847 51102 01-01-2020 31-12-2020 51104 1,00000000000000
00018847 51104 01-01-2020 31-12-2020    
00018847 51102 01-01-2021 31-05-2021    
00018847 51104 01-01-2021 31-05-2021    
00018847 51102 01-06-2021 -    
00018847 51104 01-06-2021 -    
00018847 51108 01-06-2021 - 51104 1,00000000000000

 

Can you help me get this right?

Thanks in advance,

Niek Poortman