Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
QlikView 11 for Developers

You're reading from  QlikView 11 for Developers

Product type Book
Published in Nov 2012
Publisher Packt
ISBN-13 9781849686068
Pages 534 pages
Edition 1st Edition
Languages

Table of Contents (23) Chapters

QlikView 11 for Developers
Credits
Foreword
About the Authors
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
1. Meet QlikView 2. Seeing is Believing 3. Data Sources 4. Data Modeling 5. Styling Up 6. Building Dashboards 7. Scripting 8. Data Modeling Best Practices 9. Basic Data Transformation 10. Advanced Expressions 11. Set Analysis and Point In Time Reporting 12. Advanced Data Transformation 13. More on Visual Design and User Experience 14. Security Index

Chapter 7. Scripting

In the previous chapter, we looked at building the frontend objects for use in dashboards, analyses, and reports. While it may be tempting to keep working on the frontend, we still have a bit of work to do on the backend. Very often input data is not in the exact same format as is required in the target data model, so in this chapter we will look at how we can use QlikView's built-in scripting language to transform our data.

We will first look at the script editor and some of the most important script statements, and see how we can use them to manipulate tables and control the flow of the script. This is followed by a look at some of the most commonly used functions for dealing with conditions and various data types. As QlikView scripts can get quite big and complex we will look at some ways in which we can debug our scripts. Next, we look at how we can properly organize and standardize our scripts so that they are easy to understand and maintain. We conclude this chapter...

The Script Editor


As we saw in Chapter 2, Seeing is Believing, Chapter 3, Data Sources, and Chapter 4, Data Modeling, the script editor is where a lot of the magic happens. In this chapter, we will be taking an in-depth look at the various functions that are available in this environment.

We will again be expanding the Airline Operations.qvw document that we worked on in the previous chapters. When you've opened the document, let's open the script editor by selecting File | Edit Script from the menu or by pressing Ctrl + E.

You will notice that the script editor consists of the following areas:

  • A menu bar

  • A toolbar

  • A script pane

  • A tool pane

Menu and toolbar

The menu offers a wide range of options, for some of which the toolbar offers shorthand icons. For now, the most important options to take note of are:

Script statements


A QlikView script is made up of a sequence of statements. These statements are typically used to either manipulate the data, or to conditionally control the way in which the script is executed. For example, we may want to combine two tables together, or skip over a part of a script if a condition is not met.

It is important to note that QlikView script is executed in a sequential order. This means that script is executed top to bottom, and left to right.

Building the aircraft dimension table

In Chapter 2, Seeing is Believing, we started building a small QlikView document to analyze airline operations data. We loaded a fact table and some dimension tables. All this data was loaded from QVD files, without any need for modifications. Of course, this is a scenario that you are not likely to encounter in the real world. In this example we will look at a scenario that is a little more plausible, l focusing on the Aircraft Type dimension. Instead of a single, tidy Aircraft dimension...

Conditional functions


Often in QlikView, you want to modify the data based on a condition. For example, we may want to classify any aircraft that was present in the database before 1990 as "Classic", and classify everything from 1990 onward as "Current". Let's open the script editor and see how this is done:

  1. Locate the [Aircraft Types] LOAD statement.

  2. Add a comma behind the line END_DATE as [Aircraft End Date] and press Return to create a new line.

  3. On the new line, put the following expression: If(Year(BEGIN_DATE) < 1990, 'Classic', 'Current') as [Aircraft Age Classification]

  4. As the 2010 update only contains aircraft that are newer than 2010, we do not need to use the conditional expression, instead we can use a fixed value.

  5. Add a comma behind the line END_DATE as [Aircraft End Date] and press Return to create a new line.

  6. Put the following expression on this line: 'Current' as [Aircraft Age Classification]

  7. Save and reload the document.

  8. Add Aircraft Age Classification field to the Aircraft...

Dealing with different data types


As we've seen in the previous section, QlikView offers a complete toolbox for dealing with data. In this section we will be looking at some of the most important operators and functions for dealing with strings, numbers, dates, and times.

Strings

Strings are pieces of text; in QlikView these are often used to provide context to the numbers. You may have noticed that in the script, strings are always enclosed between single quotes (').

String operators

The most common operation performed on strings is concatenating two or more strings together into a single string. This is achieved by using the & operator, for example:

[First Name] &' '& [Last Name] 

This concatenates the values of First Name and Last Name, with a space between them, into a single string containing the full name.

String functions

The following table shows the most important string functions.

Function

Description

File | Reload

Runs the entire script to reload the data

File | Save Entire Document

Saves the entire document, not just the script

Tab | Add Tab

As we have...

Debugging script


As with every form of scripting, writing QlikView scripts carries with it the risk of introducing bugs and errors. In this section, we will look at some of the available options to find and solve bugs in your script.

Syntax check

Improper use of syntax is a common cause of errors. Fortunately, QlikView has a feature that will catch these errors as they happen: Syntax Check.

As we saw earlier, QlikView script has syntax highlighting. Whenever incorrect syntax is detected, the statement is underlined in a red squiggly from that point onward. In practice, this means that often the error was made in the line that appears before the red underlined text. The following screenshot shows a piece of script with a syntax error, see if you can see what the error is.

If you looked closely at the script in the previous picture, you will have noticed that there was a comma missing after the TableName column. This causes the statement to be underlined in red from that point onward.

Besides...

Standardizing and organizing script


Have you ever experienced the following situation? A challenging problem presents itself. After many hours of thinking, developing, thinking some more, and developing some more, you have crafted a solution. "This is brilliant work!" you say to yourself, "It completely solves the problem, and in a very elegant way too."

Fast-forward a few months. A new business question presents itself and a small change to your original solution is required. You look at your original work, and after some poking around decide that you can make neither head nor tail of it."This is horrible work!" you say to yourself, "What was I thinking at the time?".

Did your script really go from brilliant to rubbish over the course of 6 months? Most likely not. You have just lost familiarity with the script. Fortunately, there are ways to ensure that you (and others) are able to quickly get up to speed when modifying existing QlikView script. The secrets are organizing your scripts and...

Re-using scripts


When developing QlikView documents, we often have to apply the same set of logic or transformations to different data. I have often observed QlikView developers taking a copy-paste approach for re-using a script. While this approach may initially work, it does make the script a lot harder to maintain. When something needs to be changed, you need to change it in each instance of the script, running the risk of different versions of the same transformation process.

In this section, we will look at two better approaches for re-using data. The first is the use of subroutines, which can be used to re-use script within an document. The second is the use of include files, which enables re-use of script between different documents.

Subroutines

A subroutine is a reusable block of script that can be called from other places in the QlikView script by using the CALL statement. This block is formed using the SUB and END SUB control statements. Subroutines can contain parameters so that...

Managing file locations and connection strings


In our current example documents, we have always referred to the Data Files folder for our source data. If, for any reason, this folder has to be moved somewhere else, we will have to manually change the source data path in many locations in many files.

Let's follow these steps to create an included script file to set the source data folder in a single location:

  1. Open Notepad (by pressing Windows Key + R, typing in notepad, and pressing Return) or any other text editor.

  2. Enter the following script:

    SET vFolderSourceData = '..\Data Files\';
  3. Save the file to the same folder as your QlikView document and call it Config.qvs.

  4. Close Notepad and return to QlikView's script editor window.

  5. Go to the Include tab and place the cursor on the first line.

  6. Go to Insert | Include Statement and select the Config.qvs file.

We have now created an include file that sets the vFolderSourceData variable to the path of the source data folder.

Note

In a real QlikView environment...

Summary


We have come to the end of this chapter on scripting. We have learned how to navigate the script editor. We have seen the most important script statements, and have applied them to our Airline Operations project. We also picked up a few tips and pointers for working with different data types in QlikView, and for debugging, standardizing, and organizing our scripts. We ended this chapter by looking at script re-using, which makes our scripts easier to maintain.

In short, we learned about the Script Editor window, what the most important script statements are and how to use them, operators and functions for dealing with various data types, and what options exist to debug a script.

We also learned how to organize and standardize our script and how to re-use your script within and between QlikView documents.

Now that we have learned the basics of QlikView scripting, in the next chapter we will apply this new knowledge while learning about data modeling best practices.

lock icon The rest of the chapter is locked
You have been reading a chapter from
QlikView 11 for Developers
Published in: Nov 2012 Publisher: Packt ISBN-13: 9781849686068
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}

Function

Explanation

Example

Result

len(string)

Returns the length of a string.

len...