@HubDoPete no custom code is needed so this is available solution for Pro subscriptions forward.
Workflow is deal based and is being triggered by checking that either the number of Associated line items on a deal is known or the Amount is known as often this also results in change in margin. We also have a manual trigger for rest of the usecases where we need to trigger the workflow.
Looping happens with the calculated properties (which is the setback of this solution as these are pretty restricted - just five (5) in Pro subscription). When using “Rollup” with SUM it sums distinct values (i.e. margin) per row and that comes out as the sum of all Line item Margins. With some filtering you can even get those done by product category.