# Aggregating nested loops into a simple loop

Convert an XML structure with nested loops into a flat CSV structure by aggregating nested loops.

## Before you begin

You have created a map and added an input and an output structure.

## About this task

This example uses the same input structure as in [Combining nested loops into a simple loop](https://help.qlik.com/talend/en-US/data-mapper-user-guide/8.0/combining-nested-loops-into-simple-loop). The goal of the transformation is to have a CSV file with a row for each Address element containing information about the delivery and the products.

The output structure contains multiple PartNumber and ProductName elements so that each row can contain all the products to be delivered to a specific address. It is defined as follows:

```
AddressesShipping
	Address (0:*)
		Name
		Street
		City
		State
		Zip
		Country
		DeliveryNotes
		PartNumber_1
		ProductName_1
		PartNumber_2
		ProductName_2
		PartNumber_3
		ProductName_3
```

## Procedure

1. Drag the following input elements to the corresponding output elements:
   * Name
   * Street
   * City
   * State
   * Zip
   * Country
   * DeliveryNotes
2. Drag the input PartNumber element to the output PartNumber\_1.

   Since the output structure is already looping on the input Address element, this causes a conflict.
3. In the dialog box that opens, select Generate AgConcat value expression and click OK.

   With this option, all values for the PartNumber elements contained in each Address element are aggregated in a single field using an AgConcat function. If you want to have a single value per field, you need to use a filter. ![](https://help.qlik.com/talend/en-US/data-mapper-user-guide/Resources/images/aggregate_nested_into_simple_01_agconcat.png)
4. Drag a SingleIndex function to the Filter argument in the Value tab of the output PartNumber\_1 element.

   To get the value of the first PartNumber element, you can leave the default value for the index.
5. Drag the input PartNumber element to the output PartNumber\_2 and PartNumber\_3 elements, then select Generate AgConcat value expression in the dialog boxes that open and click OK.

   The filters are automatically configured based on the expression defined in PartNumber\_1. ![](https://help.qlik.com/talend/en-US/data-mapper-user-guide/Resources/images/aggregate_nested_into_simple_02_filters_part.png)
6. Repeat the previous steps to map the input ProductName element to the output ProductName\_1, ProductName\_2 and ProductName\_3 elements.
   ### Example

   ![](https://help.qlik.com/talend/en-US/data-mapper-user-guide/Resources/images/aggregate_nested_into_simple_03_filters_product.png)

## Results

Your mapping is configured. If you have a sample document, you can click Test Run to see the result. In this example, it should look like this:

```
Name,Street,City,State,Zip,Country,DeliveryNotes,PartNumber_1,ProductName_1,PartNumber_2,ProductName_2,PartNumber_3,ProductName_3
Deandre King,4894  Winding Way,Southfield,MI,48075,USA,Please leave packages in shed by driveway.,872-AA,Lawnmower,926-AA,Baby Monitor,,
Burl Clark,3807 Pointe Lane,Fort Lauderdale,FL,33308,USA,,356-KX,Gas canister,,,,
```