Qlik Community

Qlik Design Blog

All about product and Qlik solutions: scripting, data modeling, visual design, extensions, best practices, etc.

Employee
Employee

Switch Control Statement

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

2 Comments
Partner
Partner

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
1,660 Views
Employee
Employee

That is correct.

0 Likes
1,660 Views