The unless prefix and suffix
is used for creating a conditional clause which determines whether a statement
or exit clause should be evaluated or not. It may be seen as a compact
alternative to the full if..end if
statement.
The statement or the exitstatement will only be executed
if condition is evaluated to
False.
The unless prefix may be used
on statements which already have one or several other statements, including
additional when or unless
prefixes.
Arguments
Argument
Description
condition
A logical expression evaluating to True or False.
statement
Any Qlik Sense script statement except control statements.
exitstatement
An exit for, exit
do or exit sub clause or
an exit script statement.
When to use it
The Unless statement returns a Boolean result. Typically, this type of function will be used as a condition when the user would like to conditionally load or exclude parts of the script.
The following lines show three examples of how the Unless function may be used:
exit script
unless A=1;
unless A=1
LOAD * from myfile.csv;
unless A=1
when B=2 drop table Tab1;
Regional settings
Unless otherwise specified, the examples in this topic use the following date format: MM/DD/YYYY. The date format is specified in the SET DateFormat statement in your data load script. The default date formatting may be different in your system, due to your regional settings and other factors. You can change the formats in the examples below to suit your requirements. Or you can change the formats in your load script to match these examples.
Default regional settings in apps are based on the regional system settings of the computer or server where Qlik Sense is installed. If the Qlik Sense server you are accessing is set to Sweden, the Data load editor will use Swedish regional settings for dates, time, and currency. These regional format settings are not related to the language displayed in the Qlik Sense user interface. Qlik Sense will be displayed in the same language as the browser you are using.
Example 1 – Unless prefix
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
The creation of variable A, which is given a value of 1.
A dataset which is loaded into a table named Transactions, unless the variable A = 2.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
id
date
amount
Results table
id
date
amount
1
08/30/2018
23.56
2
09/07/2018
556.31
3
09/16/2018
5.75
4
09/22/2018
125.00
5
09/22/2018
484.21
6
09/22/2018
59.18
7
09/23/2018
177.42
Because the variable A is assigned the value of 1 at the start of the script, the condition following the Unless prefix is evaluated, returning a result of FALSE. As a result, the script continues to run the Load statement. In the results table, all the records from the Transactions table can be seen.
If this variable value is set to equal to 2, no data will be loaded into the data model.
Example 2 – Unless suffix
Overview
Open the Data load editor and add the load script below to a new tab.
The load script starts by loading an initial dataset into a table named Transactions. The script is then terminated unless there are less than 10 records in the Transactions table.
If this condition does not result in a termination of the script, a further set of transactions is concatenated into the Transactions table and this process is repeated.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
id
date
amount
Results table
id
date
amount
1
08/30/2018
23.56
2
09/07/2018
556.31
3
09/16/2018
5.75
4
09/22/2018
125.00
5
09/22/2018
484.21
6
09/22/2018
59.18
7
09/23/2018
177.42
8
10/01/2018
164.27
9
10/03/2018
384.00
10
10/06/2018
25.82
11
10/09/2018
312.00
12
10/15/2018
4.56
13
10/16/2018
90.24
14
10/18/2018
19.32
There are seven records in each of the three datasets of the load script.
The first dataset (with transaction id 1 through 7) is loaded into the application. The Unless condition evaluates whether there are less than 10 rows in the Transactions table. This evaluates to TRUE, and therefore the second dataset (with transaction id 8 through 14) is loaded into the application. The second Unless condition evaluates if there are less than 10 records in the Transactions table. This evaluates to FALSE, and so the script terminates.
Example 3 – Multiple Unless prefixes
Overview
Open the Data load editor and add the load script below to a new tab.
In this example, a dataset containing one transaction is created as a table called Transactions.
A 'for' loop is then triggered, in which two nested unless statements evaluate:
Unless there are more than 100 records in the Transactions table
Unless the number of records in the Transactions table is a multiple of 6
If these conditions are FALSE, a further seven records are generated and concatenated onto the existing Transactions table. This process is repeated until one of the two transactions returns a value of TRUE.
Load script
Transactions:
Load
0 as id
Autogenerate 1;
For i = 1 to 100
unless NoOfRows('Transactions') > 100 unless mod(NoOfRows('Transactions'),6) = 0
Concatenate
Load
if(isnull(Peek(id)),1,peek(id)+1) as id
Autogenerate 7;
next i
Results
Load the data and open a sheet. Create a new table and add this field as a dimension:id.
Results table
id
0
1
2
3
4
5
+30 more rows
The nested unless statements that occur in the 'for' loop evaluate the following:
Are there more than 100 rows in the Transactions table?
Is the total number of records in the Transactions table a multiple of 6?
Whenever both unless statements return a value of FALSE, a further seven records are generated and concatenated onto the existing Transactions table.
These statements return a value of FALSE five times, at which point there are a total of 36 rows of data in the Transactions table.
After this, the second Unless statement returns a value of TRUE, and therefore the load statement following this will no longer be executed.
Did this page help you?
If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!