Skip to main content
Announcements
Qlik Introduces a New Era of Visualization! READ ALL ABOUT IT
cancel
Showing results for 
Search instead for 
Did you mean: 
Fq
Partner - Contributor III
Partner - Contributor III

Declaring Measures as Variables in the Script

Hi,

 

I have the following expressions:

 

This measure : 

(
Sum({<Date = {"$(='>=' & Date(Today() - 60, 'YYYY-MM-DD'))"}>} $(vUnits))
-
Sum({<Date = {"$(='>=' & Date(Today() - 120, 'YYYY-MM-DD') & '<=' & Date(Today() - 61, 'YYYY-MM-DD'))"}>} $(vUnits))
)
/
Sum({<Date = {"$(='>=' & Date(Today() - 120, 'YYYY-MM-DD') & '<=' & Date(Today() - 61, 'YYYY-MM-DD'))"}>} $(vUnits))

 

This Dynamic Dimension:

Aggr(
if(rank((
Sum({<Date = {"$(='>=' & Date(Today() - 60, 'YYYY-MM-DD'))"}>} $(vUnits))
-
Sum({<Date = {"$(='>=' & Date(Today() - 120, 'YYYY-MM-DD') & '<=' & Date(Today() - 61, 'YYYY-MM-DD'))"}>} $(vUnits))
)
/
Sum({<Date = {"$(='>=' & Date(Today() - 120, 'YYYY-MM-DD') & '<=' & Date(Today() - 61, 'YYYY-MM-DD'))"}>} $(vUnits))
) <= 15, [Product Name]),
[Product Name]
)

 

Both expressions work fine if I put them directly into a chart. 

I would like to declare them as variables in the script, so then I can call them with $(...) wherever they are needed. But I am struggling, I know you have to replace the single quotes with chr(39) but still when I am calling them in the script it says invalid dimension or invalid expression.

 

I would really appreciate if someone can help me.

 

Best regards

2 Replies
tresesco
MVP
MVP

Try like:

  1.  Copy this exact expression and paste into a variable definition
  2.  Remove '=' from the definition at the beginning, if you tempt to put one
  3.  Call this variable by exapnding with $

Note : you don't need to replace quotes with chr(39)

marcus_sommer

Defining expression-variables directly within the script is difficult especially in regard to the various kinds of quotes and even more if $-sign expansions are included. Of course there are workarounds to get it to work but you couldn't have a simple and general way just to assign an expression-string to a variable.

If it really needs to be done within the script it would be better to load it - variable-name + variable-content - from an external table and within a loop of the table the appropriate names and content are read and assigned to variables.

Both mentioned approaches have some complexity and will create some extra overhead and efforts and therefore you should have good reasons to go in this direction. Personally I don't use them else create them just within the variable-editor like @tresesco mentioned and only for those expressions which have a bigger redundancy or are long and complex and used as parts within another expressions. Variables should simplify the life and not being the opposite.

Beside this I would probably use something like this as (basis) variable:

vExp:
sum({< DateFlag = {$1}>} $(vUnits))

and the expression may then look like:

($(vExp(1)) - $(vExp(2))) / $(vExp(2))

and DateFlag is an appropriate created flag-field within the master-calendar.