Dynamic Cumulative Calculation for Column E Based on Names and Values in Google Sheets

I need assistance with a Google Sheets formula for Column E.
In my sheet, Column B contains NAMES, Column C has LEND amounts, Column D lists RETURNS, and Column E is meant to display the BALANCE.
The formula should compute the running total for each row by considering the following criteria:
- The same name continues from Column B.
- The cumulative total resets for different names in Column B.
For the first appearance of a name in Column B, the formula is:
E = C - D.
For subsequent occurrences of the same name, it should be:
E = Previous ‘E’ for that name + C - D.
When a new name appears, the balance calculates again as:
E = C - D.
I’ve attempted several approaches but haven’t achieved the desired outcome.

To achieve this in Google Sheets, you can use a combination of array formulas and conditions. Try using the IF and SUMIF functions to handle the reset and cumulative logic. Start with an IF condition to check if the current row’s name is the first occurrence. Using a unique identifier for names can help detect whether it’s a new name or a continuation. No doubt, this setup can be quite tricky, and trial and error might be necessary to fine-tune the formula. Consider breaking it down into smaller parts to verify accuracy as you build it.

Hi, you could also explore using the FILTER function combined with ARRAYFORMULA. This would allow you to calculate balances across multiple rows efficiently. By dynamically filtering based on name, you can pull only relevant rows for each calculation. Just a thought, but could help make it simpler.