Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
hic
Former Employee
Former Employee

Variables can be used in many ways in QlikView. They can have static values or they can be calculated. But when are they calculated?  At script run-time or when the user clicks? And how should they be called? With or without dollar expansion?

 

One basic way to assign a value to a variable is to use a Let statement in the script:

 

     Let vToday  = Num(Today()) ;

 

This will calculate the expression and assign it to the variable when the script is run. This is exactly what you want if you want to use a variable as a numeric parameter in your expressions.

DocProp.png

 

But if you want the expression to be evaluated at a later stage, e.g. every time the user clicks, what should you do then? One way is to store the expression as a string in the variable, using either the Set or the Let statement or by defining it in the Document Properties -> Variables:

 

     Set vSales  = Sum(Sales) ;

     Let vSales  = 'Sum(Sales)' ;

 

In neither case, the expression will be calculated. The variable will contain the string ‘Sum(Sales)’, which subsequently can be used in an expression using a dollar expansion: $(vSales).

 

With a dollar expansion, QlikView will substitute the ‘$(vSales)’ with ‘Sum(Sales)’ before the expression is evaluated. Some of you will recognize this as an old style assembler macro expansion. The subsequent calculation will be made based on the evaluation of the resulting expression. Note the two steps: (1) Variable expansion; and (2) Expression evaluation.

 

Chart.png

 

In the chart above, you can see the result of using a normal variable reference (the first expression) or using a dollar expansion (the second expression). In the second expression, the variable is expanded and the numbers are calculated correctly.

 

But this is just the beginning…

 

It is also possible to calculate the variable value, i.e. determine how it should be expanded, by using an initial equals sign in the variable definition.

 

     Let vSales2  = '=Sum(Sales)';

 

In this case, the variable value is calculated after each click, whereupon the dollar expansion in the chart expression is made, and finally the expression is evaluated. This means that the evaluation of ‘Sum(Sales)’ is done before the variable expansion. Note the three steps: (1) Variable calculation; (2) Variable expansion; and (3) Expression evaluation.

 

The table below summarizes the three methods.

 

What you see 2.png

 

With the above, you can do almost magical things. You can for instance make conditional calculations that depend on e.g. selections, client platform or user.

 

Example:

  • Create a field [Field to Aggregate] containing the names of two other numeric fields: 'Quantity' and 'Sales'
  • Create a variable vConditionalAggregationField = '=Only([Field to Aggregate])'
  • Create a chart with an expression = Sum($(vConditionalAggregationField))

 

The calculation in a chart will now toggle between Sum(Quantity) and Sum(Sales) depending on your selection.

 

Quantity.png   Sales.png

 

The use of variables is an extremely powerful tool that you can use to create flexible applications. Use it – but with caution. Too much magic behind the curtains can be confusing.

 

HIC

 

Further reading related to this topic:

The Magic of Dollar Expansions

The Little Equals Sign

46 Comments
anderseriksson
Partner - Specialist
Partner - Specialist

It is very simple to try it out yourself.
Create the variable in a document you have with suitable values.
Create a table with a dimension and two expression referencing the variables.
Make som selections and see what happens.

0 Likes
878 Views
Anonymous
Not applicable

Thanks..I did find an example in the above post.

Prefixing a variable with an equals sign

0 Likes
878 Views
Not applicable

i feel like i'm in the 5th layer of inception

0 Likes
1,176 Views
Anonymous
Not applicable

Hi,

Based on the post example, how could i get something like this:

qlk.png

I.e. based on a field that contain other fields names, I want to sum for each field.

Right now i can just do this over one simple Field with:

let vField = '=Only([$Feature])';

or

let vField = '=MaxString([$Feature])';

And the expression: Sum($(vField))

The problem is that the expression is being evaluated over all $Feature set and not for each $Feature value in the table chart.

Thanks.

0 Likes
1,176 Views
Architect
Partner - Creator
Partner - Creator

@hic 

Hi, can you please let me know if there will be any difference in outcome for the below expressions?

1> SET vPath = .\includes\include.txt;

2> LET vPath = '.\includes\include.txt';

3> LET vPath = .\includes\include.txt;

 

805 Views
hic
Former Employee
Former Employee

The two first will result in the same variable assignment. The vPath will get a value corresponding to the given string.

The third assignment will however cause a syntax error, since the '.' isn't allowed in that position.

If you want to create an include macro, you can do so by using a $-expansion:

SET variable = '$(Include=.\includes\include.txt)' ;

779 Views