20 Replies Latest reply: Sep 5, 2017 4:16 AM by Rohit Kumar RSS

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

    Thomas Karner

      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

        • Re: Sum(A) + Sum(B) or Sum(A+B)
          An Pham

          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

            • Re: Sum(A) + Sum(B) or Sum(A+B)
              Thomas Karner

              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?

                • Re: Sum(A) + Sum(B) or Sum(A+B)
                  Lakshmikandh Karthikeyan

                  Both has given same results in my app.

                  • Re: Sum(A) + Sum(B) or Sum(A+B)
                    lakkyreddy devendar

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

                    • Re: Sum(A) + Sum(B) or Sum(A+B)
                      Kuldeep Tak

                      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.

                        • Re: Sum(A) + Sum(B) or Sum(A+B)
                          Tresesco B

                          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

                            • Re: Sum(A) + Sum(B) or Sum(A+B)
                              Thomas Karner

                              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?

                          • Re: Sum(A) + Sum(B) or Sum(A+B)
                            Jonathan Dienst

                            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.

                              • Re: Sum(A) + Sum(B) or Sum(A+B)
                                Rohit Kumar

                                Hi Jonathan,

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

                                  • Re: Sum(A) + Sum(B) or Sum(A+B)
                                    Jaime Aguilar

                                    Hi,

                                    just to add to this thread, as a general rule (I'm not sure if it's stated somewhere in help/documentation, etc) you should not use sum(A*B) or sum(A+B) if the fields belong to different tables. I haven't tested it, but in most cases you get ZERO if you try to do it.

                                     

                                    But even if you get a result it won't be necessarily right. In other words it would be like an "unsafe" use of the sum in the form sum(A+B)/sum(A*B).

                                     

                                    Like it's been said, you should take care of null values because you'll have different results.

                                     

                                    Also sum(A) + sum(B) does the sum taking as reference totals, while sum(A+B) will do it record per record (similar to what sumproduct does in Excel). This is the explanation of why it won't work correctly if you try sum(A+B) when fields belong to different tables (also remember, even if you concatenate 2 different tables into one, they are still 2 tables)

                                     

                                    so in conclusion you should never take sum(A) + Sum(B) and Sum(A+B) as equivalents,

                                     

                                    hope this helps,

                                     

                                    regards

                                      • Re: Sum(A) + Sum(B) or Sum(A+B)
                                        Thomas Karner

                                        Hi,


                                        thanks for your post. You wrote:

                                        also remember, even if you concatenate 2 different tables into one, they are still 2 tables

                                        I can´t really imagine this. Can you share the explanation why this should happen?

                                          • Re: Sum(A) + Sum(B) or Sum(A+B)
                                            Juraj Misina

                                            I gues what Jaime was referring to is a situation when you concatenate two tables into one and different fact fields come from different tables.

                                            Table:
                                            LOAD
                                                 ID,
                                                 A
                                            FROM Source_1;
                                            
                                            Concatenate(Table)
                                            LOAD
                                                 ID,
                                                 B
                                            FROM Source_2;
                                            

                                             

                                            Effectively the result is one table, but with NULLs on each row:

                                            IDAB
                                            1100NULL
                                            2200NULL
                                            3300NULL
                                            1NULL100
                                            2NULL200
                                            3NULL300

                                             

                                            This really should be treated as if you were working with two separate tables (from aggregation perspective), so use RangeSum() or other approach to account for null values.

                                            • Re: Sum(A) + Sum(B) or Sum(A+B)
                                              Rohit Kumar

                                              Hi Thomas,

                                              If I am right, Sum(A+B) =! Sum(A)+Sum(B). IF table is same for A and B and we use range sum so it would be fail for those rows which has null records. Range can give us right result for one table but it could be fail somewhere.

                                               

                                              Please correct me if I am wrong!

                                               

                                              Best,

                                              Rohit

                                            • Re: Sum(A) + Sum(B) or Sum(A+B)
                                              Jonathan Dienst

                                              >>even if you concatenate 2 different tables into one, they are still 2 tables

                                               

                                              I don't think that is true. Perhaps you could offer some evidence as to why you believe this.