> For the complete documentation index, see [llms.txt](https://docs.valq.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.valq.com/model/formula-functions/range-functions/foreach.md).

# ForEach

When we have multiple nodes and want to execute the same operation for each of the node, we will use the ForEach function. It is not applicable for performing operation to a single node.

### Syntax

ForEach (Array,Iterator)

### Arguments

<table><thead><tr><th width="144">Arguments</th><th width="166">Datatype</th><th>Description</th></tr></thead><tbody><tr><td>Array</td><td>Node Name</td><td>The node for which the iterator statement is executed for all the periods.</td></tr><tr><td>Iterator</td><td>This, Children, Parent</td><td>The operation that is to be executed.</td></tr></tbody></table>

### Example

Consider the below scenario where we have Revenue and cost items for different regions. Our objective is to calculate the total costs incurred for marketing.

<table><thead><tr><th width="237">Year</th><th width="121" align="right">2020</th></tr></thead><tbody><tr><td><mark style="color:orange;"><strong>US Marketing (FOREACH)</strong></mark></td><td align="right"><strong>14000</strong></td></tr><tr><td>    <mark style="color:purple;"><strong>New York (Sum children)</strong></mark></td><td align="right"><strong>2510</strong></td></tr><tr><td>       <mark style="color:green;">Men (Subtract Children)</mark></td><td align="right">1000</td></tr><tr><td>          <mark style="color:yellow;">Revenue (Sum Children)</mark></td><td align="right">4500</td></tr><tr><td>               Sales </td><td align="right">4000</td></tr><tr><td>               Receivables</td><td align="right">500</td></tr><tr><td>          <mark style="color:red;">Costs (Sum Children)</mark></td><td align="right">3500</td></tr><tr><td>               <mark style="background-color:green;">Marketing</mark></td><td align="right"><mark style="background-color:green;">3000</mark></td></tr><tr><td>                Salary</td><td align="right">500</td></tr><tr><td> </td><td align="right"> </td></tr><tr><td>       <mark style="color:green;">Women (Subtract Children)</mark></td><td align="right">1510</td></tr><tr><td>           <mark style="color:yellow;">Revenue (Sum Children)</mark></td><td align="right">5510</td></tr><tr><td>                 Sales </td><td align="right">5000</td></tr><tr><td>                 Receivables</td><td align="right">510</td></tr><tr><td>            <mark style="color:red;">Costs (Sum Children)</mark></td><td align="right">4000</td></tr><tr><td>                  <mark style="background-color:green;">Marketing</mark></td><td align="right"><mark style="background-color:green;">2000</mark></td></tr><tr><td>                   Salary</td><td align="right">2000</td></tr><tr><td> </td><td align="right"> </td></tr><tr><td>    <mark style="color:purple;"><strong>LA (Sum children)</strong></mark></td><td align="right"><strong>-4120</strong></td></tr><tr><td>    <mark style="color:green;">Men (Subtract Children)</mark></td><td align="right">-1200</td></tr><tr><td>            <mark style="color:yellow;">Revenue (Sum Children)</mark></td><td align="right">4000</td></tr><tr><td>                   Sales </td><td align="right">3500</td></tr><tr><td>                   Receivables</td><td align="right">500</td></tr><tr><td>             <mark style="color:red;">Costs (Sum Children)</mark></td><td align="right">5200</td></tr><tr><td>                    <mark style="background-color:green;">Marketing</mark></td><td align="right"><mark style="background-color:green;">4000</mark></td></tr><tr><td>                    Salary</td><td align="right">1200</td></tr><tr><td> </td><td align="right"> </td></tr><tr><td>       <mark style="color:green;">Women (Subtract Children)</mark></td><td align="right">-2920</td></tr><tr><td>             <mark style="color:yellow;">Revenue (Sum Children)</mark></td><td align="right">4080</td></tr><tr><td>                     Sales </td><td align="right">3540</td></tr><tr><td>                     Receivables</td><td align="right">540</td></tr><tr><td>                <mark style="color:red;">Costs (Sum Children)</mark></td><td align="right">7000</td></tr><tr><td>                     <mark style="background-color:green;">Marketing</mark></td><td align="right"><mark style="background-color:green;">5000</mark></td></tr><tr><td>                     Salary</td><td align="right">2000</td></tr></tbody></table>

To achieve this, the following formula should be written in the <mark style="color:orange;">\[US Marketing]</mark> node,

<mark style="color:blue;">**`SUM`**</mark>**`(`**<mark style="color:blue;">**`FOREACH`**</mark>**`(`**<mark style="color:blue;">**`THIS`**</mark>**`.`**<mark style="color:blue;">**`CHILDREN`**</mark>**`,`**<mark style="color:blue;">**`FOREACH`**</mark>**`(`**<mark style="color:blue;">**`THIS`**</mark>**`.`**<mark style="color:blue;">**`CHILDREN`**</mark>**`.`**<mark style="color:blue;">**`THIS`**</mark>**`,`**<mark style="color:blue;">**`THIS`**</mark>**`.`**<mark style="color:blue;">**`CHILDREN`**</mark>**`.`**<mark style="color:blue;">**`GET`**</mark>**`(2).`**<mark style="color:blue;">**`CHILDREN`**</mark>**`.`**<mark style="color:blue;">**`GET`**</mark>**`(1))))`**

### **Explanation**

<table><thead><tr><th width="267"></th><th></th></tr></thead><tbody><tr><td>(FOREACH (THIS.CHILDREN,</td><td>As we are entering the formula in the parent node of both regions - New York and LA , THIS.CHILDREN will refer to each of the children of <mark style="color:orange;">[US Marketing]</mark> node which is <mark style="color:purple;">[New York]</mark> and <mark style="color:purple;">[LA]</mark></td></tr><tr><td>FOREACH (THIS.CHILDREN.</td><td>This will refer to each of the child nodes of <mark style="color:purple;">[New York]</mark> and <mark style="color:purple;">[LA]</mark> which are <mark style="color:green;">[Men]</mark> and <mark style="color:green;">[Women]</mark></td></tr><tr><td>THIS,</td><td>This refers to that same node so it will be <mark style="color:green;">[Men]</mark> and <mark style="color:green;">[Women]</mark> </td></tr><tr><td>THIS.CHILDREN.GET(2).</td><td>This will consider the second child <mark style="color:red;">[Costs]</mark> of the Men and Women nodes as we have mentioned Get(2)</td></tr><tr><td>CHILDREN.GET(1) )))</td><td>This will get the value of the first child of <mark style="color:red;">[Costs]</mark> that is <mark style="background-color:green;">[Marketing]</mark>. Sum of these <mark style="background-color:green;">[Marketing]</mark>nodes are returned which is 14000.</td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.valq.com/model/formula-functions/range-functions/foreach.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
