Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
KHSDM
Creator III
Creator 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!

1 Solution

Accepted Solutions
jonathandienst
Partner - Champion III
Partner - Champion III

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?

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein

View solution in original post

4 Replies
Anil_Babu_Samineni

Try in text object like $(vAgeRange)

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
jonathandienst
Partner - Champion III
Partner - Champion III

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?

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
sunny_talwar

Here is another option suggested by Stefan here:

Expression as a variable with $-sign expansion

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

KHSDM
Creator III
Creator III
Author

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.