Need Help with Logic for Billing Reconciliation

Hi everyone! Would like to seek help on how the logic for the following should work. Due to the nature of my work laptop, I can’t upload or download .xaml files and will hence rely on screenshots.

I am doing a bot for Billing Reconciliation. There is a Master PO Number issued (Column A - ID Code) and there are sub-POs issued under this Master PO (Column D - Sys Code) when teams make purchases. When billing is done, teams either quote the ID Code or Sys Code when issuing invoices.

Hence, in my reconciliation report, there are 3 scenarios that my bot needs to handle in order to produce the correct Total Amount Billed and Outstanding Balance left under each ID Code to ensure that we do not double-count numbers.

Can I seek someone’s assistance for the Activities needed to account for all 3 scenarios? I have included my current incorrect output, and the desired outcome that needs to be produced.

Any help will be greatly appreciated! Thank you so much.