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: 
johnnyjohn
Creator
Creator

How to make Expression into parameterized function/variable

Hey, I'm hoping someone can help me with the below.

So I have this expression, which winsorizes some data, and works well. 

Avg(
  Coalesce(
    if( price< $(=Fractile(price,0.05)), $(=Fractile(price,0.05))),
    if( price> $(=Fractile(price,0.95)), $(=Fractile(price,0.95))),
    price
  )
);

For the life of me I cannot turn this into a parameterized variable. I tried creating the below in my load script:

Set fWeightedAvgTest8 =

Avg(
  Coalesce(
    if( $1 < Fractile($1,$2), Fractile($1,$2)),
    if( $1 > Fractile($1,$3), Fractile($1,$3)),
    $1
  )
);

And calling it in my script like this: 

$(fWeightedAvgTest8(price, 0.05, 0.95))

But it doesn't work, it says Nested aggregation not allowed

I also tried like this 

Set fWeightedAvgTest6 =

Avg(
  Coalesce(
    if( $1 < '=Fractile($1,$2)', '=Fractile($1,$2)'),
    if( $1 > '=Fractile($1,$3)', '=Fractile($1,$3)'),
    $1
  )
);

When calling it in the script, it says OK, but the KPI that has that expression is blank. 

 

Any help would be much appreciated! Thanks! 

Labels (4)
1 Solution

Accepted Solutions
QFabian
Specialist III
Specialist III

Hi @johnnyjohn , one option is put variables inside the formula in the chart, like this :

 

Avg(Coalesce(if( price < $(=Fractile(price,$(vExp1))), $(=Fractile(price,$(vExp1)))),if( price> $(=Fractile(price,$(vExp2))), $(=Fractile(price,$(vExp2)))), price))

 

The variables can be created in script :

Set vExp1 = 0.05;
Set vExp2 = 0.95;

 

and then in layout you can change the variables values in input boxes :

QFabian_0-1672858506042.png

 

And if you want to test creating and using a formula just in the script execution, youcan create functions.

https://help.qlik.com/en-US/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/ScriptControlS...

 

QFabian

View solution in original post

1 Reply
QFabian
Specialist III
Specialist III

Hi @johnnyjohn , one option is put variables inside the formula in the chart, like this :

 

Avg(Coalesce(if( price < $(=Fractile(price,$(vExp1))), $(=Fractile(price,$(vExp1)))),if( price> $(=Fractile(price,$(vExp2))), $(=Fractile(price,$(vExp2)))), price))

 

The variables can be created in script :

Set vExp1 = 0.05;
Set vExp2 = 0.95;

 

and then in layout you can change the variables values in input boxes :

QFabian_0-1672858506042.png

 

And if you want to test creating and using a formula just in the script execution, youcan create functions.

https://help.qlik.com/en-US/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/ScriptControlS...

 

QFabian