QlikView variables are useful as they provide ease of future script maintenance. A variable holds either a single numeric or string value.
Back to basics
You can define a QlikView variable by using SET or LET keywords in your script.
SET assigns a string on the right-hand side of the equal to the variable
LET evaluates string on the right-hand side of the equal sign and assigns resulting value to the variable
For example, if you want to limit fact table with rolling 3 years of data, you can use a calculation and store that as a variable.
LET vYears =AddYears(today(), -3);
Now, you can use the variable in the where clause…
Fact:
Load
OrderDate,
….
from …
where OrderDate >= ‘$(vYears)’
;
If you are using same expression in multiple charts within your document then you may want to store that expression in a variable and instead use the expression using dollar-sign expansion in those charts that share same expression.
Variables with Dollar-sign Expansion
You can use dollar-sign expansion allows you to replace text in an expression with a variable. You can use dollar-sign expansion within Set Analysis syntax.
For example, you can define a variable in the variable overview to get max year.
vMaxYear = max(Year)
Now, this variable can be used with dollar-sign expansion within Set modifier.
sum({<Year = {$(vMaxYear)}>} Sales)
Conditional show
You can use a variable to show or hide a sheet or an object.
You can also enable/disable expression or show/hide column using a variable.