Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
SonPhan
Partner - Creator
Partner - Creator

Function as Variable

Hello,

I had a question recently, but I have almost figured out the solution.

It's about the following I have a system variable that I define as a function. If I now call the function with a small sum function and Set Analysis it works for 3 of 4 cases.

I am interested in the case that does not work. The function contains a system variable defined on the surface.

$(vFunc(
sum({<Expression1={"$(=$(v61))"}, Expression2={'0'}, Expression2={'1'}>}Expression1)
))

and vFunc is only $1

which should only read the input.

Does anyone know how to cover the fourth case like this? This is a trivial example, which will be used later for practical purposes.

SonPhan_0-1616691123720.png

 

Labels (2)
1 Solution

Accepted Solutions
GaryGiles
Specialist
Specialist

The issue seems to be that Qlik interprets the comma "," between your set analysis criteria as addition parameters that are being passed to the vFunc variable ($2, $3, etc.). 

One way to solve, is to use ';' instead of comma in the expression to pass it as a parameter and then replace ';' with ',' in the vFunc;  Not sure if this is the best approach, but it works.

vFunc = $(=Replace($1,';',','))

The expression:

$(vFunc(
'sum({<Expression1={"$(v61)"}; Expression2={0}; Expression2={1}>} Expression1)'
))

Note that I got rid of the single quotes around the Expression2 criteria.  If you must include quotes, use 2 single quotes around the values because it is inside a string. 

Why is Expression2 included twice in your set analysis?  I believe only the second will apply.

Hope this helps.

 

View solution in original post

7 Replies
marcus_sommer

It doesn't work because your parameter contains a comma which will be always treated as a parameter-delimiter from the variable. There is no way to mask it else you would need any workarounds which don't contain commas and/or the commas are replaced with any other char and later the approach is reversed again.

I don't want to say that's not possible to nest variables but I wouldn't recommend any trial of it because it's the opposite to the main-aim of variables which is to simplify things.

- Marcus

GaryGiles
Specialist
Specialist

The issue seems to be that Qlik interprets the comma "," between your set analysis criteria as addition parameters that are being passed to the vFunc variable ($2, $3, etc.). 

One way to solve, is to use ';' instead of comma in the expression to pass it as a parameter and then replace ';' with ',' in the vFunc;  Not sure if this is the best approach, but it works.

vFunc = $(=Replace($1,';',','))

The expression:

$(vFunc(
'sum({<Expression1={"$(v61)"}; Expression2={0}; Expression2={1}>} Expression1)'
))

Note that I got rid of the single quotes around the Expression2 criteria.  If you must include quotes, use 2 single quotes around the values because it is inside a string. 

Why is Expression2 included twice in your set analysis?  I believe only the second will apply.

Hope this helps.

 

SonPhan
Partner - Creator
Partner - Creator
Author

Thanks you ! This helps me ! 

SonPhan
Partner - Creator
Partner - Creator
Author

Hello thank you for your solution,
when I work with this approach I make good progress, but there is one limitation I noticed, when I work with conditions e.g. buttons it does not recognize the "". This does not work in the normal case. I have already tried to replace ' but without success. Would you perhaps also have a solution suggestion?

SonPhan_0-1617134148147.png

This doenst work 

GaryGiles
Specialist
Specialist

Not sure if this is causing your issue, but are you missing a ) at the end of that expression?

$(vFunction('if(vButton="ButtonA";1;2)'))

 

SonPhan
Partner - Creator
Partner - Creator
Author

Unfortunately no, I forgot the bracket probably from hectic thanks for the tip. I think it is because of 'ButtonA' which was written as "ButtonA".  But i have no idea how to solve it. 

GaryGiles
Specialist
Specialist

Try using 2 single quotes on each side of ButtonA instead of double quotes.  Like this:

$(vFunc('if(vButton=''ButtonA'';1;2)'))

This worked for me when vButton is define as ButtonA in the variable definition panel.