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

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
atafsson
Creator
Creator

For and For each in the same loop

I have problems using a for loop and a for each expression in the same time.

My data looks something like this:

Temp:

LOAD * INLINE[

Client     DimID     DimPos      RowNo

A          A1          1               1

A          A2          2               2

A          A3          3               3

B          A1          1               4

B          B1          2               5

B          B3          3               6

C          B1          1               7

C          B2          2               8

C          C1          3               9

];

I want to create variables of the DimID for each client to each DimPos

The result would look like this for DimPos:

Dim1A=A1

Dim1B=A1

DIM1C=B1

Dim2A=A2

Dim2B=B1

Dim2C=B1

I dont know how many clients there are (the point is that you can add or remove one or more clients).

I do know that it always going to be 8 dimensions for each client.

I've done this

Set Clientlist='A','B','C';

FOR a=0 to 9

For Each Client in $(Clientlist)

Let Dim$(a)ID$(Client) =  Peek('DimId',Peek('DimPos',$(a),'Temp'),'Temp');

NEXT Client

NEXT a

...but it gives the wrong result..

Can someone help me out?

Labels (1)
2 Replies
MayilVahanan
MVP
MVP

HI

FOR each Client in $(Clientlist)

FOR a=0 to 9

For Each Client in $(Clientlist)

Let Dim$(a)ID$(Client) =  Peek('DimId',Peek('DimPos',$(a),'Temp'),'Temp');

NEXT a

NEXT Client

First close the inner loop and then outer loop

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.
atafsson
Creator
Creator
Author

Sorry, my bad.. I've corrected it.

Thanks Mayil, but that will give a result of all 9 DimId on all clients.

I want to add 1 to $(a) at the same time the client changes.. When the loop is finished with the first client

a=1,2,3

after this the client switches, and at the same time I want the $(a) to restart with DimPos =1 then 2 then 3. (But the Peek function takes RowNo...)

Can I use the Step function after the for statement? Something like this:

For a=1 to 3 step 3

Can I use a i IF statement to get the right result?

/AG