Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
New-Qlik
Creator II
Creator II

For loop for saving into excel

Hie,

I have an requirement with customer sales data and i want to create csv files based on customer id 

Table A:

load 

Customer id,

customer name ,

sales TW,

Sales LW

From Customer_table.qvd(qvd);

I have 50 customers 

so I want csvs to be generated with customer id as suffix. and data in csv should be filtered based to customer id and it should get Store in some location 

customer_1.csv - should have data just for customer id 1

customer_2.csv

customer_3.csv

...... Customer_50.csv

 

 

solution which i thought:

For loop for customer-id

load 

...

resident Table A 

where customer_id = (variable passed via for loop)

store table A into .....customer-(variable name)

 

1 Solution

Accepted Solutions
Gui_Approbato
Creator III
Creator III

Hello. Try this:

//First, load only the customer names

CustomersTable:

load  distinct 

"Customer id" as CustomerColumn

From Customer_table.qvd(qvd);

//Then, start your loop with a customer in a variable

For i=0 to NoOfRows('CustomersTable')-1

Let vCustomer = Peek('CustomerColumn',i,'CustomersTable');

 

// now include the WHERE clause with the variable at the end of your table

 

TableA:

load 

Customer id,

customer name ,

sales TW,

Sales LW

From Customer_table.qvd(qvd) where "Customer id"='$(vCustomer)' ;

 

// Now store and drop the table, using the variable to name the file

Store TableA into [lib://Folder/Customer $(vCustomer) .csv] (txt);

Drop table TableA;

//Next i will go to the next customer

Next i

 

Hope it helps.

Regards

Guilherme

View solution in original post

4 Replies
Gui_Approbato
Creator III
Creator III

Hello. Try this:

//First, load only the customer names

CustomersTable:

load  distinct 

"Customer id" as CustomerColumn

From Customer_table.qvd(qvd);

//Then, start your loop with a customer in a variable

For i=0 to NoOfRows('CustomersTable')-1

Let vCustomer = Peek('CustomerColumn',i,'CustomersTable');

 

// now include the WHERE clause with the variable at the end of your table

 

TableA:

load 

Customer id,

customer name ,

sales TW,

Sales LW

From Customer_table.qvd(qvd) where "Customer id"='$(vCustomer)' ;

 

// Now store and drop the table, using the variable to name the file

Store TableA into [lib://Folder/Customer $(vCustomer) .csv] (txt);

Drop table TableA;

//Next i will go to the next customer

Next i

 

Hope it helps.

Regards

Guilherme

New-Qlik
Creator II
Creator II
Author

Thank you for replying , it seems it is working but only issue is vCustomer is storing just till no 10

not any other customers after that so if i create a table in front end like 

CustomerColumn   $(vCustomer)

1                                     10

2                                     10

....

20                                 10

100                           10 ,.............

 

for all customer ids its just say 10 n then for storing csv also it stores 1-10 n after then nothing .

 

Gui_Approbato
Creator III
Creator III

If you know how many ids you have, you can change this piece:

For i=0 to NoOfRows('CustomersTable')-1    

The NoOfRows  can be replaced by the number of loops, like : For i=0 to 500 (if you have 500 ids).

Try this

New-Qlik
Creator II
Creator II
Author

i cannot hard code that number because no of customers keeps on increasing , it should pull form table only as per you expression but for some reason it juts same 10