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

Announcements
Qlik Connect 2026 Agenda Now Available: Explore Sessions
cancel
Showing results for 
Search instead for 
Did you mean: 
fkuyTuyt
Contributor II
Contributor II

Set Analysis Aggregation?

Hello, I'm confused about something and need some help, please.

I want to show difference in sales in 2019 vs. 2018, so in my measure expression I have this:

=sum({$<[Usage_data.Date.autoCalendar.Year]={$(vDate1)}>}Qty)-sum({$<[Usage_data.Date.autoCalendar.Year]={$(vDate2)}>}Qty)

Where vDate1 = 2019 and vDate2 = 2018. It works, but if I want to calculate difference in percentages like =sum((A-B)/B), so in this case it would be :

=sum(({$<[Usage_data.Date.autoCalendar.Year]={$(vDate1)}>}Qty)-sum({$<[Usage_data.Date.autoCalendar.Year]={$(vDate2)}>}Qty)/sum({$<[Usage_data.Date.autoCalendar.Year]={$(vDate2)}>}Qty))

Then the calculation returns blanks. What am I missing? Even if I try to do this:

=sum(({$<[Usage_data.Date.autoCalendar.Year]={$(vDate1)}>}Qty)-sum({$<[Usage_data.Date.autoCalendar.Year]={$(vDate2)}>}Qty)/2)

It returns blanks. Also, when I do:

=sum({$<[Usage_data.Date.autoCalendar.Year]={$(vDate1)}>}Qty)) <- notice second right parenthesis, which is an error, the expression editor doesn't indicate an error, but the result of this calculation is 0. Is it a bug? It should indicate an error. 

Second question:

Is there a more elegant way to calculate the above, e.g. difference in sales in 2019 and 2018? 

 

3 Replies
Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

Try

=sum({$<[Usage_data.Date.autoCalendar.Year]={$(vDate1)}>}Qty)/sum({$<[Usage_data.Date.autoCalendar.Year]={$(vDate2)}>}Qty)-1


talk is cheap, supply exceeds demand
fkuyTuyt
Contributor II
Contributor II
Author

I had to switch vDate1 to vDate2 and vDate2 to vDate1, but it works as expected. Thanks a lot. Can you explain why your approach works and mine doesn't? 

Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

You're nesting sums which in this case is simply wrong. You don't want to calculate a sum of sums. You simply want the variance of two different sums.

If you ever want to do a sum of sums you also need to use the aggr function to specify the set of fields over which the outer sum should aggregate the values.


talk is cheap, supply exceeds demand