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: 
dching
Contributor II
Contributor II

Calculating Percentage in Qlik Sense load script

Hi Guys 

i have an sql script and my percentage is working perfectly fine but when i transfer this to qlik sense its not working - how can i calculate percentage in load script ?

my sample is below: 

select 1,2,3 from cte1

union all

select 1,2,3 from cte2

union all

select  1,2,    %of sum of cte1 and sum of cte2 -- this is where am struggling - want this in load script as i want it display as a row not column

Labels (5)
3 Replies
Jan-Hendrik
Partner - Contributor III
Partner - Contributor III

Can you provide sample data set for cte1 and cte2 and well as example of what resulting table should look like after calculating %of sum of cte1 and sum of cte2 as a row

dching
Contributor II
Contributor II
Author

select datemonth
,Code = '1'
,ProductCode
,count(distinct Productcode) TotalProducts
from Sales
where statuss = 'active'

union ALL

select datemonth
,Code = '2'
,ProductCode
,count(distinct Productcode) TotalDamaged
from Sales
where statuss = 'damaged'

Union All

select datemonth
,Code = '3' ---% of product damaged
,productcode
,ROUND(CAST(COUNT(distinct TotalDamaged) * 100.0 / COUNT(TotalProducts)AS FLOAT), 2) ----
from Sales
where statuss in ('damaged','active')

-- This works ok in sql and not in Qlik sense 

-- i do not want to create the percentage as an expression want it in the load script 

 

Lakshmanan
Partner - Contributor III
Partner - Contributor III

Hi , 
From your example , 
you can try something like this ,

Table1:
select datemonth
//,Code = '1'
,ProductCode
,count(distinct Productcode) TotalProducts,Datemonth+ProductCode as Key
from Sales
where statuss = 'active'

Left join ( Table1)

Load *;

select datemonth
//,Code = '2'
,ProductCode
,count(distinct Productcode) TotalDamaged,Datemonth+ProductCode as Key
from Sales
where statuss = 'damaged'

 

Left join ( Table1)

Load *, Total Damaged/Total products as %

Resident Table1;