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:
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 |
$(XXX) | variable |
Count(XXX) | field or variable |
XXX() | function |
When naming an entity, avoid assigning the same name to more than one field, variable, or measure. There is a strict order of precedence for resolving conflicts between entities with identical names. This order is reflected in any objects or contexts in which these entities are used. This order of precedence is as follows:
-
Inside an aggregation, a field has precedence over a variable. Measure labels are not relevant in aggregations and are not prioritized.
-
Outside an aggregation, a measure label has precedence over a variable, which in turn has precedence over a field name.
-
Additionally, outside an aggregation, a measure can be re-used by referencing its label, unless the label is in fact a calculated one. In that situation, the measure drops in significance in order to reduce risk of self-reference, and in this case the name will always be interpreted first as a measure label, second as a field name, and third as a variable name.
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 dialog:
- 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.