4 Replies Latest reply on Jan 31, 2013 11:07 AM by aishwaryarajagopalan0

    Multiple if then else within same variable calculation

    aishwaryarajagopalan0

      Hello,

       

      Is it possible to build multiple if then else end conditions check within same variable calculations in Tableau.

       

      I am looking for an work around for the below calculations in Tableau:

       

      number x:= b

      if (a > x) then x:=a

      end

      if c in ('XXX','YYY') THEN x else

      (

      if d > x

      then x:=d

      end

      if e > x

      then x:=e

      end

      x

      )

        • 1. Re: Multiple if then else within same variable calculation

          Yes, you can use and IF THEN ELSEIF statement.

           

          Check this out: http://onlinehelp.tableausoftware.com/v7.0/pro/online/en-us/help.htm

           

          Middle of the page...

           

          IF test1 THEN value1 ELSEIF test2 THEN value2 ELSE else END

           

          There is no built-in limit to the number of ELSEIF test THEN value’s in an IF expression, however, individual databases may impose a limit on IF statement complexity. While an IF statement can be rewritten as a series of nested IIF statements, there are differences in how the expressions will be evaluated. In particular, an IIF statement distinguishes TRUE, FALSE and UNKNOWN, while an IF statement only worries about TRUE and not true (which includes both FALSE and UNKNOWN). For example,

           

          IF [Region]=”West” THEN 1 ELSEIF [Region]=”East” THEN 2 ELSE 3 END

           

          Hope this helps.

          1 of 1 people found this helpful
          • 2. Re: Multiple if then else within same variable calculation
            aishwaryarajagopalan0

            Thanks for the response Derek but in the issue stated above; the "if condition" check occurs always and not when prior condition fails for which i can go for ELSEIF .

            • 3. Re: Multiple if then else within same variable calculation
              Dana Withers

              Hello,

              I'm a bit confused as to the progression of IFs. This may not answer your question correctly on if you can have multiple IFs following each other, but do you have to do it like that?

               

              Following your IF-"story", I think this is what you do:

              Set X to the value of B

              If A is bigger then X, set X to the value of A

              If C is in a range return X otherwise...

                   if D is bigger then X set X the value of D

                   if E is bigger then X set X the value of E

                   return X

               

              So in that story, several IFs are following each other and may or may not do something to X. If you shuffle it around a bit, you can achieve the same with this:

              If C is in a range...

                   return the biggest value of X, D and E (where X is the biggest of A and B)

              else

                   return the biggest value of A and B

              end

               

              Which in Tableau Calculations could be something like this:

              IF ([C]  = "xxx" or [C] = "yyy")THEN

              max( max( max([A],[B]) , [D]), [E])

              ELSE

              max([A],[B])

              END

               

              Hope this helps

              Dana

              • 4. Re: Multiple if then else within same variable calculation
                aishwaryarajagopalan0

                Thanks a lot for the response Dana, my mind was stuck thinking about IF's.Never went in direction of playing around with the conditions. Thanks much again.