Maintaining expressions

In small applications, maintaining expressions isn't a problem. Every time you want to modify something, you can open each chart and change the expressions. However, when you have many applications using the same or similar expressions, changing them one by one can become challenging. 

A very simple and good way of maintaining expressions is to have them all in a centralized file. This can be an Excel file that you can then read. If anything changes, you must only change things in one place. The file also gives you a place to explain each of the expressions for further clarity. 

Each expression will be stored in a variable, and the variable will then be used directly in the chart expressions.

The following is an example of an Excel file format containing the application expressions:

Variable name Environment Expression definition Active flag Comments
vToday Live 'today()' Yes Contains today's date
vBookName Live 'Mastering Qlik Sense' Yes Book name
vSalesCurrentYear Live sum({< Year = {2018} >} Sales)' Yes Calculates sales for year 2018

 

This sample Excel file has five columns:

  • Variable name: This contains the name of the variable to be used in the expressions.
  • Environment: This column provides the flexibility to have different variables for different environments. This is useful if you want to test an expression while developing without affecting the live applications. 
  • Expression definition: This is the expression or value to be assigned to the variable.
  • Active flag: With this flag, you can make expressions active or inactive.
  • Comments: This explains what each variable and expression means.

You can then paste the following code into the script, which automatically creates the variables for you. In the following example, the file is called Variables.xlsx:

[Variables]:
LOAD
"Variable Name",
Environment,
"Expression Definition",
"Active Flag",
Comments
FROM [lib://AttachedFiles/Variables.xlsx]
(ooxml, embedded labels, table is Sheet1)
WHERE [Active Flag] = 'Yes' AND Environment = 'Live';

FOR i = 0 to NoOfRows('Variables')

LET vTempVar = peek('Variable Name',$(i), 'Variables');
LET $(vTempVar) = peek('Expression Definition', $(i), 'Variables');

NEXT;

Drop Table Variables;

In the previous code, we first load all the fields from my Variables file. In a real situation, you wouldn't need to load all the fields but only those containing the variable name and the expression value. We also give the table a meaningful name: [Variables].

Let's analyze the code in parts:

[Variables]:
LOAD
"Variable Name",
Environment,
"Expression Definition",
"Active Flag",
Comments
FROM [lib://AttachedFiles/Variables.xlsx]
(ooxml, embedded labels, table is Sheet1)
WHERE [Active Flag] = 'Yes' AND Environment = 'Live';

Note that I only loaded active expressions and those that I want in my live environment. If you are working in a development environment, simply change the WHERE clause as needed. For example, if you want to use only variables that we are testing while developing, use the following WHERE clause:

...
WHERE [Active Flag] = 'Yes' AND Environment = 'Development';

What we do next is to loop through the values of the Variables table. Inside the control statement loop, we will do the following:

  1. Read each table record
  2. Create the variables
  3. Assign values to the variables:
FOR i = 0 to NoOfRows('Variables')

NEXT;

Here, we use the function NoOfRows('Table_Name'), which returns the number of records in a previously loaded table. We will then loop through all the values in the table.

Within this loop, we then create a temporary variable that will hold the name of the final variables coming from theVariables.xlsx file. The following line of code shows how it can be done:

LET vTempVar = peek('Variable Name',$(i), 'Variables');

The peek() function returns the value of the field [Variable Name] in table [Variables] in the i position. The value of i increases with every loop.

We then create the final statement that creates the variable and assigns the expression or value to it:

LET $(vTempVar) = peek('Expression Definition', $(i), 'Variables');

After assigning the expressions to variables, you no longer need the table containing such expressions and thus we can delete the table. The following command shows how to delete a table:

Drop Table Variables;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.37.196