Transform data with a ForEach activity
The ForEach Activity defines a repeating control flow in a Microsoft Fabric pipeline. This activity is used to iterate over a collection and executes specified activities in a loop. The loop implementation of this activity is similar to a ForEach looping structure in programming languages.
Add a ForEach activity to a pipeline
This section describes how to use a ForEach activity in a pipeline.
Prerequisites
To get started, you must complete the following prerequisites:
- A tenant account with an active subscription. Create an account for free.
- A workspace is created.
Creating the activity
Create a new pipeline in your workspace.
Search for ForEach in the pipeline Activities pane, and select it to add it to the pipeline canvas. If you can't see it initially, use the arrow on the right side of the activities toolbar to scroll to the right to find it.
Select the new ForEach activity on the canvas if it isn't already selected.
In the General tab at the bottom of the canvas, enter a name for the activity in the Name property.
(Optional) You can also enter a description.
ForEach settings
Select the Settings tab, where you can specify whether processing of the items in the batch should be Sequential (or otherwise in parallel). You can also specify a maximum number of items to process at the same time with Batch count. Finally, you must specify a list of comma delimited Items, which can be parameterized or include dynamic content. Add a few items to the Items list as shown in the example.
ForEach activities
You'll also define an activity or activities to be performed on each of the items in the list, in the ForEach Activities pane.
Select the + button to add a new activity to the pane. You'll see a list of activities to choose. You can add multiple activities to the ForEach activity, and each is run on each of the items in the Items list. Whether the Sequential option is selected in the ForEach settings or not, each of the child activities in the ForEach activities pane are processed sequentially to one another for each item. However, if Sequential isn't selected, multiple items are processed in parallel, each of them running sequentially through the list of child activities specified.
Referencing an item within an activity
Select one of the child activities in the ForEach Activities pane, and switch to its Settings tab. In this example, a Stored Procedure activity was selected. Populate the settings for the activity as you normally would select a connection and stored procedure. You can use the @item() iterator to refer to the current item being processed anywhere within an activity that supports dynamic content. Here the @item() was used as the value for the FruitName parameter that is passed to a stored procedure.