Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
zagzebski
Creator
Creator

Looping Script with Variables


I need to write a script that creates a table for every month using variables:

Basically what I need is to run the following script for every month through Oct2013. (40+ times!). Each table would be concatentated with the previous (so would have one big table at end). How can this be done so I loop through each month? I am trying to avoid writing this code 40 different times...

Table:

Load Sum(Sales)

Dept

MonthYr,

Resident Production

Group by Dept

Where MonthYr = Jan2010;

4 Replies
swuehl
MVP
MVP

Maybe similar to this:

For i = 0 to 39

Let vMonthYear = date(AddMonths(makedate(2010),$(i)),'MMMYYYY');

Table:

Load Sum(Sales)

  Dept,

  only(MonthYr) as MonthYr,

Resident Production

Group by Dept

Where MonthYr = '$(vMonthYear)';

Next i

preminqlik
Specialist II
Specialist II

other way is keep all files in one folder and then access from by ising '*' them like eg below

Temp_Production:

load *

from PATH/*;

Production_Final:

noconcatenate

Load Sum(Sales)

Dept

MonthYr,

Resident Temp_Production

Group by Dept,MonthYr;

drop table Temp_Production;

andrewpettit
Partner - Creator
Partner - Creator

If you're really loading all the months from the same resident table "Production" why not simply group by Dept and MonthYr and eliminate the need for the loop?

zagzebski
Creator
Creator
Author

There is actually alot more to what I am trying to do - just didnt show it all. I am basically creating aggragated cubes that have the each dept's sales as a percent of the total sales for every month. It was way to hard to do on the front end.