Aller au contenu

Leçon 19 · Macros and Apps

Batch and Iterative Macros

Understand Alteryx batch macros with control parameters and iterative macros that loop until done, including iteration limits and how to debug them.

Standard macros run once. Two other kinds repeat work. A batch macro runs once per record or group of control values. An iterative macro feeds rows back into itself until a condition is met. They are powerful, and also easy to get wrong.

Use them only after you have checked that a simpler tool, such as a join, a Generate Rows tool or a Summarize, cannot do the job.

Batch macros

A batch macro contains a Control Parameter tool. The workflow connects a control stream to it. For each control record (or each group, if you set grouping), the macro runs from start to end with that value. At the end, the results of all runs are combined (unioned) into one output.

Typical examples:

  • Process each file in a list, even if each needs its own query.
  • Call a query once per region.
  • Create a separate report per manager.

An Action tool applies the control value inside the macro, for example to change a filter or a file path.

Because outputs are unioned, runs with different columns can cause trouble. The macro settings offer ways to match the results, such as by name or by position. The same advice as for the Union tool applies.

Python Stand-in: a batch loop that unions the results

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note West: the stand-in produces a row with zeros. Your real macro might produce no rows for that value. Decide which behavior you need and test it.

Iterative macros

An iterative macro has an iteration input and an iteration output. Rows that come out of the iteration output go back into the iteration input for the next pass. The loop stops when no rows come out of the iteration output, or when it reaches the maximum number of iterations.

flowchart LR
  A["Macro Input"] --> P["Process rows"]
  P -->|done| O["Macro Output<br/>finished rows"]
  P -->|not done| I["Iteration Output"]
  I -.next pass.-> A

Rows that need more work go around again.

Typical uses are walking through a hierarchy (who reports to whom), paging through an API until no more pages exist, or repeating a calculation until values settle.

Set a maximum iteration count and choose what happens when it is reached. The Designer help describes choices such as stopping with an error, or warning and passing leftover records. A silent limit can produce partial results, so choose the behavior that tells you loudly when it happens.

Python Stand-in: climb a management hierarchy with a loop limit

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The cycle between x and y would loop forever without a limit. Bad data like this exists in real systems.

The same logic in SQL

Sometimes a database does it better. A recursive query walks a hierarchy in one statement:

SQL Stand-in: recursive SQL for the same hierarchy

Press Run (or Ctrl + Enter) to see the result here.

See recursive CTEs to learn more.

Debugging loops

  • Test with a tiny input where you can predict every pass.
  • Keep a pass counter field and look at it in the output.
  • Put Browse tools inside the macro while testing, and run the macro with its sample inputs.
  • Check that every row eventually leaves the loop.

Engines and parallelism

The AMP engine can run parts of a workflow in parallel. The 2026.3 release notes say batch macros on AMP can now run iterations in parallel. If your iterations write the same temporary file or table, parallel runs can collide, so make names unique. AMP and the older engine can also differ in how they handle errors inside iterative macros, so test on the engine you use in production.

Choosing the right macro

Need Choose
Reuse steps once per call Standard macro
Repeat for each control value Batch macro
Repeat until a condition is met Iterative macro
Match, group or sum rows Normal tools or SQL

Handling failures inside a batch

With many runs, some may fail: one file is damaged, or one region has an unexpected column. Decide in advance what should happen. Stopping the whole workflow is simple and safe for financial loads. Recording the failure and continuing with the other values can suit exploratory jobs, but it takes extra design and testing. In both cases, aim for a run log with one row per control value: the value, the number of records and a status.

Compare the log with the control stream at the end. Every control value should appear in it. A missing value is easy to overlook, especially when there are hundreds of them, and the combined result gives no hint that something was skipped.

Practice task

Take three small tables, one for each of three regions, and design a batch process that totals each one. Write down the control values, what the Action tool changes on each run and the columns that every run must return, so that the results can be combined. Then add a fourth region that has no data and decide what its output row should look like.

For the iterative case, draw a folder tree four levels deep as a table with folder and parent columns. Predict which rows leave the loop on each pass when you search for all folders below the top one. Count the passes by hand, set a limit somewhat above that number and explain what you want to happen if the limit is ever reached.

Common mistakes

Never leave a loop without a sensible maximum. An endless loop can burn hours of machine time or flood an external API.

  • Calling an API in a loop without respecting its rate limits.
  • Assuming batch results arrive in control order. Add a column with the control value and sort.
  • Using a batch macro where one Join would be faster and clearer.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What drives each run of a batch macro?

    The macro runs once per control record or group.
  2. 2. When does an iterative macro normally stop?

    It loops until nothing is sent back or the maximum is reached.
  3. 3. What happens to batch macro outputs at the end?

    Results of all iterations are unioned together.
  4. 4. Which problem can a cycle in hierarchy data cause?

    Cyclic references keep rows going around, so a limit protects you.

Summary

  • Batch macros run once per control value and union the results.
  • Iterative macros feed rows back until done or until the limit.
  • Always set a limit and decide what happens when it is reached.
  • Test loops with tiny inputs and consider simpler tools or SQL first.

Next: Analytic Apps and Interface Tools

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.