Spreadsheet: 713701d1610888606-sum-if-criteria-by-grouping-period-grouping-sums
Sheet: Example

Spendings |  |  |  |  |  | Budgeted per period | 100 |  |  |  |  |  |  |  |  |  | Debug calculations |  |  |  |  | Attempts at formulas
 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  | This is an attempt to create a single formula to calculate column H without relying on extra tables (such as the debug culculations in column J-M).
 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  | Debugging values:
Date | Spent | Period |  |  | Period | Spent | Total available amount | Extra-Budget top-up |  |  |  |  |  |  |  | Period | Incoming bonus | Budget result | Outgoing bonus | Running sum
2020-12-27 00:00:00 | 10 |  |  |  | 1 |  |  |  |  |  |  |  |  |  | 1 | 0 |  |  |  |  | Attempt 1 | Attempt 2
2021-01-01 00:00:00 | 15 |  |  |  | 2 |  |  |  |  |  |  |  |  |  | 2
2021-02-02 00:00:00 | 75 |  |  |  | 3 |  |  |  |  |  |  |  |  |  | 3
2021-03-03 00:00:00 | 80 |  |  |  | 4 |  |  |  |  |  |  |  |  |  | 4
2021-03-15 00:00:00 | 40 |  |  |  | 5 |  |  |  |  |  |  |  |  |  | 5
2021-04-04 00:00:00 | 30 |  |  |  | 6 |  |  |  |  |  |  |  |  |  | 6
2021-05-05 00:00:00 | 50 |  |  |  | 7 |  |  |  |  |  |  |  |  |  | 7 | 0
2021-06-06 00:00:00 | 100 |  |  |  | 8 |  |  |  |  |  |  |  |  |  | 8
2021-06-16 00:00:00 | 230 |  |  |  |  |  |  |  |  |  |  |  |  |  | 9
2021-07-07 00:00:00 | 35 |  |  |  |  |  |  |  |  |  |  |  |  |  | 10
2021-08-08 00:00:00 | 60 |  |  |  |  |  |  |  |  |  |  |  |  |  | 11
 |  |  |  |  |  |  |  |  |  |  |  |  |  |  | 12
 |  |  |  |  |  |  |  |  |  |  |  |  |  |  | 13
 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  | Find period by date: | 2021-07-07 00:00:00
 |  |  |  |  | Scenario
Periods |  |  |  |  | Imagine a big bucket that at the start of every period has the budgeted amount (100) added to it.
 |  |  |  |  | The spendings for each period is withdrawn from the bucket, leaving any remainder behind for the upcoming periods.
 |  |  |  |  | When the bucket is empty (i.e. the "outgoing bonus" is <= 0), then it is empty for the next period.
Start | Stop | Period |  |  | If the "outgoing bonus" is negative, some external force will cover that debt, so the outgoing bonus is reset to zero.
 | 2020-12-22 00:00:00 |  |  |  | No debt shall be carried forth to the next period!
2020-12-23 00:00:00 | 2021-01-24 00:00:00 | 1
2021-01-25 00:00:00 | 2021-02-24 00:00:00 | 2 |  |  | Real-life scenario:
2021-02-25 00:00:00 | 2021-03-24 00:00:00 | 3 |  |  | Imagine that every month you budget 100 for fuel for your car.
2021-03-25 00:00:00 | 2021-04-22 00:00:00 | 4 |  |  | If you don't spend it all, you can use the surplus in the upcoming months.
2021-04-23 00:00:00 | 2021-05-24 00:00:00 | 5 |  |  | However, if you spend more than you have, you'll have to borrow from someone else.
2021-05-25 00:00:00 | 2021-06-24 00:00:00 | 6 |  |  | In the case of this budget, you conveniently forget that you borrowed to cover your debt and don't let it affect your budget.
2021-06-25 00:00:00 | 2021-07-22 00:00:00 | 7
2021-07-23 00:00:00 | 2021-08-24 00:00:00 | 8
2021-08-25 00:00:00 | 2021-09-23 00:00:00 | 9
2021-09-24 00:00:00 | 2021-10-24 00:00:00 | 10
2021-10-25 00:00:00 | 2021-11-24 00:00:00 | 11
2021-11-25 00:00:00 | 2021-12-22 00:00:00 | 12
2021-12-23 00:00:00 | 2022-01-24 00:00:00 | 13
2022-01-25 00:00:00