ForEach
Last updated
Last updated
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.
ForEach (Array,Iterator)
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.
To achieve this, the following formula should be written in the [US Marketing] node,
SUM
(
FOREACH
(
THIS
.
CHILDREN
,
FOREACH
(
THIS
.
CHILDREN
.
THIS
,
THIS
.
CHILDREN
.
GET
(2).
CHILDREN
.
GET
(1))))
Array
Node Name
The node for which the iterator statement is executed for all the periods.
Iterator
This, Children, Parent
The operation that is to be executed.
US Marketing (FOREACH)
14000
New York (Sum children)
2510
Men (Subtract Children)
1000
Revenue (Sum Children)
4500
Sales
4000
Receivables
500
Costs (Sum Children)
3500
Marketing
3000
Salary
500
Women (Subtract Children)
1510
Revenue (Sum Children)
5510
Sales
5000
Receivables
510
Costs (Sum Children)
4000
Marketing
2000
Salary
2000
LA (Sum children)
-4120
Men (Subtract Children)
-1200
Revenue (Sum Children)
4000
Sales
3500
Receivables
500
Costs (Sum Children)
5200
Marketing
4000
Salary
1200
Women (Subtract Children)
-2920
Revenue (Sum Children)
4080
Sales
3540
Receivables
540
Costs (Sum Children)
7000
Marketing
5000
Salary
2000
(FOREACH (THIS.CHILDREN,
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 [US Marketing] node which is [New York] and [LA]
FOREACH (THIS.CHILDREN.
This will refer to each of the child nodes of [New York] and [LA] which are [Men] and [Women]
THIS,
This refers to that same node so it will be [Men] and [Women]
THIS.CHILDREN.GET(2).
This will consider the second child [Costs] of the Men and Women nodes as we have mentioned Get(2)
CHILDREN.GET(1) )))
This will get the value of the first child of [Costs] that is [Marketing]. Sum of these [Marketing]nodes are returned which is 14000.