Nested blocks

You can nest ForEach loops and If blocks inside one another to any depth.

You can place an If block inside a ForEach loop, a ForEach loop inside an If block, or nest blocks/loops of the same type. There is no limit on nesting depth.

  • If inside ForEach — Evaluate a condition on each iteration. For example, loop over rows and only export when a column value meets a threshold.
  • ForEach inside If — Run a loop only when a condition is true. For example, check whether a data table has rows before iterating over them.
  • If inside If — Chain conditions. There is no Else If task; use an If block inside the Else task instead.
  • ForEach inside ForEach — Iterate over combinations of columns. For example, loop over regions in an outer loop and over products in an inner loop.

Building nested blocks

  • Add If and/or ForEach blocks to the job.
  • Tasks inside a block are shown indented according to how deeply they are nested.
  • Select a block and use Move Up and Move Down to reorder tasks. When you move a block, the whole block moves as a unit, including everything in it.
  • Dragging a block past the boundary of another block shifts it into or out of that block, one nesting level at a time.
  • Duplicating a block copies the block itself (its start and end markers), not the tasks inside it.

Referencing columns in nested ForEach loops

Inside nested ForEach loops, the {forEachProperty:<column>} field variable can reference columns from either the current loop or any enclosing outer loop.

If an inner and outer loop have a column with the same name, the value of the inner loop is used.

Flow control in nested blocks

  • Using Break stops the innermost ForEach loop, even when placed inside an If block. Any outer loops continue to run.
  • Using Exit ends the entire job regardless of nesting depth.

Example

The following job iterates over regions and products, skipping discontinued products. The Break stops the inner product loop but the outer region loop continues:

Open Analysis from Library
ForEach (Data Table: "Regions", Column: "Region")
    ForEach (Data Table: "Products", Column: "Product")
        If
            result = "{forEachProperty:Product}" == "Discontinued"
            Break
        End If
        Export Data
    ForEach End
ForEach End