Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
RSvebeck
Specialist
Specialist

Re-use a previously used calculation in the script

Often I find my self in need of a previously written calculation later in the same script. How can I reuse the calculation?

Example:

This is what I would like to be able to do, even though  I know this is not possible I think this explains it best:

Load

if(SOLDQTY=0 and STOCKQTY<=0,0,

  if(SOLDQTY<=0,9999,

   if(STOCKQTY/SOLDQTY>0,STOCKQTY/SOLDQTY,

    0))) as MyMeasure,


if(Ordertype=1 and MyMeasure<=1,'A',

if(Ordertype=1 and MyMeasure>1 and MyMeasure<=5,'B',

  if(Ordertype>1 and MyMeasure>1 and MyMeasure<=26,'C',

   if(MyMeasure>26 and MyMeasure<=50,'D',

    if(MyMeasure>50,'E')))))as [MyMeasure groups]

resident myTable;

So my question is, can I do something like this? Or do I have to make resident loads to acomplish this without having to copy/paste the calculation of MyMeasure into the calculation in [MyMeasure groups] 8 times!

Observe that this example is not realistic, its just an example.

Perhaps using a variable for MyMeasure is the solution?

Regards

Robert

Svebeck Consulting AB
1 Solution

Accepted Solutions
RSvebeck
Specialist
Specialist
Author

I found that This solution works (using variables):


Set MyMeasure = '

if(SOLDQTY=0 and STOCKQTY<=0,0,

  if(SOLDQTY<=0,9999,

   if(STOCKQTY/SOLDQTY>0,STOCKQTY/SOLDQTY,

    0))) ';

Load

$(MyMeasure) as MyMeasure,


if(Ordertype=1 and $(MyMeasure) <=1,'A',

if(Ordertype=1 and $(MyMeasure) >1 and $(MyMeasure) <=5,'B',

  if(Ordertype>1 and $(MyMeasure) >1 and $(MyMeasure) <=26,'C',

   if($(MyMeasure) >26 and $(MyMeasure) <=50,'D',

    if($(MyMeasure) >50,'E')))))as [MyMeasure groups]

resident myTable;

But I still would like to know if there are other ways, perhaps of performance reasons, faster?

Robert

Svebeck Consulting AB

View solution in original post

1 Reply
RSvebeck
Specialist
Specialist
Author

I found that This solution works (using variables):


Set MyMeasure = '

if(SOLDQTY=0 and STOCKQTY<=0,0,

  if(SOLDQTY<=0,9999,

   if(STOCKQTY/SOLDQTY>0,STOCKQTY/SOLDQTY,

    0))) ';

Load

$(MyMeasure) as MyMeasure,


if(Ordertype=1 and $(MyMeasure) <=1,'A',

if(Ordertype=1 and $(MyMeasure) >1 and $(MyMeasure) <=5,'B',

  if(Ordertype>1 and $(MyMeasure) >1 and $(MyMeasure) <=26,'C',

   if($(MyMeasure) >26 and $(MyMeasure) <=50,'D',

    if($(MyMeasure) >50,'E')))))as [MyMeasure groups]

resident myTable;

But I still would like to know if there are other ways, perhaps of performance reasons, faster?

Robert

Svebeck Consulting AB