The first thing you should as yourself when you have a conditional expression is: Can this problem be solved with Set Analysis?
The reason is that Set Analysis is always faster if the data amount is large. Hence, Set Analysis should be the preferred solution for conditional aggregations. See
The second question is: Can I prepare some of the calculations in the script, so that less calculations are made during the actual session?
If you in the script create Boolean fields - flags - the calculations in the UI will be faster and easier to express. See e.g.