Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

khimhoe_sdm
New Contributor III

Variable in Expression stored as Variable

!Hi all,

Currently I have this expression:

sum({$<Age={'$(vAgeRange)'}>} Amount)

I have 2 buttons in my layout with an action that will change the value of the vAgeRange variable.

Now, I need to store this expression in a variable in the script so that I can call the variable in my chart instead of using the expression in chart.

SET vExp_Amount = sum({$<Age={'$(vAgeRange)'}>} Amount);

The problem is that the vAgeRange will be evaluated in the script level. Due to this, the expression become static in the chart (since the vAgeRange is not evaluated anymore in the chart)

Any idea on how I can get around this?

*Attached is the example with the 2 workarounds suggested below. Thanks for the help!

Tags (2)
1 Solution

Accepted Solutions
MVP
MVP

Re: Variable in Expression stored as Variable

The $ expansion in the set statement IS evaluated (even though this a SET):

SET vExp_Amount = sum({$<Age={'$(vAgeRange)'}>} Amount);

As $(vAgeRange) may expand to <empty string> at this stage in the script execution, so the actual statement will become:

SET vExp_Amount = sum({$<Age={''}>} Amount);

Which is not what you want. To get around this, defer the evaluation like this:

Set vExp_Amount = sum({$<Age={'#(vAgeRange)'}>} Amount);

Let vExp_Amount = Replace(vExp_Amount, '#', '$');

Does that answer your question?

4 Replies

Re: Variable in Expression stored as Variable

Try in text object like $(vAgeRange)

MVP
MVP

Re: Variable in Expression stored as Variable

The $ expansion in the set statement IS evaluated (even though this a SET):

SET vExp_Amount = sum({$<Age={'$(vAgeRange)'}>} Amount);

As $(vAgeRange) may expand to <empty string> at this stage in the script execution, so the actual statement will become:

SET vExp_Amount = sum({$<Age={''}>} Amount);

Which is not what you want. To get around this, defer the evaluation like this:

Set vExp_Amount = sum({$<Age={'#(vAgeRange)'}>} Amount);

Let vExp_Amount = Replace(vExp_Amount, '#', '$');

Does that answer your question?

MVP
MVP

Re: Variable in Expression stored as Variable

Here is another option suggested by Stefan here:

Expression as a variable with $-sign expansion

LET vExp_Amount = 'Sum({$<Age={"$' & '(vAgeRange)"}>} Amount)';

khimhoe_sdm
New Contributor III

Re: Variable in Expression stored as Variable

Thank you Jonathan and Sunny! Both methods works, but I've chosen Jonathan's method as it is easier to read. I've edited my first post with an example containing both method. Hope this will help others.