Examples of using a variable in an expression
A variable in Qlik Sense is a named entity, containing a data value. When a variable is used in an expression, it is substituted by its value or the variable's definition.
Example:
The variable x contains the text string Sum(Sales).
In a chart, you define the expression $(x)/12. The effect is exactly the same as having the chart expression Sum(Sales)/12.
However, if you change the value of the variable x to for example Sum(Budget), the data in the chart are immediately recalculated with the expression interpreted as Sum(Budget)/12.
How names are interpreted
It is not recommended to name a variable identically to a field or a function in Qlik Sense. But if you do, you must know how to use them in an expression.
Example:
XXX is a field, a variable and a function. XXX will be interpreted as one of them depending on how you create the expression.
Expression | XXX interpreted as |
---|---|
$(XXX) | variable |
Count(XXX) | field |
XXX() | function |
Variable calculation
There are several ways to use variables with calculated values in Qlik Sense, and the result depends on how you define it and how you call it in an expression.
This example requires the following data is loaded in the data load editor:
Let's define two variables, from the variables overview:
- Name vSales Definition'Sum(Sales)'
- Name vSales2 Definition'=Sum(Sales)'
In the second variable, we add an equal sign before the expression. This will cause the variable to be calculated before it is expanded and the expression is evaluated.
If you use the
If you add a dollar-sign expansion and call
Finally, if you call
Dim | $(vSales) | $(vSales2) |
---|---|---|
A | 350 | 1560 |
B | 470 | 1560 |
C | 740 | 1560 |
As you can see,