8 Replies Latest reply: Jan 26, 2010 6:01 PM by John Witherspoon RSS

    Nested if-else into set analysis

    ravish85shah

      Dear All,

      I have following expression :-
      sum(if(some condition,sales,if(some condition,sales))

      I want to convert above nested if-else expression into set analysis.

      simple if expression can be converted into set analysis very easily.

      Thanks in advance.

      Regards,
      Ravish.

        • Nested if-else into set analysis
          Neil Miller

          I'm assuming the Sales field would be different for each condition.

          This should work:

          Sum ({<Field1 = {1}>} Sales1) + Sum ({<Field1 = {'<1>1'}, Field2 = {1}>} Sales2)


          If your values are not numeric, I think you can use this for the not equal portion:

          Sum ({<Field1 = {Y}>} Sales1) + Sum ({<Field1 -= {Y}, Field2 = {Y}>} Sales2)


            • Nested if-else into set analysis
              John Witherspoon

              Yeah, it would help if we could tell if the conditions are different and/or the sales are different. As written, it's just sum(if(some condition,sales)), which is almost certainly not what you were asking about.

              I'm not going to go through all four possibilities, so I'll just give you the rough idea. Set analysis has operators for union and exclusion, and some combination of these are likely what you want. Have a look at the help text for set analysis. Or give an actual example, and we can easily convert it to the set analysis equivalent.

              • Nested if-else into set analysis
                ravish85shah

                Thanks for your help.
                I want to convert following nested if-else expression into set analysis :-

                =sum(if(F1='FTD',if(year=vIndexYear and Month=vIndexMonth and Day=vIndexDay,amt),
                if(F1='MTD',if(year=vIndexYear and Month=(vIndexMonth) and Day<=vIndexDay,amt),
                if(F1='YTD',if(year=vIndexYear and new<=m,amt)))))

                so that my chart will be looked like as follows :-

                 

                F1FTDMTDYTD
                200400700


                Regards,
                Ravish.

                 

                  • Nested if-else into set analysis
                    John Witherspoon

                    Before we even get into set analysis, you should realize that the IF statement as you wrote it is being evaluated for every row of the table, where part of it really only needs to be evaluated once. So you'd get some improvement by just moving parts of it outside of the sum:

                    =if(F1='FTD',sum(if(year=vIndexYear and Month=vIndexMonth and Day =vIndexDay,amt))
                    ,if(F1='MTD',sum(if(year=vIndexYear and Month=vIndexMonth and Day<=vIndexDay,amt))
                    ,if(F1='YTD',sum(if(year=vIndexYear and new<=m,amt)))))

                    And that's the basic structure I'd keep when converting to set analysis. I think the set analysis expression would be this (untested):

                    =if(F1='FTD',sum({<year={$(=vIndexYear)} Month={$(=vIndexMonth)} Day={$(=vIndexDay)}>}amt)
                    ,if(F1='MTD',sum({<year={$(=vIndexYear)} Month={$(=vIndexMonth)} Day={'<=$(=vIndexDay)'}>}amt)
                    ,if(F1='YTD',sum({<year={$(=vIndexYear)} new={'<=$(=m)'}>}amt))))