Variables – making your life easier

Variables are one of the most flexible elements in the QlikView realm. As we have seen throughout the book, you can use them as part of your script, the navigation schema or as a mechanism for the user to interact with the app. Likewise, they can help you standardize your dashboards by creating consistent color palettes or speed up your development by making it easier to create new calculations.

Formulas and Set Analysis

Stunning dashboards are not only functional, user friendly, and eye catching, but also robust and easy to maintain. One of the benefits of using variables in a QlikView app is that you can encapsulate common calculations. For example, imagine that you are dealing with an expression such as this one:

=sum({$<Category={'A', 'B', 'C'}, Cancelled={0}>} Sales)

Instead of copying and pasting it onto several charts time and again, you can create a variable called vSales to store it. Now, whenever you need to use it, you just have to type the variable name:

=money(vSales)

In the same manner, those terrifying 30-line expressions can be greatly simplified by using multiple variables:

=money(vSales –vCOGS - vOtherIncome)

Besides the obvious advantage of using a single variable instead of a long and complex formula, whenever the business rule changes, you only need to modify the variable definition once instead of changing dozens of objects, thus reducing the maintenance time and avoiding mistakes.

Another way of using variables to make your expressions simpler is to store bits and pieces to mix them up later on. For instance, the following variable contains the part of a Set Analysis expression that ignores all the selections made in the Calendar fields.

LET Ignore_Calendar = 
'Year=, Month=, Day=, MonthName=, Date=, Date_ID='; 

You can combine it with other common variables to display the YTD sales:

Formulas and Set Analysis

Alternatively, you can create a custom report using slider/calendar objects linked to other variables:

Formulas and Set Analysis

Tip

The easiest way to debug this kind of formulas is to create a nameless expression in a straight table and let the cursor hover over the column's header:

Formulas and Set Analysis

Handling colors with variables

Managing colors can be really annoying if your palette grows beyond five elements or if you need to keep consistency across multiple documents. Remembering all the codes, using Sticky Notes, or opening old files to copy the color tiles can be removed from your task list if you store them directly in variables:

LET Green_Background = RGB(225, 250, 225);
LET Red_Background   = RGB(255, 240, 230);

Then, you need to call them in the object's properties:

if([Margin %]>.21, $(Green_Background), $(Red_Background)) 

Besides defining a variable for the color as a whole, you can also create an Excel file that contains their R, G, and B components to assemble them later. This allows you to adjust the transparency (the Alpha parameter of the ARGB function) depending on where you plan to use them:

Handling colors with variables
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset