Tutorial - Scripting For Beginners PDF
Tutorial - Scripting For Beginners PDF
Qlik Sense®
June 2019
Copyright © 1993-2019 QlikTech International AB. All rights reserved.
HELP.QLIK.COM
© 2019 QlikTech International AB. All rights reserved. Qlik®, Qlik Sense®, QlikView®, QlikTech®, Qlik Cloud®, Qlik
DataMarket®, Qlik Analytics Platform®, Qlik NPrinting ®, Qlik Connectors®, Qlik GeoAnalytics®, Qlik Core®,
Associative Difference®, Lead with Data ™, Qlik Data Catalyst™, Qlik Associative Big Data Index™ and the QlikTech
logos are trademarks of QlikTech International AB that have been registered in one or more countries. Other
marks and logos mentioned herein are trademarks or registered trademarks of their respective owners.
Contents
Before you can create visualizations in your app in Qlik Sense, you have to load your data.
Knowing how to use load scripts allows you to prepare and manipulate your data when you
load it into your app.
You can load data using the data manager or the data load editor. You use the data load
editor when you want to create, edit and run a data load script.
You require access to the data load editor and should be allowed to load data in Qlik Sense Enterprise on
Windows or Qlik Sense Enterprise on Kubernetes.
Note that there are minor differences when loading data in Qlik Sense Enterprise on Windows or Qlik Sense
Enterprise on Kubernetes. Those differences have been indicated where applicable.
If you are using Qlik Sense Cloud Business, you can follow the instructions for Qlik Sense Enterprise on Windows.
l Customers.xlsx
l Dates.xlsx
l Region.txt
l Sales.xlsx
Additionally, the package contains a copy of the Scripting Tutorial app. You can upload the app to your hub.
We recommend building the app yourself as described in the tutorial to maximize your learning. Additionally,
you would have to upload and connect to your data files as described in the tutorial for the data loads to work.
However, if you run into problems, the app may help you troubleshoot. We have indicated which script segments
are associated with each lesson.
At help.qlik.com, you find the Qlik Sense online help and a number of downloadable guides.
These are all valuable sources of information and are highly recommended.
You can also load data using the data manager, but when you want to create, edit and run a data load script
you use the data load editor.
In the script, the fields and tables to load are specified. Scripting is often used to specify what data to load from
your data sources. You can also manipulate the data structure by using script statements.
During the data load, Qlik Sense identifies common fields from different tables (key fields) to associate the data.
The resulting data structure of the data in the app can be monitored in the data model viewer. Changes to the
data structure can be achieved by renaming fields to obtain different associations between tables.
After the data has been loaded into Qlik Sense, it is stored in the app.
When you open the data load editor, the script editor appears in the middle of your screen. Script sections are
shown as tabs in the left menu. Qlik Sense automatically creates the Main section. Data connections are shown
in the right menu.
If you are doing this tutorial in Qlik Sense Enterprise on Windows, you will be connecting to your data files using
the AttachedFiles connection. If you are doing this tutorial in Qlik Sense Enterprise on Kubernetes, you will be
connecting to your data files using the MyDataFiles connection.
The script must be written using the Qlik Sense script syntax. Qlik Sense syntax keywords are highlighted in blue.
l Click D in the toolbar to enter syntax help mode. In syntax help mode you can click on a syntax
keyword (marked in blue and underlined) to access syntax help.
l Place the cursor inside or at the end of the keyword and press Ctrl+H.
You can also use the keyboard shortcut Ctrl+Space to show the keyword list, and Ctrl+Shift+Space to
show a tooltip.
The data load editor toolbar contains a shortcut for commenting or uncommenting code. Click … , or press Ctrl
+ K to comment or uncomment code.
Indenting code
You can indent the code to increase readability. Click À to indent the text (increase indentation) or, click à to
outdent the text (decrease indentation).
In this tutorial, you will be using data from files, so you will be using LOAD statements.
You can also use a preceding LOAD to be able to manipulate the content of the data loaded. For example,
renaming fields has to be done in a LOAD statement, whereas the SELECT statement does not permit any
changes to field names.
The following rules apply when loading data into Qlik Sense:
l Qlik Sense does not differentiate between tables generated by a LOAD or a SELECT statement. This means
that if several tables are loaded, it does not matter whether the tables are loaded by LOAD or SELECT
statements or by a mix of the two.
l The order of the fields in the statement or in the original table in the database is unimportant to the Qlik
Sense logic.
l Field names are case sensitive and are used to establish associations among data tables. Due to this, at
times it is necessary to rename fields in the load script to achieve a desired data model.
Do the following:
Before you load data into your app for the first time, there is an option to use Add data to
easily load data from files. However, in this tutorial we want to see the script, so we will use
the data load editor.
5. Open the data load editor from the drop-down menu in the top toolbar.
6. Click P in the left menu to add a new script section below the section named Main.
Using more than one section makes it easy to keep your script organized. The script section will execute
in order when you load data.
8. Qlik Sense Enterprise on Windows: Under AttachedFiles in the right menu, click Select data.
Qlik Sense Enterprise on Kubernetes: Under MyDataFiles in the right menu, click Select data.
10. Deselect the fields # of Days Late and # of Days to Ship. You might need to click on the field headings to
see the complete field names.
11. Search for date in the Filter fields search field.
12. Click on the heading Invoice Date and type Bill Date to rename the field.
13. Click Insert script. The load script is inserted into the Sales section of the script editor. Note that Qlik
Sense puts double quotes around field names that contain a space.
Your script should look like this:
14. Add the following row above the LOAD statement to name the table Table1:
Table1:
15. Now adjust the script to ensure that the dates are interpreted correctly. Change the Date field to the
following:
Date#(`Date`,'MM/DD/YYYY') as "Date",
Now, let's load another table called Dates. After we load the table, Qlik Sense will connect it with the Sales
table on the Date field.
The top line of your script should now look like this:
25. Qlik Sense Enterprise on Windows: Under AttachedFiles in the right menu, click Select data.
Qlik Sense Enterprise on Kubernetes: Under MyDataFiles in the right menu, click Select data.
Under Field names, make sure that Embedded field names is selected to include the
names of the table fields when you load the data.
26. Upload and then select Dates.xlsx. A data preview window opens.
Data preview window for Dates data file
28. Add the following on the row above the LOAD statement to name the table Table2:
Table2:
29. To ensure that the Month column in the file Dates.xlsx is interpreted correctly in Qlik Sense we need to
apply the Month function to the Date field.
Change the Date field to the following:
Now you have created a script to load the selected data from the file Dates.xlsx. It is time to load the data
into the app.
33. Click Preview in the bottom left corner. Click on the name of the table Table2.
This will display information about the table. In the Preview field you can see that 628 rows of data have
been loaded into the internal table Table2. If you instead click on a field in the table, you will see
information about the field.
The data is now available to use in visualizations in an app. We will show you how later in this tutorial.
3 Renaming fields
In the previous topic, we showed you how to rename fields in the data preview window. In the heading of the
data preview window, you renamed Invoice Date to Bill Date. When you inserted the load script, you could see
that the field would be renamed using the keyword AS.
Do the following:
Load script window with script to rename fields (Qlik Sense Enterprise on Windows)
Load script window with script to rename fields (Qlik Sense Enterprise on Kubernetes)
3 Reducing data
Qlik Sense provides several different ways to reduce the amount of data that you load into your app. You can,
for example, filter data from files or from data connectors.
Do the following:
This tells Qlik Sense to only load data where sales are greater than $100.00. It also uses the Date function
to load data where the date is equal to or greater than January 1, 2013.
Load script window with script to reduce amount of data loaded (Qlik Sense Enterprise on Windows)
Load script window with script to reduce amount of data loaded (Qlik Sense Enterprise on Kubernetes)
6. If you the add the data to a table in your app, you can see that only the data that conforms to the
conditions that you created was loaded.
Note that we added the Sales field as a dimension. This is so that the Sales values are shown
individually. If we had added Sales as a measure, the values would have instead been aggregated per
date.
Typically, you would add Sales as a measure. With measures, you have the option to display values as
currency amounts (for example, dollars) by applying number formatting to the column.
However, as we are using Sales as a dimension, we need a different approach. In this case, we will use a
chart expression. Even though we are not discussing chart functions in detail in this tutorial, this is a
good opportunity for a quick example.
When we use Sales as a dimension, the values are displayed as numeric, as shown in the table above.
To fix this, you can open the chart expression editor Sales field by clicking fx, and then using the Money
function.
=Money( Sales )
Expression editor
The values in the field will now display as monetary units. Those units (in this case, dollars) are specified
in the Main section of the load script.
Table containing Bill Date and Sales fields. Sales now shown in dollars.
7. Now that we have completed this example, comment out the Where statement. Don't forget to add a
semicolon to the end of your first LOAD statement.
Your script should look like this:
Load script window with Where statement commented out (Qlik Sense Enterprise on Windows)
Load script window with Where statement commented out (Qlik Sense Enterprise on Windows)
3 Transforming data
You can transform and manipulate data using many different techniques in the data load editor.
One of the advantages to data manipulation is that you can choose to load only a subset of the data from a file,
such as a few chosen columns from a table, to make the data handling more efficient. You can also load the
data more than once to split up the raw data into several new logical tables. It is also possible to load data from
more than one source and merge it into one table in Qlik Sense.
In this topic, you will perform some basic data transformation using a Resident load, and then a Preceding load.
In this example, you will create a new table called Sales_Buckets and then load the data from Table1 using a
resident load. In the Sales_Buckets table, you will create a variable called quantity_threshold, and then use a
Where statement to only load data that meets that threshold.
Sales_Buckets:
LOAD
"Sales Qty" as "High_Quantity",
"Item Description" as "Item",
"Customer Number" as "Customer"
Resident Table1
Where ("Sales Qty" > $(quantity_threshold));
Load script window with script to create new table called Sales_Buckets (Qlik Sense Enterprise on Windows)
Load script window with script to create new table called Sales_Buckets (Qlik Sense Enterprise on Kubernetes)
6. Add the data to a table in your app. Add Item and Customer as dimensions. Add High-Quantity as a
measure aggregated on Count, and then again aggregated on Sum. Next, add a new column as a
measure with the following formula:
= Sum(High_Quantity) / Count(High_Quantity)
Your table shows, for example, that Customer 10025737 has made 4 large orders of High Top Dried
Mushrooms, with an average quantity of 14,800. To perform sorts on the data in the fields, close Edit
mode by clicking Done.
7. Now that we have completed this example, comment out the script for the quantity_threshold variable
and the Sales_Buckets table.
The end of your script should now appear as follows:
As mentioned earlier in this tutorial, you can load data into Qlik Sense using the LOAD and SELECT statements.
Each of these statements generates an internal table. LOAD is used to load data from files or from an inline
table, while SELECT is used to load data from databases. You have used data from files in this tutorial. In this
example, you will use an inline table. However, it is worth noting that a preceding load can be used above a
SELECT statement to manipulate your data. The basics are the same as you will see here using LOAD.
This example is not related to the data we are loading in this tutorial. It is only used to show an example of what
a preceding load can look like. You will create an inline table in the data load editor called Transactions. Date
interpretation will be performed in the preceding LOAD, where a new field called transaction_date will be
created. This field is created from the sale_date field.
3 Concatenation
Concatenation is an operation that takes two tables and combines them into one.
The two tables are added to each other by stacking one on top of the other, with a column for each distinct
column name. The data is not changed and the resulting table contains the same number of records as the two
original tables together. Several concatenate operations can be performed sequentially, so that the resulting
table is concatenated from more than two tables.
The number and names of the fields must be exactly the same for automatic concatenation to take place. The
order of the two LOAD statements is arbitrary, but the table will be given the name of the table that is loaded
first.
Do the following:
4. On a new line in the script in the section Dates, copy and paste the LOAD statement for Table2. This will
cause the data to be loaded twice. Name the second table Table2a.
You could also delete the existing script, and copy and paste the following (Qlik Sense Enterprise on
Windows):
Table2:
LOAD
"Date",
Month (Date) as "Month",
Quarter,
"Week",
"Year"
FROM [lib://AttachedFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
Table2a:
LOAD
"Date",
Month (Date) as "Month",
Quarter,
"Week",
"Year"
FROM [lib://AttachedFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
Or delete the existing script, and copy and paste the following (Qlik Sense Enterprise on Kubernetes):
Table2:
LOAD
"Date",
Month (Date) as "Month",
Quarter,
"Week",
"Year"
FROM [lib://MyDataFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
Table2a:
LOAD
"Date",
Month (Date) as "Month",
Quarter,
"Week",
"Year"
FROM [lib://MyDataFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
Do the following:
1. Edit the LOAD statement for Table2a, adding Concatenate and commenting out Week.
Your script should now appear as follows (Qlik Sense Enterprise on Windows):
Table2a:
Concatenate LOAD
"Date",
Month (Date) as "Month",
Quarter,
// "Week",
"Year"
FROM [lib://AttachedFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
Your script should now appear as follows (Qlik Sense Enterprise on Kubernetes):
Table2a:
Concatenate LOAD
"Date",
Month (Date) as "Month",
Quarter,
// "Week",
"Year"
FROM [lib://MyDataFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
By commenting out Week, we make sure that the tables are not identical.
4. Click Table2 in the data model viewer, and then click Preview.
The table has the fields Date, Month, Quarter , Week, and Year . The field Week is still showing, because it
was loaded from Table2.
5. Click Week in Table2. The preview shows that the number of non-null values for the field is 628. However,
it you click any of the other fields, you see that number of non-null values is 1256. Week was loaded just
once, from Table2. The number of values, or records, is the sum of the number of records in Table2 and
Table2a.
Do the following:
1. To be able to separate the content of the two tables completely, add NoConcatenate to the LOAD
statement in Table2a, and rename the fields so that Qlik Sense does not create a synthetic key based on
the matching fields. Uncomment Week in Table2 so that the two tables have the same fields.
Your script should now appear as follows (Qlik Sense Enterprise on Windows):
Table2:
LOAD
"Date",
Month (Date) as "Month",
Quarter,
"Week",
"Year"
FROM [lib://AttachedFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
Table2a:
NoConcatenate LOAD
"Date" as "Date2",
Month (Date) as "Month2",
Quarter as "Quarter2",
"Week" as "Week2",
"Year" as "Year2"
FROM [lib://AttachedFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
Your script should now appear as follows (Qlik Sense Enterprise on Kubernetes):
Table2:
LOAD
"Date",
Month (Date) as "Month",
Quarter,
"Week",
"Year"
FROM [lib://MyDataFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
Table2a:
NoConcatenate LOAD
"Date" as "Date2",
Month (Date) as "Month2",
Quarter as "Quarter2",
"Week" as "Week2",
"Year" as "Year2"
FROM [lib://MyDataFiles/Dates.xlsx]
(ooxml, embedded labels, table is Dates);
4. Now that we have finished demonstrating concatenation, we no longer need Table2a. Delete all the rows
in the LOAD statement for Table2a, and then click Load data.
3 Circular references
If there are circular references (loops) in a data structure, the tables are associated in such a way that there is
more than one path of associations between two fields.
Do the following:
The red dotted lines indicate that a circular reference has been created. This is something that you
should avoid, because it may cause ambiguities in the data interpretation.
If you look at Table1 and the Table2 in the screenshot above, you can see that they have the Date field in
common. You can also see that Table1 and Region have the SalesKey field in common. Finally, notice that the
Table2 and Region have the field Week in common. This means that a loop, a circular reference, has been
created. Since this can cause problems in the data analysis later on, let us remove it.
The easiest way to resolve this is to rename or remove one of the fields. In our case, we have loaded some data
that we do not need for our app, and we can remove it.
Do the following:
LOAD
Region,
"Region Code"
FROM [lib://AttachedFiles/Region.txt]
(txt, codepage is 28591, embedded labels, delimiter is '\t', msq);
Your script should now appear as follows (Qlik Sense Enterprise on Kubernetes):
LOAD
Region,
"Region Code"
FROM [lib://MyDataFiles/Region.txt]
(txt, codepage is 28591, embedded labels, delimiter is '\t', msq);
Data model viewer showing that circular reference has been removed
3 Synthetic keys
When two or more internal tables have two or more fields in common, this implies a composite key relationship.
Qlik Sense handles this through synthetic keys. These keys are anonymous fields that represent all occurring
combinations of the composite key.
When the number of composite keys increases, depending on data amounts, table structure and other factors,
Qlik Sense may or may not handle them gracefully. For example, they may affect performance and increase
memory usage. Whenever there are several synthetic keys that are dependent on each other, it is good practice
to remove them.
Do the following:
Do the following:
Data model viewer showing that synthetic key has been removed
Do the following:
1. On top of the assets panel to the left, click H to open Fields. Here you find all the fields in all the tables
that you have loaded in the data load editor.
2. Scroll down to the bottom of the list, and click the field Year . Drag it to the center of the top left filter
pane.
3. In the same way, add Quarter , Month and Week to the filter pane.
You have now created a filter pane with four dimensions: Year , Quarter , Month, and Week.
Do the following:
1. Click Fields H .
2. Click the field Region and drag it to the center of the bar chart area.
3. Click Add "Region".
4. Click the field Sales and drag it to the center of the bar chart area.
5. Click Add as measure > Sum(Sales) .
6. In the properties panel on the right-hand side, click Appearance and then Presentation. Select
Horizontal.
The bars are now displayed horizontally.
7. In the properties panel on the right-hand side, click Sorting.
The sorting order is displayed.
8. Drag Sum([Sales]) above Region so that the dimensions are sorted by Sum([Sales]) (measure value)
instead of Region (dimension value, alphabetically).
The bar chart is complete, showing the sales results for the different regions. This is a basic bar chart.
There are many options to enhance it in the properties panel (to the right). Just to show you one of the
possibilities, let us use the title area for something more than just a title.
9. Paste the following into the title field of the bar chart:
Do the following:
1. Click Fields H .
2. Click the field Month and drag it to the center of the line chart area.
3. Click Add "Month".
4. Click the field Year and drag it to the center of the line chart area.
5. Click Add "Year".
6. Click the field Margin and drag it to the center of the line chart area.
7. Click Add as measure > Sum(Margin) .
8. Add the title Profit margin on top of the line chart.
Charts with data
Completed sheet
Because we limited the amount of monthly sales data in our original Sales.xlsx file, there is little data
available for our chart after the end of March for each year. You can make selections in your filter pane
so that you are only comparing the first three months of each year.
10. Click the Month field in the filter box, and then select Jan, Feb, and Mar .
Filter box
11. Close the filter pane. The Profit margin chart shows the data for the first three months of each year.