Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

Contributor III

When to use $(variable) and when just to use variable?

I'm a bit confused on when to use $(variable) and when just to use variable.  I have the following script;

For vYear = 2010 to 2015

For vMonth = 1 to 12

vMonth = vYear & NUM(vMonth,'00')

<Do stuff>

WHERE {[0CALMONTH].[$(vmonth))]};

But should that bolded line be vMonth = $(vYear) & NUM ($(vMonth),'00')?

As far as I can tell they do the same thing (in this instance).

I understand that $(variable) is saying "don't interpret this as a literal string" but that doesn't always matter, so is it just a matter of "best practice" to always use $(variable) or is there actually different functionality?


3 Replies

Re: When to use $(variable) and when just to use variable?


Re: When to use $(variable) and when just to use variable?


In script SQL/LOAD you should always use the $() to embed the variable value.

In the front end if you are giving = in the variable definition then you can directly use variable name, otherwise you have to give $(variablename) to evaluate the expression.

Hope this helps you.




Re: When to use $(variable) and when just to use variable?

This expression is OK as a let expression. No need for the expansion here.

Let vMonth = vYear & NUM(vMonth,'00');

Expansions are necessary in SQL expressions because the database server has no concept of QV variables. Expansions are also necessary in set expressions and sometimes when you need to inject some dynamic text into a statement.

In the front end, if the variable is defined with a leading =, then it is globally evaluated and it can be used without an expansion (it already contains the result of the expression). If not, then you need to inject the variable into the expression using a $ expansion. For example:

     Set vMaxDate = '=Date(Max(Date))';

     Use in expression:  =vMaxDate

     Set vMinDate = 'Date(Min(Date))';

     Use in expression    ='$(vMinDate)'  // quotes needed for a date