2 Replies Latest reply on Nov 3, 2017 10:15 AM by Guy Ballard

    Why does this code work?

    Guy Ballard

      I had a student submit some code to challenge a statement I made regarding the difference between CASE and IF statements (that CASE statements do not support boolean algebra). The use case here is to create age buckets based upon an age dimension. The submitted code is below. I don't understand why it works and I have never seen code like it on forums or through my google research. It appears it somehow gets around the CASE statement's normal limitation on boolean algebra AND evaluating multiple statements in one pass (using AND).

       

      CASE TRUE

      WHEN 0 <= [Age] AND [Age] <= 12 THEN 'Child'

      WHEN 13 <= [Age] AND [Age] <= 19 THEN 'Teenager'

      WHEN 20 <= [Age] AND [Age] <= 25 THEN 'Young Adult'

      WHEN 26 <= [Age] AND [Age] <= 65 THEN 'Adult'

      WHEN 65 < [Age] THEN 'Senior'

      END

        • 1. Re: Why does this code work?
          Deepak Rai

          It Works because it is Correct. The Student has just reversed the logic.

          • 2. Re: Why does this code work?
            Guy Ballard

            I think it's very clever what he's done and I'm happy to eat crow. I just don't understand what Tableau is doing. Why does using TRUE in the expression let Tableau then evaluate a < or > when it normally throws an error when trying to do this. Why does:

             

            CASE TRUE

             

            WHEN  65 < [Age] THEN 'Senior'

            ELSE 'Not Senior'

             

            END

             

            work, while

             

            CASE [Age]

             

            WHEN  65 < [Age] THEN 'Senior'

            ELSE 'Not Senior'

             

            END

             

            does not work. The only difference is the usage of 'TRUE' in the expression.