Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
See why IDC MarketScape names Qlik a 2025 Leader! Read more
cancel
Showing results for 
Search instead for 
Did you mean: 
salto
Specialist II
Specialist II

Loop with a list of values (not from i to j)

Hello,

I need to loop over the records of a table. Seems to be quite easy: for VAR=0 to NoOfRows('TableName') -1

But this is a problem for me. I need VARto be:

VAR
1
2
5
7
11
12

Otherwise I get an error. Is it  possible to make a loop with a list of values like in the table above? Thanks!

14 Replies
martinpohl
Partner - Master
Partner - Master

try this:

Values:

LOAD * INLINE [

    Values

    1

    2

    5

    7

    11

    12

];

load concat (distinct Values,',') as Valuelist resident Values;

drop table Values;

let values=peek('Valuelist',0,'Values');

for each v in $(values)

trace $(v);

next;

maxgro
MVP
MVP

I supposed your list was made of constant values

with variable values, you can read it from a table (in my example List)

and then check if  they are also in Data table

List:

load Concat(F3, ',') as F3;

LOAD * INLINE [

    F3

    1

    2

    5

];

Data:

LOAD * INLINE [

    F1

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

];

LET vList = Peek('F3', 0, 'List');

trace 'vList='  $(vList);

for each v in $(vList)

  vF1 = Peek('F1', v-1, 'Data');

  if ($(vF1)=$(v)) then

  trace 'found match v='  $(v);

  end if;

next v;

salto
Specialist II
Specialist II
Author

Thanks a lot, it worked!

salto
Specialist II
Specialist II
Author

Hi Alessandro,

     the suggestion from Erica did the job - many thanks for your help.

     Cheers.

salto
Specialist II
Specialist II
Author

Hi Massimo,

     the suggestion from Erica did the job - many thanks for your help.

     Cheers.