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
hic
Former Employee
Former Employee

It is probably because the example is bad...

First of all, if you want Alt3 to respect the chart dimension, all you need to do is to define the variable as

     ='Sum(Sales)'

i.e. with quotes instead of without.

With "dynamic" I mean that the expression is not fixed - it can be changed by different conditions before it is evaluated. With the risk of creating an even worse example I suggest you look at the following variable definition:

=If(WeekDay(Today())='Mon','Avg','Sum') & '(Amount)'

This will on Mondays evaluate to 'Avg(Amount)' and on other week days to 'Sum(Amount)'. So if you expand this variable as expression in a chart, you will have different numbers different days of the week. I don't know why you would want this, but is serves as an example that you can calculate the expression, before the expression itself is calculated.

Makes sense?

HIC

11,304 Views
danieloberbilli
Specialist II
Specialist II

Right - I see - thanks a lot for your reply.

All the best from Leipzig

Daniel

0 Likes
11,304 Views
Not applicable

can you add the QVW and data files for this ? Thank you.

EDIT -

I tried to make a qvw file which demonstrates your lesson. Can you please help me to complete the sample app ? It is at - Prefixing a variable with an equals sign

0 Likes
11,304 Views
Not applicable

Hi Rob,

Tip to replace pipe character with chr(39)  when set analysis is included in variable, saved my day.

I owe you..

Thanks @Rob

-Regards

Ramesh

0 Likes
11,304 Views
Not applicable

In Henric's final example, how would one go about making the custom "field to aggregate" box?

Would you create a new List Box and populate it using an expression? If so, what syntax would be used? Or would another method be better?

Example:

  • Create a field [Field to Aggregate] containing the names of two other numeric fields: 'Quantity' and 'Sales'
0 Likes
11,328 Views
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

alyshakub In this example, one would normally load the field as an INLINE table in the scipt.

LOAD * INLINE [

Field to Aggregate

Quantity

Sales

]

;


-Rob

11,328 Views
Not applicable

Thank you Rob. That worked perfectly.

0 Likes
11,328 Views
Not applicable

Hi Henric,

Thanks for your post.

I have a scenario where I need to sum a number of dynamic columns based on user input from field selection, not sure this can be done.

In the script, I defined a variable to form the full expression:

Set vSumFieldSelections = 'Sum([' & GetFieldSelections([fieldName],']+[') & '])';

Then my expression in one of the QlikView text object:

=$(vSumFieldSelections)

The above code doesn't work, appreciate if you can provide some idea for me to move forward.

Thanks & Regards,

K

0 Likes
11,328 Views
hic
Former Employee
Former Employee

You want to make a two-step evaluation:

  1. Evaluate he GetFieldSelections call
  2. Evaluate the Sum()

but the way you have written it you will just get one step.

However, if you change your Set statement to

   Set vSumFieldSelections = ='Sum([' & GetFieldSelections([fieldName],']+[') & '])';

you will get a variable that is recalculated every time. Then it could get calculated to

     Sum([2015-03-10]+[2015-03-11])

if the field values are dates, and I have selected these two dates.

But this still doesn't help you, since the dates are field values. and you need field names in the sum function. Further, if you want to sum the field values, you just need to use

     Sum( fieldName )

HIC

0 Likes
11,328 Views
Not applicable

Hi Henric, thanks for your help. Finally I make it work by putting only single field in the sum function.

0 Likes
11,328 Views