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: 
Not applicable

using Aggr to sum distinct values

I need to total a price column on orders as follows:

idsalesmanprice
1tom100
1jim100
2jim1
2sam1
3tom100
3bob100
3joe100
201

ie when totalling we should only sum the price once per order. I tried using:

sum(aggr(price,id)), which does the correct sum but it look like this:

idsalesmanprice
1tom100
1jim0
2jim1
2sam0
3tom100
3bob0
3joe0
201

The sum is correct but I dont want it to zero out the prices like that. Any ideas what formula i can use to make it look like the first table?

Thanks

1 Solution

Accepted Solutions
vgutkovsky
Master II
Master II

See attached solution.

Regards,

View solution in original post

4 Replies
Not applicable
Author

I think this works, so that aggr is only used in the total and doesnt affect the displayed prices in the table:

if(dimensionality()=0,sum(aggr(price,id)),price)

Anyone got a better solution?

johnw
Champion III
Champion III

Possibly sum(aggr(nodistinct price, id),price)), but I bet it messes up the total. If so, no, I'm not thinking of a better solution than yours right now.

vgutkovsky
Master II
Master II

See attached solution.

Regards,

Not applicable
Author

Nice one, thanks.