Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

zagzebski
Contributor

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;

Tags (2)
4 Replies
MVP
MVP

Re: Looping Script with Variables

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
Valued Contributor II

Re: Looping Script with Variables

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
Contributor

Re: Looping Script with Variables

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
Contributor

Re: Looping Script with Variables

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.

Community Browser