Dollar-sign expansion with variables

Dollar-sign expansion is used to read or use the value of a variable in the script. In the previous section, we showed you how you could see what a variable contains; however, variables would not be useful if we could only read their values but not make use of them.

The dollar-sign expansion starts with $( and ends with ) at the end of the variable name, for example:

$(vSumResult)

To illustrate this better, let's have a look at the following example:

SET vMyInterestRate = 0.02;

Balance:
LOAD
AccountId
,Balance
,Balance * $(vMyInterestRate) AS [Interest On Balance]
,Balance * (1+$(vMyInterestRate)) AS [Final Balance]
FROM
[lib://MyFiles/CustomerBalances.qvd]
Note that numbers assigned to variables can be used as a string or number. Adding single quotes will make no difference.

In this example, we first create the variable vMyInterestRate and assign it a 2% interest rate.

We then load my data and create two more fields. Interest On Balance applies 2% of the balance by using a dollar-sign expansion on the variable we previously created.

Final Balance contains the balance plus its interest. 

So, the dollar-sign expansion is replaced with the same value assigned to the variable or with the result of an expression once it is evaluated. We mention this because it is important to consider strings when making comparisons.

For example, consider the following code:

SET vBookName = 'Mastering Qlik Sense';

BookTable:
LOAD * INLINE
[
Book Name
Mastering Qlik Sense
];

IF peek([Book Name]) = $(vBookName) THEN
TRACE True;
ELSE
TRACE False;
ENDIF;

In this example, we assign the string Mastering Qlik Sense to the variable vBookName. We then compare the field [Book Name] with the variable. However, this will throw an error because the value of vBookName when we use dollar-sign expansion is Mastering Qlik Sense and not 'Mastering Qlik Sense'. As the single quotes are missing, Qlik Sense thinks we are comparing the value of [Book Name] with another field called Mastering Qlik Sense.

What we want to do is to make sure our book name is enclosed in single quotes. The following correction to the code will make it work:

...
IF peek([Book Name]) = '$(vBookName)' THEN
...
..................Content has been hidden....................

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