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

Announcements
Qlik Connect 2026! Turn data into bold moves, April 13 -15: Learn More!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Autogenerate with a for. next.... (Loop)

Hi All,

I just can't seem to work this out and would appreciate any assistance.

Here is my script:

Let vRamsNoList = peek('RamsNoList',0,TempRamsList);

DROP Table TempRamsList;

For Each a in $(vRamsNoList)

TempCalendar:

Load $(a) as RamsNo, Date(addmonths(Date#(($(vMaxLoadedDate)&'01'),'YYYYMMDD'),(RowNo()-1)*-1),'YYYYMM') as Period autogenerate(24);

NEXT;

Calendar:

NoConcatenate

LOAD RamsNo, date(Period,'YYYYMM') as Period

Resident TempCalendar;

drop table TempCalendar;

This is what it is returning:          

RamsNoPeriod
111201212
111201211
111201210
111201209
111201208
111201207
111201206
111201205
111201204
111201203
111201202
111201201
222201112
222201111
222201110
222201109
222201108
222201107
222201106
222201105
222201104
222201103
222201102
222201101
333201012
333201011
333201010
333201009
333201008
333201007
333201006
333201005
333201004
333201003
333201002
333201001

This is what i want it to return:

RamsNoPeriod
111201212
111201211
111201210
111201209
111201208
111201207
111201206
111201205
111201204
111201203
111201202
111201201
222201212
222201211
222201210
222201209
222201208
222201207
222201206
222201205
222201204
222201203
222201202
222201201
333201212
333201211
333201210
333201209
333201208
333201207
333201206
333201205
333201204
333201203
333201202
333201201

Please advise on what i'm doing wrong, i'm convinced it is something to do with aggregating the autogenerated field with the RamsNo field.


Thanks.

1 Solution

Accepted Solutions
hic
Former Employee
Former Employee

Since I do not have the qvd file, I cannot see what goes wrong. But just looking at your script, I can see one big simplification: Don't use a For Next loop. You can do the same thing inside a Load statement using a While loop on a preceding Load:

LOAD

          RamsNo,

          date(Period,'YYYYMM') as Period;

Load

          RamsNo,

          Date(addmonths(Date#(($(vMaxLoadedDate)&'01'),'YYYYMMDD'),(IterNo()-1)*-1),'YYYYMM') as Period

          While IterNo() <= 24;

Load

          RamsNo

          FROM [......qvd] (qvd)

          where Active = 1;

Here I have two preceding loads in front of the Load that loads the QVD. So the bottom Load is piped into the Load with the While loop, which creates 24 records per initial record. Finally the result is piped into the top Load statement.

HIC

View solution in original post

4 Replies
Not applicable
Author

I have attached an example.

Please assist.

Thanks

hic
Former Employee
Former Employee

Since I do not have the qvd file, I cannot see what goes wrong. But just looking at your script, I can see one big simplification: Don't use a For Next loop. You can do the same thing inside a Load statement using a While loop on a preceding Load:

LOAD

          RamsNo,

          date(Period,'YYYYMM') as Period;

Load

          RamsNo,

          Date(addmonths(Date#(($(vMaxLoadedDate)&'01'),'YYYYMMDD'),(IterNo()-1)*-1),'YYYYMM') as Period

          While IterNo() <= 24;

Load

          RamsNo

          FROM [......qvd] (qvd)

          where Active = 1;

Here I have two preceding loads in front of the Load that loads the QVD. So the bottom Load is piped into the Load with the While loop, which creates 24 records per initial record. Finally the result is piped into the top Load statement.

HIC

hic
Former Employee
Former Employee

An additional point (which has nothing to do with your loop not working). You use

     Let vMaxLoadedDate = '200001';

and then

     addmonths(Date#(($(vMaxLoadedDate)&'01'),'YYYYMMDD'), ... )

I would instead use

     Let vMaxLoadedDate = Num(MakeDate(2000,01)) ;

and

     addmonths($(vMaxLoadedDate), ... )

Then vMaxLoadedDate will be an integer that you can use directly in date and time functions. No additional interpretation necessary.

HIC

Not applicable
Author

Thank you Henric,

I think my script was very over complicated:)

Much appreciated.