Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

Announcements

Breathe easy -- you now have more time to plan your next steps with Qlik!
QlikView 11.2 Extended Support is now valid through December 31, 2020. Click here for more information.

rsvebeck
Not applicable

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

1 Solution

Accepted Solutions
rsvebeck
Not applicable

Re: Re-use a previously used calculation in the script

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

1 Reply
rsvebeck
Not applicable

Re: Re-use a previously used calculation in the script

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