3 Replies Latest reply on Feb 3, 2019 4:09 PM by Ken Flerlage

# What is Tableau's alternative to the Visual Basic SWITCH function?

Hello,

Sorry but I am new to all of this, both Visual Basic and Tableau.

I have a Visual Basic SWITCH function that returns a value [Hours Worked] if the last 2 digits of a string are either; 04  05  or 70

SUM(IIF(RIGHT([WBS Element],2) ="70" ,[Hours Worked ],0))/SUM(SWITCH (RIGHT([WBS Element],2), "70",[Hours Worked],"04",[Hours Worked],"05",[Hours Worked],0))

In Tableau I have been able to write it as a nested IF

SUM(IIF(RIGHT([WBS Element],2) ="70",[Hours Worked],0))/(SUM(IIF(RIGHT([WBS Element],2) ="70",[Hours Worked],0)) + SUM(IIF(ENDSWITH([WBS Element],"4"),[Hours Worked],0)) + SUM(IIF(ENDSWITH([WBS Element], "5"),[Hours Worked],0)))

But this means that the code loops three times through each row and then sums them together.

So to my question, is there a Tableau function that performs in the same/similar way to the Visual basic SWITCH function?

• ###### 1. Re: What is Tableau's alternative to the Visual Basic SWITCH function?

CASE is the best alternative. You'd do:

CASE RIGHT([WBS Element],2)

WHEN "70" THEN [Hours Worked]

WHEN "04" THEN [Hours Worked]

WHEN "05" THEN [Hours Worked]

ELSE 0

END

Or you can write it as a single line:

CASE RIGHT([WBS Element],2) WHEN "70" THEN [Hours Worked] WHEN "04" THEN [Hours Worked] WHEN "05" THEN [Hours Worked] ELSE 0 END

• ###### 2. Re: What is Tableau's alternative to the Visual Basic SWITCH function?

Brilliant, thank you Ken.

• ###### 3. Re: What is Tableau's alternative to the Visual Basic SWITCH function?

Any time!