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

WEIGHTED AVERAGE AND IF STATMENT

Need to plot bar chart to compare weighted average hours for 2020 vs 2019 Products.

1) Calculate weighted average hours for2020 Products

2) Calculate weighted average hours for 2019 Products if 2020 Products QTY is greater than 0.

which mean for product E, sum of 2020 QTY is 0. It is excluded from 2019 weighted average.

MonthORDERProduct NameHOURSQTY2019 HPP
Jan1Product A10040
Jan2Product B150250
Jan3Product C200360
Jan4Product D100470
Jan5Product E50010
Jan6Product A200640
Jan7Product B100750
Jan8Product C150260
Jan9Product D200170
Jan10Product E200010

 

Below is the correct value:

2020 AVERAGE HOURS48.19
2019 AVERAGE HOURS55
3 Replies
Ksrinivasan
Specialist
Specialist

hhi,

provide adequate data,

where 2020 data, how you have arrived correct value that 48.19 and 55

ksrinivasan

Catherine_CAS
Contributor
Contributor
Author

Apologies.. missing out the text in the header. I have attached my working excel for your reference.

Ksrinivasan
Specialist
Specialist

hi,

kindly find the result chart and script as below

Ksrinivasan_0-1611028145309.png

 

Script it may workaround solution

cath:
LOAD
Month,
"Product Name",
"2020 HOURS",
"2020 QTY",
"2019 HPP" as "2019 HPP"
FROM [lib://REPORT EXTRACTION TEST/SSSS1.xlsx]
(ooxml, embedded labels, table is cat);
TEMP:
NoConcatenate
LOAD
"Product Name",
SUM("2020 QTY") AS QTY,
SUM("2020 HOURS")/SUM("2020 QTY") AS HOURS_2020,
if(SUM("2020 QTY")>'0',SUM("2019 HPP")/2,'0') AS HOURS_2019
Resident [cath]
GROUP BY "Product Name";
Store TEMP into [lib://REPORT EXTRACTION /TEST.qvd](qvd);
Drop table cath;
drop table TEMP;
Final:
LOAD
'Product' as Product,
"Product Name",
QTY,
HOURS_2020,
HOURS_2019
FROM [lib://REPORT EXTRACTION/TEST.qvd]
(txt, utf8, embedded labels, delimiter is ',', msq)
WHERE NOT MATCH("QTY", '0');

ksrinivasan