In this post I want to share with you a good practice in handling the various expressions that exist in a QlikView document. The most used expressions are the ones used in charts, where they hold measures such as Sum(Sales), Sum(Price*Quantity), etcetera. These are the ones more likely to be reused by other objects and in different sheets. There many other expressions including Chart Attributes, Color Expressions and Show Conditions, you can see them all by going to the menu Settings/Expression Overview.
The use of expressions can be intensive in QlikView, especially when having a sophisticated user interface. There is a growing need to handle these expressions in a more efficient way, and this can be accomplished by the use of variables.
Reasons for holding expressions in variables:
*To achieve reuse: the formula for a measure such as Sales usually remains the same across a QlikView document, so it doesn’t make sense to write it on every chart.
*To enforce consistency in the formulas: by avoiding the risk of having different formulas that calculate the same measure.
*To provide a single point to apply changes: if and when a formula needs to be changed, you only need to change one variable and all the charts and other objects that refer to that variable will follow.
*To allow the end user to make changes through an input box, when needed. This could be the case of targets for KPIs or general parameters.
Variables can be created manually by going to the menu Settings / Variable Overview or by using the SET/LET statements in the script. They have a name and a value, which can hold any sort of strings or numbers, and they can be used as a reference from every sheet object. The Input Box is the object designed to show variables in the user interface.
If you want to start experimenting with moving your expressions to variables try the following:
1)Go to the Expressions tab on one of your charts and copy one of the formulas, for instance Sum(SalesValue)
2)Go to the menu Settings / Variable Overview and click on the “Add” button to create a variable. Give it a name such as vFormulaSales (it is a best practice to have all variable names starting with a v to help differenciate them from Fields).
3)Select your variable from the variable list an paste the formula from the chart in the “Definition” text box. If the formula starts with an = sign, remove it. Finally click on “OK” to save the changes.
4)Go back to the Expressions tab of your chart properties and replace the formula with the following: $(vFormulaSales)
The $ sign expansion indicates the string contained in the variable is a formula that needs to be calculated.
The final step is to replace replace the cloned formulas in all the other objects so they all refer to the same formula contained in the new variable. Every new object that needs to show Sum(Sales) should also refer to the variable.
You may already have quite a few QlikView documents where you didn’t apply this practice, but it’s never too late to get started. In the long term it’s really worth it.