If you’re like me, you probably manage your expressions in variables that you store in an external variables table rather than defining them directly in the Data Load Editor. This allows for all your variables to be easily accessible in one place, so you can always review and edit them.
Variables still play an important role in Qlik Sense development, along with Master Items. While you could simply have an Expression hard-coded in the Master Item itself, I prefer to have my expressions defined as variables in an external file that I then load into my app and include in the definition of each Master Item in dollar sign expansion.
Using variables in place of hard-coded expressions has a few benefits. First, if you or another developer make a change to an expression, you can easily compare the difference between the old and new expressions in any advanced text editor. Second, you cannot use Master Items as parts of expressions for other Master Items. For example, if you have a Master Measure for Total Sales and a Master Measure for Cost, you cannot simply make a Master Measure for Margin by subtracting the Cost Master Measure from the Total Sales Master Measure. However, if you used variables for these Master Measures, you could subtract one Variable from the other.
How would I go about this? Well, I would start off in my variables table with two variables, let’s call them exp_Sales and exp_Cost, along with their respective definitions. Note, while the “exp” in my variable name is by no means necessary, it is a good way for me to differentiate between variables that contain expressions compared to variables that contain other definitions when I have a long list of variables. Then, in my Qlik Sense app, after I have loaded my data, I will create Master Measures for Total Sales and Cost with the variable name enclosed within $(). To create a third Master Measure for Margin, I can then simply use:
“$(exp_Sales) – $(exp_Cost)”
as the expression.
On to the little things…
French mathematician, scientist, and philosopher Blaise Pascal once famously said “little things console us because little things afflict us.” I’m sure in 17th century France, Pascal was not referring to loading his script in Qlik Sense, and yet the quote rings true. The way we are most used to loading our script in Qlik Sense is by going into the Data Load Editor and clicking “Load Data” in the upper righthand corner. While not tremendously cumbersome, it certainly becomes annoying having to do this when you are working in the App Layout and need to load your data after, say, editing an Expression in your variables table. Recently, however, I stumbled upon a shortcut that might save you a few seconds here and there. When working in the Layout in Edit mode, simply open the Fields view on the left side of the screen and click on the circular arrow on the bottom, next to the “Add” button. Boom! You’ve loaded your data without ever having to leave the Layout.
I hope you will play around with using variables in your Master Items, as well as externalizing your variables. If you would like an even more in-depth look at variables, Rob Wunderlich includes a detailed Variables discussion in his session on Advanced Scripting at the Masters Summit for Qlik. Interested in signing up? The next Masters Summit takes place April 1-5, 2019 in Stockholm, Sweden. We hope to see you there!
1 Comment
Thank you, a very useful article indeed!