Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
thkarner
Partner - Creator III
Partner - Creator III

Sum(A) + Sum(B) or Sum(A+B)

Hi,

a quick one, where I´m uncertain:

I simply want to sum up two fields inculding (same) set analysis. Normally I do it in this way:

sum( {<[Date.Cal.Year]={2017}>} [FieldA]) + sum( {<[Date.Cal.Year]={2017}>} [FieldB])

Now I saw that this also works:

sum( {<[Date.Cal.Year]={2017}>} ([FieldA] + [FieldB]))

FieldA and FieldB are both from the same (facts) table.

Due to I have a very large data set and complex nested formulas I´d prefer the second version, but I´m uncertain if there could be any differences in the result.

Can anybody confirm whether the two versions are identical or otherwise outline the difference?

Thanks, Thomas

20 Replies
kenphamvn
Creator III
Creator III

Hi

i think the second solution is bester

but if FieldA and FieldB are both from the same (facts) table you can pre caculate in script load data

Load [FieldA] + [FieldB] as TOTAL

from table

size app will increase but performance is best

thkarner
Partner - Creator III
Partner - Creator III
Author

Thanks. I know about pre-calculation in the script, which is not applicable in my case for some reasons.

Could you (or anybody else) confirm doubtless if the 2 calculations would lead to same result?

lakshmikandh
Specialist II
Specialist II

Both has given same results in my app.

Anonymous
Not applicable

if both fields comes from same table the result should be same for both calculations

Not applicable

Hi Thomas,

If both columns have any numeric values then the result will be same. But if there is a possibility that any of the column can have NULL/Non numeric values then I will advise the first method for accuracy.

tresesco
MVP
MVP

I would try to expand on Chandan's comment. To avoid the null/non-numeric issue, you could also try a simpler way using RangeSum() like:

sum( {<[Date.Cal.Year]={2017}>} RangeSum([FieldA] , [FieldB]))


Capture.PNG Capture2.PNG

jonathandienst
Partner - Champion III
Partner - Champion III

They will NOT give the same results if there are any null values.

For Sum(A+B), if A is null, then A+B is also null, so that value of B will not be in the sum result. But it will be in the sum result for Sum(A) + Sum(B). The same applies if B is null and A is not null.

Deciding which is the correct one depends on your requirements. But make the mistake of assuming that they are equivalent.

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
rohitk1609
Master
Master

Hi Jonathan,

How it will work, when both facts are from different table ?

thkarner
Partner - Creator III
Partner - Creator III
Author

Hi Treseco B,

In my case I very large data sets and have ensure performance.

Do you know how is your expression is executed?

sum( {<[Date.Cal.Year]={2017}>} RangeSum([FieldA] , [FieldB]))

Possible methods I see are:

a) Calculate the RangeSum for ALL (millions) of records and afterwards calculate the Sum with the Set Analysis

OR

b) The main calculation is the Sum considering the records in the Set Analysis only and then calculate the RangeSum for those records

If it´s method be I see this as a resolution also in case of large data sets.

What do you think?