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

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Loop to create datefield by adding 2 months to each startdate until reach start + 24 month


Hi

I have a table with two fields. ID and startdate. Each ID has a startdate, now I want to create a new datefield that should be generated by each ID´s startdate + 2month until it reach startate+24 months. I´m thinking of some sort of loop for each ID and startdate but have no clue how to start

The new datefield should look like this

First date is the startdate.                    ie 2011-02-01

2nd date is startdate +2                        ie 2011-04-01

3d date is startdate +4                          ie 2011-06-01

4th date is startdate + 6                        ie 2011-08-01

5th date is startdate + 8 and so on until startdate + 24month/ 2 years are reached.   ie2013-02-01

And that should be done fort each unic ID and it´s startdate.

Any suggestions are appreciated

1 Solution

Accepted Solutions
Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

Try

Data:

LOAD FLYKTNR,

    addmonths(StartDate, IterNo()*2-2) as StartDate

FROM [AddMonths.qvd](qvd)

while iterno() <=13 ;

If you don't want the last startdate, i.e. startdate+24months then change <=13 to <13 or <=12


talk is cheap, supply exceeds demand

View solution in original post

6 Replies
Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

let vStart = num(date#('2011-02-01','YYYY-MM-DD');

Dates:

load addmonths($(vStart),recno()*2 - 2) as StartDate

autogenerate(13);


talk is cheap, supply exceeds demand
Not applicable
Author

yes. but vStart have to be picked from the datefield 'startdate' beacuse it´s diffrent for each ID so the generated dates will be diffrent for each ID

Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

No problem, but if you don't give us that information we'll instead solve the problem you did post. Please post a sample document with the real problem explained.

You'll probably need to use a while clause and the iterno() function like in the date example in this blog post: Loops in the Script


talk is cheap, supply exceeds demand
Not applicable
Author

Thanks. I attached the qvd file in my first post. But I don´t understand why it add.xml to the filename. Remove that and you have a sample.

More background:

Each student (in the sampledata fieldname FLYKTNR) have the right to a scholarship for 2 years from their start. The scholarship are given every second month with the first on at the startdate. And I would like to generate thoose dates as paydates to forcast how many scholarships we have each month forward in time.

Gysbert_Wassenaar
Partner - Champion III
Partner - Champion III

Try

Data:

LOAD FLYKTNR,

    addmonths(StartDate, IterNo()*2-2) as StartDate

FROM [AddMonths.qvd](qvd)

while iterno() <=13 ;

If you don't want the last startdate, i.e. startdate+24months then change <=13 to <13 or <=12


talk is cheap, supply exceeds demand
Not applicable
Author

Thanks!