DAZL Documentation | Data Analytics A-to-Z Processing Language


Contents

forEach

Flow Control

slug: step-foreach

ForEach Step

Purpose

Iteratively executes a series of steps for each value in a specified collection, enabling batch processing, multi-scenario analysis, and automated workflows. Acts as a dynamic loop mechanism for repetitive data processing tasks.

When to Use

  • Process each distinct value in a column (e.g., each region, product, or category)
  • Iterate through a predefined list of values
  • Generate multiple outputs with different parameters
  • Create separate reports for each segment
  • Apply consistent transformations across multiple datasets
  • Automate repetitive processing tasks
  • Build multi-scenario analysis pipelines

How It Works

  1. Takes a collection of values and a set of steps to execute
  2. For each value in the collection:
    • Sets a variable with the current value
    • Executes the specified steps with this variable available
    • Processes the steps in sequence, maintaining context between steps
  3. Handles two modes of operation:
    • Fixed list iteration: Iterates through explicitly provided values
    • Column-based iteration: Extracts unique values from a dataset column
  4. Returns the final state after all iterations complete
  5. Provides detailed logging of loop execution for debugging

Parameters

Required

At least one of:

  • values (array) - Explicit list of values to iterate through
  • column (string) - Column name to extract unique values from

Plus:

  • steps (array) - Step definitions to execute for each value

Optional

  • varName (string) - Name of the variable to set for each iteration (default: item)

Input Requirements

  • For column-based iteration, dataset must contain the specified column
  • Step definitions must be valid and executable
  • Variable name must be a valid identifier

Variable Access

  • Each iteration sets a variable (default name: item) with the current value
  • This variable is accessible in all steps and expressions within the loop
  • Example usage in expressions: $item or ${varName}

Example Usage

Iterate Through Fixed Values

forEach:
  values: [2023, 2024, 2025]
  varName: year
  steps:
    - filter:
        where: "transaction_year = ${year}"
    - cube:
        dimensions: [region, product_category]
        measures: [revenue, units_sold]
    - print:
        title: "Sales Report - ${year}"
    - export:
        filename: "sales_${year}.xlsx"

Process Each Unique Category

forEach:
  column: product_category
  varName: category
  steps:
    - filter:
        where: "product_category = '${category}'"
    - sort:
        by: ["-revenue"]
    - calculate:
        assign:
          category_rank: "$rank"
          category_share: "$revenue / $total_revenue * 100"
    - chart:
        type: bar
        x_axis: product
        y_axis: revenue
        title: "Revenue by Product - ${category}"

Use Cases

Multi-Segment Reporting

Generate individual reports for each business segment:

forEach:
  column: segment
  varName: segment
  steps:
    - filter:
        where: "segment = '${segment}'"
    - cube: { /* segment analysis */ }
    - chart: { /* segment visualization */ }
    - export:
        type: pdf
        filename: "${segment}_report.pdf"

Parameterized Analysis

Run the same analysis with different parameters:

forEach:
  values: [7, 14, 30, 90]
  varName: days
  steps:
    - timeSeries:
        date_field: transaction_date
        operations:
          - type: sma
            column: revenue
            period: ${days}
    - chart:
        type: line
        x_axis: transaction_date
        y_axis: "revenue_sma_${days}"
        title: "${days}-Day Moving Average"

Related Documentation

  • executeIf-step - Conditional execution based on conditions
  • while-step - Execute steps while a condition remains true
  • map-step - Transform a collection with a function
  • log-step - Log information during iteration
  • setVariable-function - How variables are managed during execution