Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
Jennell_McIntire
Employee
Employee

The switch control statement is one of many controls statement that can used to determine the flow of the script.  In Qlik Sense help, it is defined as:

 

The switch control statement is a script selection construct forcing the script execution to follow different paths, depending on the value of an expression.

 

Syntax:

Switch expression {case valuelist [ statements ]} [default statements] end switch

 

The switch control statement can be used in the script to identify a specific execution path.  With the switch control statement, several paths can be defined and a default path can be defined as well when no match is found in the case clause.  The various script paths cannot cross – they should be individual paths that do not overlap.  In the script below, the expression x will be compared to each case.  When there is a match, the script for that case will be executed.

 

In the example below, Case 1 will be executed – the variable y will be set to Sunday and one record will be generated in the table.

script.png

Below is a look at the record generated.

Table.png

It is also possible to have more than one value for a case, for instance, you can use the script below to run the same script if x matches 1, 2 or 3.

case 1 2 3.png

When faced with various script execution paths, try using the switch control statement.  While I rarely have a need to use it, I like how neat and straight-forward the syntax is.  I have attached an example Qlik Sense app with the full script for you to test out for yourself.

 

Thanks,

Jennell

6 Comments
guruprem
Partner - Creator III
Partner - Creator III

Thanks Jennell for showcasing the use case. I understand switch is a control statement. However switch is not replacement of 'nested if' with in a load statement, am I correct?

0 Likes
8,953 Views
Jennell_McIntire
Employee
Employee

That is correct.

0 Likes
8,953 Views
zbeauchemin
Partner - Contributor III
Partner - Contributor III

Thank you!

0 Likes
7,268 Views
KD2015
Contributor III
Contributor III

Hi Everyone, can someone explain why the script is outputting for Case 1 (Sunday) and not the other Cases in the first example? I read the whole article but did not understand how the script control is picking the Case to output & Qlik Help page also doesn't clearly explain how the Switch Statement works. Thank you 

696 Views
Lisa_P
Employee
Employee

In Jennell's example, it is the definition of the let x = 1; that is deciding on the case

667 Views
KD2015
Contributor III
Contributor III

Thank you Lisa, I get it now. I was getting confused a little by the usage of 'x' to not only call the Case, but to also determine the number of autogenerated rows...but yes I see it now. Thank you

636 Views