In fact, it is weird contract if we required in script. But, Try a luck use RangeSum() for your 2 Sum() expressions. I don't prefer to use this as script due to nested aggregation issue. Can this possible to use same in UI rather Script.
Before develop something, think If placed (The Right information | To the right people | At the Right time | In the Right place | With the Right context)