Variables in QlikView

Variables in QlikView 

are very useful in data transformations and Calculations.

Variables can be defined by SET and LET statements.

Variables can be defined in script editor or by going into menu -> settings/variable overview.

Set and Let

The Set statement is used for defining Script variables

e.g.  Set vMinDate = ’10/31/2008′ ;

Set vMaxdate = ’12/31/2020” ;

The Let statement evaluates the expression on the right side of ‘=’ and assigns it to the variable  on the left side of ‘=’

Let vToday = Today();

After you run your script, you can see all your defined variables, under  menu Settings/variable Overview. New Variables can also be defined here.

Var1

Var22

Some uses of Variables

Variables are used when you want to perform a calculation once and use it in multiple charts.Variables are also used when you want to perform calculations dynamically.

Create a variable, assign it to a calculation. To create variable go to menu settings/Variable overview. Click on Add.

FreightCalc1

Use variable in the chart calculation

Var_InChart

Use variable in Set Analysis. 

Create a variable vCategory as vCategory = Category  in a similar fashion as vFreight is defined above.

Now use this variable in Set Analysis expression. CategoryName will be replaced based on the user selection

Var_InSetAnalysis2

Var_InSetAnalysis_Cat

Tip : Expressions in Variables can be defined by prefixing with = sign or without = sign. When variable expression is prefixed with = sign, for example, vTotalFreight = Sum(Freight) then this variable caculation will be performed globally i.e. for entire data set. If used in charts,all dimensions will be ignored and it will return the same values. 

If you want your calculation to align with the dimensions of the chart or selections then use variable expression without = prefix.

Variables provide lot of flexibility when you have to do calculations dynamically. You can also use Variables to set your global variables at document level. Like setting the path of your qvd files or include files etc.

Visit my blog variables in Text file  to learn how to declare variables in a text file so that you can define variables once and use them in multiple applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.