Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Learn how to migrate to Qlik Cloud Analytics™: On-Demand Briefing!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

IS it possible to have a switch statement in an expression

Hi,

Is it possible to have a switch statement in an expression????

Actually I have a scenario in which

if(var=1)

   set analysis1 getting execuited;

if(var=2)

    set analysis2 getting execuited;

if(Var=3)

  set analysis3 getting execuited;

else

default value;

this set of conditional stmt is an expression in a straight table chart I need to replace this conditional stmt with a switch statement or else any other apart from conditional statement....Can any one help me regarding the above

Thanks,

Kiruthiga

2 Replies
Not applicable
Author

Hi,

why don't you use anidated if? They work fine in expressions. For your example:

if(var=1, analysis1,if(var=2, analysis2, if(var=3, analysis3, defaultvalue)))

johnw
Champion III
Champion III

The closest equivalent of the switch statement is probably pick(match()).  For instance:

pick(1+match(var,1,2,3),defaultvalue,analysis1,analysis2,analysis3)

Though in your case, with ascending whole number values, you may not need the match():

pick(1+var,defaultvalue,analysis1,analysis2,analysis3)

But I think nesting if() statements is more clear and more easily maintained if you can afford the overhead of repeated reference to the variable.  I would typically put one per line, giving what I think is the clearest connection between the value and the result:

if(var=1,analysis1
,if(var=2,analysis2
,if(var=3,analysis3
         ,defaultvalue)))

Grrr... the new forum won't let me put a space in front of the if() on the first line to line them up properly.