Dynamic/virtual subtraction of values

Hi!
I’m trying a “simple” calculation since days, it’s frustrating, maybe someone can give me a hint.
I want to simulate inventory movements: subtract orders from inventory.
But I really don’t know how to handle it, since at the start point (first row) EM has to fetch the “real” stock amount, but after the second entry it has to regard the virtually calculated new stock.
I tried merging, iterating, calling, aggregating …

Example 1st table (orders):
| order no. | item | amount |
| 1 | ABC | 10 |
| 2 | DEF | 5 |
| 2 | ABC | 5 |
| 3 | DEF | 15 |

Example 2nd table (“real” stock):
| item | stock |
| ABC | 100 |
| DEF | 100 |

Goal:
| order no. | item | amount | stock |
| 1 | ABC | 10 | 90 (100-10) |
| 2 | DEF | 5 | 95 (100-5) |
| 2 | ABC | 5 | 85 (90-5) |
| 3 | DEF | 15 | 80 (95-15) |

Many thanks in advance!!
Regards, Michael

Hi Michael,

the approach here is to have for each item an initial (opening) balance, then calculate the running cumulative amount of orders, and finally subtract the cumulative amount from the initial balance – that would give us the ending (closing) balance for each order (transaction).

See the example below with your data.

running-balance.morph (8.4 KB)
Book1.xlsx (9.0 KB)

When you see it, it’s so simple. :sweat_smile:
“Running total” is the key; thank you so much for this hint as well as your quick & awesome support!!!

You’re welcome! :slight_smile: