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

Help me to derive some logic to build the one report.

Hi All,

I want to display distinct combination (customer + country+branch) of records in my report table.  Please find my table and expected output. Please help me to derive logic. 

- Display the all the distinct records (key group of customer+country+branch) irrespective of timestamp.

- Display the maximum date record if the key group is same on particular day.

- Customer can repeat on different days and we can display in report.

- TOTAL values need to be added on that particular day, if the INPUT column value and OUTPUT column value is different on the maximum date record .

- TOTAL value need to be displayed on the TOTAL cell as it is, If the INPUT value and OUTPUT value is same.

I have covered all scenarios in below tables. both main table and expected output. please help me with this complex requirement.

My Table:

CUSTOMERCOUNTRYBRANCHINPUTOUTPUTTOTALDATE
276GB 572919/13/2019 10:00:58 AM
276GBGB-HR572919/13/2019 10:31:23 AM
276GBGB-HR572919/13/2019 10:51:23 AM
276GBGB-HR572719/16/2019 10:46:13 AM
276GBGB-HS122919/16/2019 10:50:48 AM
276GBGB-HR572959/30/2019 10:11:58 AM
276GBGB-HR571539/30/2019 10:20:58 AM
9001ININ-HS512110/30/2019 8:00:18 AM
9001ININ-HS1112210/30/2019 9:00:18 AM
9001ININ-HS1114210/30/2019 9:30:18 AM
9001ININ-HS1012310/30/2019 10:00:18 AM
9001ININ-HS1114211/30/2019 11:30:18 AM
9001INNZ-BB5730510/30/2019 10:20:58 AM
9001INNZ-BB5757510/30/2019 11:30:48 AM

 

Expected Output:

CUSTOMERCOUNTRYBRANCHINPUTOUTPUTTOTALDATE
276GB 572919/13/2019 10:00:58 AM
276GBGB-HR572919/13/2019 10:51:23 AM
276GBGB-HR572719/16/2019 10:46:13 AM
276GBGB-HS122919/16/2019 10:50:48 AM
276GBGB-HR571589/30/2019 10:20:58 AM
9001ININ-HS1012810/30/2019 10:00:18 AM
9001ININ-HS1114211/30/2019 11:30:18 AM
9001INNZ-BB57571010/30/2019 11:30:48 AM

 

Thanks in advance. 

Labels (3)
1 Solution

Accepted Solutions
Saravanan_Desingh

Please check.

tab1:
LOAD *, Date(Floor(DATE)) As tmpDt INLINE [
    CUSTOMER, COUNTRY, BRANCH, INPUT, OUTPUT, TOTAL, DATE
    276, GB,  , 57, 29, 1, 9/13/2019 10:00:58 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:31:23 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:51:23 AM
    276, GB, GB-HR, 57, 27, 1, 9/16/2019 10:46:13 AM
    276, GB, GB-HS, 12, 29, 1, 9/16/2019 10:50:48 AM
    276, GB, GB-HR, 57, 29, 5, 9/30/2019 10:11:58 AM
    276, GB, GB-HR, 57, 15, 3, 9/30/2019 10:20:58 AM
    9001, IN, IN-HS, 5, 12, 1, 10/30/2019 8:00:18 AM
    9001, IN, IN-HS, 11, 12, 2, 10/30/2019 9:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 10/30/2019 9:30:18 AM
    9001, IN, IN-HS, 10, 12, 3, 10/30/2019 10:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 11/30/2019 11:30:18 AM
    9001, IN, NZ-BB, 57, 30, 5, 10/30/2019 10:20:58 AM
    9001, IN, NZ-BB, 57, 57, 5, 10/30/2019 11:30:48 AM
];

Right Join(tab1)
LOAD CUSTOMER, COUNTRY, BRANCH,  tmpDt, Timestamp(Max(DATE)) As DATE, 		If((Count(DISTINCT INPUT)=1 And Count(DISTINCT OUTPUT)=1) Or Index(Concat(DISTINCT INPUT=OUTPUT,','),'-1')>0, Only(TOTAL), Sum(TOTAL)) As NewTOTAL
Resident tab1
Group By CUSTOMER, COUNTRY, BRANCH, tmpDt
;

Drop Field tmpDt;

commQV45.PNG

View solution in original post

7 Replies
Saravanan_Desingh

Can u check this?

tab1:
LOAD *, Date(Floor(DATE)) As tmpDt INLINE [
    CUSTOMER, COUNTRY, BRANCH, INPUT, OUTPUT, TOTAL, DATE
    276, GB,  , 57, 29, 1, 9/13/2019 10:00:58 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:31:23 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:51:23 AM
    276, GB, GB-HR, 57, 27, 1, 9/16/2019 10:46:13 AM
    276, GB, GB-HS, 12, 29, 1, 9/16/2019 10:50:48 AM
    276, GB, GB-HR, 57, 29, 5, 9/30/2019 10:11:58 AM
    276, GB, GB-HR, 57, 15, 3, 9/30/2019 10:20:58 AM
    9001, IN, IN-HS, 5, 12, 1, 10/30/2019 8:00:18 AM
    9001, IN, IN-HS, 11, 12, 2, 10/30/2019 9:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 10/30/2019 9:30:18 AM
    9001, IN, IN-HS, 10, 12, 3, 10/30/2019 10:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 11/30/2019 11:30:18 AM
    9001, IN, NZ-BB, 57, 30, 5, 10/30/2019 10:20:58 AM
    9001, IN, NZ-BB, 57, 57, 5, 10/30/2019 11:30:48 AM
];

Right Join(tab1)
LOAD CUSTOMER, COUNTRY, BRANCH,  tmpDt, Timestamp(Max(DATE)) As DATE
Resident tab1
Group By CUSTOMER, COUNTRY, BRANCH, tmpDt//, INPUT, OUTPUT, TOTAL
;

Drop Field tmpDt;

commQV43.PNG

govind1501
Contributor III
Contributor III
Author

Thanks for response saran. One scenario is not covered, ie TOTAL.  Whenever INPUT AND OUTPUT value is same then we can display TOTAL as it is, but whenever INPUT AND OUTPUT value is different, then need to add the TOTAL values in that particular day.  Please find the expected output to cover these scenarios. 

Here total added of records on that particular day if INPUT AND OUTPUT is different in main table (ex: first two records TOTAL)

Second record TOTAL displayed as it is as INPUT AND OUTPUT is same.

CUSTOMERCOUNTRYBRANCHINPUTOUTPUTTOTALDATE
276GBGB-HR5715809/30/2019 10:20:58
9001ININ-HS1012810/30/2019 10:00:18
9001INNZ-BB5757510/30/2019 11:30:48
Saravanan_Desingh

Please check.

tab1:
LOAD *, Date(Floor(DATE)) As tmpDt INLINE [
    CUSTOMER, COUNTRY, BRANCH, INPUT, OUTPUT, TOTAL, DATE
    276, GB,  , 57, 29, 1, 9/13/2019 10:00:58 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:31:23 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:51:23 AM
    276, GB, GB-HR, 57, 27, 1, 9/16/2019 10:46:13 AM
    276, GB, GB-HS, 12, 29, 1, 9/16/2019 10:50:48 AM
    276, GB, GB-HR, 57, 29, 5, 9/30/2019 10:11:58 AM
    276, GB, GB-HR, 57, 15, 3, 9/30/2019 10:20:58 AM
    9001, IN, IN-HS, 5, 12, 1, 10/30/2019 8:00:18 AM
    9001, IN, IN-HS, 11, 12, 2, 10/30/2019 9:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 10/30/2019 9:30:18 AM
    9001, IN, IN-HS, 10, 12, 3, 10/30/2019 10:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 11/30/2019 11:30:18 AM
    9001, IN, NZ-BB, 57, 30, 5, 10/30/2019 10:20:58 AM
    9001, IN, NZ-BB, 57, 57, 5, 10/30/2019 11:30:48 AM
];

Right Join(tab1)
LOAD CUSTOMER, COUNTRY, BRANCH,  tmpDt, Timestamp(Max(DATE)) As DATE, 		If(Not(Count(DISTINCT INPUT)=1 And Count(DISTINCT OUTPUT)=1), Sum(TOTAL), Only(TOTAL)) As NewTOTAL
Resident tab1
Group By CUSTOMER, COUNTRY, BRANCH, tmpDt
;

Drop Field tmpDt;

commQV44.PNG

govind1501
Contributor III
Contributor III
Author

Saran - You are there to output. Perfect. one thing missed. Please advise

No need to add only if INPUT and OUTPUT is same. In below case no need to add TOTAL as INPUT and OUTPUT is same.

Main Table Records:

CUSTOMERCOUNTRYBRANCHINPUTOUTPUTTOTALDATE
9001INNZ-BB5730510/30/2019 10:20:58
9001INNZ-BB5757510/30/2019 11:30:48

 

Expected Output: Total not added here. In our response it is showing 10.

CUSTOMERCOUNTRYBRANCHINPUTOUTPUTTOTALDATE
9001INNZ-BB5757510/30/2019 11:30:48
Saravanan_Desingh

Please check.

tab1:
LOAD *, Date(Floor(DATE)) As tmpDt INLINE [
    CUSTOMER, COUNTRY, BRANCH, INPUT, OUTPUT, TOTAL, DATE
    276, GB,  , 57, 29, 1, 9/13/2019 10:00:58 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:31:23 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:51:23 AM
    276, GB, GB-HR, 57, 27, 1, 9/16/2019 10:46:13 AM
    276, GB, GB-HS, 12, 29, 1, 9/16/2019 10:50:48 AM
    276, GB, GB-HR, 57, 29, 5, 9/30/2019 10:11:58 AM
    276, GB, GB-HR, 57, 15, 3, 9/30/2019 10:20:58 AM
    9001, IN, IN-HS, 5, 12, 1, 10/30/2019 8:00:18 AM
    9001, IN, IN-HS, 11, 12, 2, 10/30/2019 9:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 10/30/2019 9:30:18 AM
    9001, IN, IN-HS, 10, 12, 3, 10/30/2019 10:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 11/30/2019 11:30:18 AM
    9001, IN, NZ-BB, 57, 30, 5, 10/30/2019 10:20:58 AM
    9001, IN, NZ-BB, 57, 57, 5, 10/30/2019 11:30:48 AM
];

Right Join(tab1)
LOAD CUSTOMER, COUNTRY, BRANCH,  tmpDt, Timestamp(Max(DATE)) As DATE, 		If((Count(DISTINCT INPUT)=1 And Count(DISTINCT OUTPUT)=1) Or Index(Concat(DISTINCT INPUT=OUTPUT,','),'-1')>0, Only(TOTAL), Sum(TOTAL)) As NewTOTAL
Resident tab1
Group By CUSTOMER, COUNTRY, BRANCH, tmpDt
;

Drop Field tmpDt;

commQV45.PNG

Saravanan_Desingh

Check this.

tab1:
LOAD *, Date(Floor(DATE)) As tmpDt INLINE [
    CUSTOMER, COUNTRY, BRANCH, INPUT, OUTPUT, TOTAL, DATE
    276, GB,  , 57, 29, 1, 9/13/2019 10:00:58 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:31:23 AM
    276, GB, GB-HR, 57, 29, 1, 9/13/2019 10:51:23 AM
    276, GB, GB-HR, 57, 27, 1, 9/16/2019 10:46:13 AM
    276, GB, GB-HS, 12, 29, 1, 9/16/2019 10:50:48 AM
    276, GB, GB-HR, 57, 29, 5, 9/30/2019 10:11:58 AM
    276, GB, GB-HR, 57, 15, 3, 9/30/2019 10:20:58 AM
    9001, IN, IN-HS, 5, 12, 1, 10/30/2019 8:00:18 AM
    9001, IN, IN-HS, 11, 12, 2, 10/30/2019 9:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 10/30/2019 9:30:18 AM
    9001, IN, IN-HS, 10, 12, 3, 10/30/2019 10:00:18 AM
    9001, IN, IN-HS, 11, 14, 2, 11/30/2019 11:30:18 AM
    9001, IN, NZ-BB, 57, 30, 5, 10/30/2019 10:20:58 AM
    9001, IN, NZ-BB, 57, 57, 5, 10/30/2019 11:30:48 AM
];

Right Join(tab1)
LOAD CUSTOMER, COUNTRY, BRANCH,  tmpDt, Min(INPUT) As Min_INPUT, Max(INPUT) As Max_INPUT
Resident tab1
Group By CUSTOMER, COUNTRY, BRANCH, tmpDt
;

tab2:
NoConcatenate
LOAD * Resident tab1;
Right Join(tab2)
LOAD CUSTOMER, COUNTRY, BRANCH,  tmpDt, Timestamp(Max(DATE)) As DATE, Count(DISTINCT INPUT) As iCnt,
		Count(DISTINCT OUTPUT) As oCnt, Index(Concat(DISTINCT INPUT=OUTPUT,','),'-1')>0 As ioCat,
		If((Count(DISTINCT INPUT)=1 And Count(DISTINCT OUTPUT)=1) Or Index(Concat(DISTINCT INPUT=OUTPUT,','),'-1')>0, Only(TOTAL), Sum([TOTAL])) As NewTOTAL,
		If(Concat(DISTINCT Min_INPUT=Max_INPUT,',')='-1', 'Y') As INPUT_Flag
Resident tab1
Group By CUSTOMER, COUNTRY, BRANCH, tmpDt
;

tab3:
NoConcatenate
LOAD *, If(INPUT_Flag='Y',INPUT, Min_INPUT&':'&INPUT) As NewINPUT
Resident tab2
;

Drop Field tmpDt;
DROP Table tab1, tab2;

commQV48.PNG

govind1501
Contributor III
Contributor III
Author

Just found one bug on below case. We need to display the min value based on the timestamp on particular day. (here 5 correct) but if we change value from 5 to 30 in inline load then it should show 30:10 but in our case, we are getting 10:10. In the same way max value also. As we already getting the max records on particular that is fine. Min value need to corrected.

CUSTOMERCOUNTRYBRANCHOUTPUTDATEINPUTMin_INPUTMax_INPUTNewINPUTNewTOTAL
276GB 299/13/2019 10:00:58 AM575757571
276GBGB-HR159/30/2019 10:20:58 AM575757578
276GBGB-HR279/16/2019 10:46:13 AM575757571
276GBGB-HR299/13/2019 10:51:23 AM575757571
276GBGB-HS299/16/2019 10:50:48 AM121212121
9001ININ-HS1210/30/2019 10:00:18 AM10103010:108
9001ININ-HS1411/30/2019 11:30:18 AM111111112
9001INNZ-BB5710/30/2019 11:30:48 AM575757575