FIFO stock calculation

Hi guys, i have a dataset as below, with the desired output (named: “init rate”) at the last column:

| ID | Date_order | Production_weight | Production_rate | Stock_Available | Desired_output |
| A  | 0          | 100000            | 4.2             | 0               | 4.20           |
| A  | 1          | 30000             | 4.5             | 100000          | 4.27           |
| A  | 2          | 26000             | 4.4             | 130000          | 4.29           |
| A  | 3          | 30000             | 4.5             | 132000          | 4.33           |
| A  | 4          | 26000             | 4.4             | 138000          | 4.34           |

“Init rate” will be the name of the desired output column

The logic is, which will apply for each ID (many IDs will be in the dataset, I use only one for example):

  • If the date order is 0, then the “init rate” is equal to the “production rate”

  • Else the formula will be calculated as follows:

(“Stock Available” “Init Rate for previous date” + “Production weight” “Production Rate”) / (“Production weight” + “Stock Available”)

for example, row 1: (100000 4.2 + 30000 4.5) / (100000 + 30000) = 4.27

The tricky part is you have to use the newly calculated “init rate” of the previous row for the current row calculation.

I can archive it easily in Alteryx, but I am not sure how to do it in Knime.

Thank you for your help.