Not all variables are created equal

By Barry Harmsen

Beware of the equals signIt has been a while since my last post. To get back in the habit of regular updates, I am starting today with a short tip on a caveat of the use of the equals sign (=).

Starting an expression with or without an equals sign may almost seem like an arbitrary decision. Most developers quickly figure out that this is not true for text objects. However, there is another, less obvious area where the use of the equals sign can greatly impact how (and more importantly, when) your expression is calculated.

When creating a variable in the variable overview (Ctrl + Alt + V) an expression that starts with an equals sign is directly calculated by QlikView. An expression that does not start with an equals sign is not calculated. The implication of this difference becomes clear when you consider the following example:

Raw data

An application contains data on yearly revenue per store. The raw data is shown above. To get the total revenue, a variable called v_Revenue is created than contains the following expression:

=sum(Revenue)

When this variable is used in a straight table to display the total sum of revenue per store for all years, the following result is shown:

Revenue per store using a variable starting with an equals sign

This is obviously not the correct amount, it is the total revenue for all stores and years. Because the variable expression started with an equals sign, it was immediately calculated at the document level by QlikView, without taking the Store dimension into consideration.

The solution is to remove the equals sign from the start of the variable expression:

sum(Revenue)

The next step is to modify the expression in the straight table so that the variable is enclosed in a Dollar-sign followed by parentheses:

$(v_Revenue)

This is called “Dollar-sign expansion” and it tells QlikView to calculate the variable. Because the variable is now being calculated at the straight table expression level, it correctly takes the Store dimension into consideration.

Revenue totals when the equals sign is not used in the variable expression

The example QVW below shows the difference between the two variable expressions. As always, if you have questions or comments, feel free to drop them in the comment section below.

Download the example QVW