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 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:
Dim | $(vSales) | $(vSales2) |
---|---|---|
A | 350 | 1560 |
B | 470 | 1560 |
C | 740 | 1560 |
As you can see, $(vSales) results in the partial sum for a dimension value, while $(vSales2) results in the total sum.
Did this page help you?
If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!