Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
noahfels
Contributor III
Contributor III

Set Analysis in variable

I have stored several expressions that I use in a table in the GUI as variables in the script. 
For example: Set vTest = if( condition, 'true', 'false'); 
I use them in my table as follows: $(vTest). This works fine except for expressions which include setAnalysis.
For example: SET vOrder_Quantity = sum( {< Week_Original_Date = {$(=Week(today()))}>} order_quantity);

What happens is that this variable when called as $(vOrder_Quantity) is interpreted as: 

sum( {< Week_Original_Date = {}>} order_quantity);

The expression $(=Week(today())) is not interpreted correctly. Why is that and how can I put the given expression into a variable. (Needed for usability reasons in NPrint)

 

Labels (1)
1 Solution

Accepted Solutions
RsQK
Creator II
Creator II

Hey, you need to avoid the dollar sign expansion in script, here's how:

My sample data looks like this:

DATA:
LOAD * INLINE [
Week_Original_Date,order_quantity
50,100
51,150
52,300
];

Define the variable like this:

SET vOrder_Quantity = sum( {< Week_Original_Date = {##(=Week(today()))}>} order_quantity);
LET vOrder_Quantity = REPLACE('$(vOrder_Quantity)','##','$');

And in the front-end:

RsQK_0-1639481756690.png

 

 

View solution in original post

8 Replies
RsQK
Creator II
Creator II

Hey, you need to avoid the dollar sign expansion in script, here's how:

My sample data looks like this:

DATA:
LOAD * INLINE [
Week_Original_Date,order_quantity
50,100
51,150
52,300
];

Define the variable like this:

SET vOrder_Quantity = sum( {< Week_Original_Date = {##(=Week(today()))}>} order_quantity);
LET vOrder_Quantity = REPLACE('$(vOrder_Quantity)','##','$');

And in the front-end:

RsQK_0-1639481756690.png

 

 

noahfels
Contributor III
Contributor III
Author

That works fine, thank you... kind of crayz though.

Or
MVP
MVP

Nothing crazy about it - using dollar sign expansion in the script will result in the dollar sign being expanded in the script. If that didn't happen, all sorts of things wouldn't be possible... 🙂

noahfels
Contributor III
Contributor III
Author

Makes sense 🙂 @Or 
However now I have a different expression, which also doesn't come back with the correct result:
set vStock = Sum(Stock) - num(sum(<ProdType = {'defective'} Stock), '#.##0'); 
What am I missing here?

RsQK
Creator II
Creator II

Try:
NUM(Sum(Stock) - sum({<ProdType = {'defective'}>} Stock), '#.##0')

Or
MVP
MVP

No idea. Is the ProdType actually 'defective' (case-sensitive)? Also, why are you applying a num to part of the formula, and why not just write this as a single formula?

Sum({< ProdType -= {'defective'} >} Stock)

Best bet with this stuff is to use it in a regular chart first, and when it actually works in a chart, move it to a variable (you can also just set the variable manually in the variable editor, which saves you the issue of having to do it in script and deal with dollar sign expansion there).

 

noahfels
Contributor III
Contributor III
Author

That does not work for me. The num seems to work fine in different scenarios, however the set Analysis does not seem to work properly.

noahfels
Contributor III
Contributor III
Author

Ok so I have used the neater expression with -= in the chart and it works fine. However when I store it in my variable:

Set vStock = Sum({<ProdType -={'defective'}>} Stock); 

and use $(vStock) in the Chart then it gives me the result of Sum(Stock) without the set analysis restriction. 

(I am using the script because I don't have access to the variable editor in a published app)