Rollup
rollUp()
is particularly useful in the context of organizational hierarchy as it summarizes hierarchical information more efficiently than using node.d
It can be used to calculate the sum, average, count, maximum, or minimum of the property values for a node and its descendants
It takes the gereric form rollUp(aggregator, property)
Rollup Sum
The simple use of rollup
is to aggregate a property for a node and its decendants
An example would be to calculate total salary of a team
Use rollup
and define the aggregator and the property
node.rollUp("sum", "currentSalary")
Rollup excluding self
By default rollUp()
includes the current node (its self)
If you want the expression to exclude the current node then you can add an argument to the rollUp()
to "exclude self"
node.rollUp("sum", "currentSalary", { includeSelf: false })
Rollup with filters
If you have any filters applied, rollup()
will perform calculations on the filtered subset of your data
To do roll-up calculations on the unfiltered data, allrollUp()
should be used in the expression, instead of rollUp()
node.allrollUp("sum", "currentsalary")`
Rollup other aggregators
rollup()
can be used with any of the Aggregator Functions with sum
being replaced with avg
, cnt
, max
, min
to perform other operations depending on the situation.
To calculate the highest salary within a team
node.rollUp("max", "currentSalary")
Rollup with a lambda
rollup()
can be used with a Lambda expression to test for a condition being met
An example would be to calculate total salary of a team, but only of managers (those who have at least one direct report)
The lambda expression can be used which tests if each node is a manager node._is_leaf == false
node.rollUp("sum", n => (n._is_leaf == false ? n.currentsalary : 0))
i.e. for the target node the expression tests if the node is not a leaf, if the node passes the test the current salary is used otherwise the value of 0 is used