Search code examples
casetableau-api

Tableau issue with case statment


I am trying to say if the year is 2021 and the month is one then INT [api_contacts_handled] should be 320 and so on. [Y] and [M] are smallint.

IF [Y] = 2021 THEN

CASE [M]
    WHEN 1 THEN [api_contacts_handled] = 320
    WHEN 2 THEN [api_contacts_handled] = 420
    WHEN 3 THEN [api_contacts_handled] = 520
    WHEN 4 THEN [api_contacts_handled] = 620
    WHEN 5 THEN [api_contacts_handled] = 820
    WHEN 6 THEN [api_contacts_handled] = 920
    ELSE [api_contacts_handled]
END

ELSE [api_contacts_handled] END

Error - Expected type Boolean fount Integer, Results types from CASE expressions should match.

Any help would be great.

Thanks John


Solution

  • Probably you're missing the point of how case works.

    If the logic is the one you wrote, you should create a calculated field called api_contacts_handled_calculated, with this formula:

    IF [Y] = 2021 THEN
      CASE [M]
        WHEN 1 THEN 320
        WHEN 2 THEN 420
        WHEN 3 THEN 520
        WHEN 4 THEN 620
        WHEN 5 THEN 820
        WHEN 6 THEN 920
        ELSE [api_contacts_handled]
      END
    ELSE [api_contacts_handled]
    END
    

    You cannot "overwrite" your value like this:

    THEN [api_contacts_handled] = 320
    

    In addition, after "then" Tableau recognize a boolean statement checking the column against the values, which leads to your error.

    So with that new calculated field, you will test all you're conditions and assign a value based on them, handling the else parts as well.