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.
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.
The string XXX represents a field, a variable, a function, or a measure. XXX will be interpreted as one of these depending on how you create the expression.
|Expression||XXX interpreted as|
|XXX||measure, variable, or field|
|Count(XXX)||field or variable|
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 vSales variable as it is, for example in a measure, the result will be the string Sum(Sales), that is, no calculation is performed.
If you add a dollar-sign expansion and call $(vSales) in the expression, the variable is expanded, and the sum of Sales is displayed.
Finally, if you call $(vSales2), the variable will be calculated before it is expanded. This means that the result displayed is the total sum of Sales. The difference between using =$(vSales) and =$(vSales2) as measure expressions is seen in this chart showing the results:
As you can see, $(vSales) results in the partial sum for a dimension value, while $(vSales2) results in the total sum.