Okay, so I got Gemini to help with this in the sheet.
Insert a column to the right of column C (position D).
In row 7 add PREV 12 MO as header.
Row 10 (cash flow row) add this formula into D10
=SUM(MAP($BT$2:$BT, LAMBDA(cat, IF(cat="",, SUMIFS(Transactions!$E:$E, Transactions!$D:$D, cat, Transactions!$B:$B, ">="&EDATE(E$7, -12), Transactions!$B:$B, "<="&EOMONTH(E$7, -1))))))
Then in Row 12 (income row) add this formula to D12:
=if( isblank(E$8), IFERROR(1/0), LET( prev_totals, MAP($BT$2:$BT, LAMBDA(cat, IF(cat="",, SUMIFS(Transactions!$E:$E, Transactions!$D:$D, cat, Transactions!$B:$B, ">="&EDATE(E$7, -12), Transactions!$B:$B, "<="&EOMONTH(E$7, -1))))), MAP( SEQUENCE(max($BQ$2:$BQ)+1,1,0), LAMBDA(row, IFERROR( INDEX(prev_totals, MATCH(row, $BQ$2:$BQ, 0)) * if(XLOOKUP(row, $BQ$2:$BQ, $BR$2:$BR)="Expense",-1,1), IFS( NOT(isna(match(row, $BP$2:$BP,0))), LET( type, XLOOKUP(row, $BP$2:$BP, $BR$2:$BR), group, XLOOKUP(row, $BP$2:$BP, $BS$2:$BS), total, if(type="Expense",-1,1) * IFERROR(SUM(FILTER(prev_totals, $BS$2:$BS=group, $BR$2:$BR=type)), 0), if(total=0,,total) ), NOT(isna(match(row, $BO$2:$BO,0))), LET( type, XLOOKUP(row, $BO$2:$BO, $BR$2:$BR), total, if(type="Expense",-1,1) * IFERROR(SUM(FILTER(prev_totals, $BR$2:$BR=type)), 0), if(total=0,,total) ), true, ) ) ) ) ) )
It should update dynamically based on the period you have selected at the top. You can move the frozen column line over to the right if you don’t want column D to move out of view as you scroll. here’s what it looks like:
I did validate it in the dummy dataset I was using and the output of the calculations look correct, but if something seems off, I’d recommend using Gemini or some other AI to try and help fix it, since I didn’t build this myself 