3 Replies Latest reply on Jun 27, 2016 11:22 AM by Derrick Austin

# Calculated Field - Multiple IF Statements?

I need to group some columns (A,B,C,D,R) using a calculated field for a filter under the following categories:

C, D, ABR, ABCR, ABDR, ABCDR

I'm trying this with an IF statement:

IF [Class C] > 0 THEN 'C'

ELSEIF [Class D] > 0 THEN 'D'

ELSEIF [Class A] > 0

OR [Class B] > 0

OR [Class R] > 0 THEN 'ABR'

ELSEIF [Class A] > 0

OR [Class B] > 0

OR [Class C] > 0

OR [Class R] > 0 THEN 'ABCR'

ELSEIF [Class A] > 0

OR [Class B] > 0

OR [Class D] > 0

OR [Class R] > 0 THEN 'ABDR'

ELSEIF [Class A] > 0

OR [Class B] > 0

OR [Class C] > 0

OR [Class D] > 0

OR [Class R] > 0 THEN 'ABCDR' END

The problem with this is it only returns the first four filters (C, D, ABR).  Obviously this is because by the time it gets to ABR, it's already looped through all the possibilities and doesn't even bother with the last three. Is there a simple way to return each category so that it's exclusive from the one before?  If I could do multiple independent IF/END statements instead of using ELSEIF, that would be ideal, but Tableau doesn't seem to like that.

• ###### 1. Re: Calculated Field - Multiple IF Statements?

Hey Owen,

In order to make these all mutually exclusive, you would need to create multiple calculated fields - one for each of the conditions. An IF statement can only return one result.

Hope this helps!

- Derrick

• ###### 2. Re: Calculated Field - Multiple IF Statements?

Is there a way to get the multiple calculated fields into one filter?

EDIT: I tried creating a combined field with all of them, but it's incredibly unwieldy and ugly, and it returns a few extra filter options that I don't want.  Not sure if there's a way to clean it up.

• ###### 3. Re: Calculated Field - Multiple IF Statements?

You could do a parameter, but not a filter. A filter looks at a single field/row. A parameter can look at additional things.

You can setup your filter field to be something like this:

[myParam] = 'ALL'

OR

[myParam] = 'ABC' AND [ABC Calc] = 'ABC'

OR

[myParam] = 'C' AND [C Calc] = 'C'

OR

[myParam] = 'DEF' AND [ABC Calc] = 'DEF'

1 of 1 people found this helpful