Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
thanhphongle
Creator II
Creator II

sum(aggr(x), y) with if(z)

Hello Community

these is my table

contract_nocontract_termcontract_term_type
1360
161
162
2480
2121
262
3360
361
362

contract_nofi_acquisition
130000
29000
32000

I want to calculate the weighted average of the contract term where contract_term_type > 0. The weighting should refere to fi_acquisiton

What I tried so far is

if(contract_term_type>0, Sum(Aggr(sum(cost_asquisition/Sum(TOTAL cost_acquisition)*contract_term),contract_no)))

Unfortunetly is does not work

Hope someone can help me.

And is there a way to solve it with Set Analysis Function?

1 Solution

Accepted Solutions
sunny_talwar

Try this

=Sum(Aggr(Sum({<contract_term_type = {'>0'}>}contract_term*fi_acquisition), contract_no))/Sum(fi_acquisition)

View solution in original post

12 Replies
sunny_talwar

How about this

Sum({<contract_term_type = {'>0'}> }Aggr(Sum(contract_term/Sum(TOTAL contract_term)*contract_term), contract_no)))

isorinrusu
Partner - Creator III
Partner - Creator III

Or


Instead of what Sunny says:


sum({<contract_term_type=-{0}>}contract_term)

thanhphongle
Creator II
Creator II
Author

I just tried ur solutions.

QV returns me about 32 as average.

But if I look at the table

contract_no = 1 -> 6 + 6 = 12

contract_no = 2 -> 12 +6 = 18

contract_no = 3 -> 6 +6 = 12
So the weighted average should be at least lower than 18

does maybe the part
Aggr(Sum(contract_term/Sum(TOTAL contract_term)*contract_term), contract_no))

should consider the conditions where contract_type>0?

sunny_talwar

Whoes solution are you talking about?

sunny_talwar

Also, what are you weighting on? Contract_term_type?

thanhphongle
Creator II
Creator II
Author

I was talking about your solution. It should be weighted on contract term

sunny_talwar

That means you just need a simple average then, right? Try this

=Avg(Aggr(Sum({<contract_term_type = {'>0'}>}contract_term), contract_no))

thanhphongle
Creator II
Creator II
Author

oh sorry it should be weighted on the fi_acquisiton. I forgot to mentioned in the table. I reedit the post and added a second table

my weighted function is this:

Sum(Aggr(sum(fi_acquisition/Sum(TOTAL fi_acquisition)*contract_term),contract_no))

sunny_talwar

Give me a final number you look to get based on the above data and the logic used