Using data from previous row in calculation with loop

Hi @Aura02 , I was confused about the “formula” column included in your screenshot that makes no sense to me, so I ignored it, and just worked on the basis that

Balance = (initial stock if first row for product, or previous Balance otherwise) - Client orders + incoming stock

This doesn’t require loops, and can be achieved using Column Expressions formula:

/* Items to "remember" for next row -- do not initialise them */
var lastProduct
var lastBalance

// processing for the current row...

if (column("Product") != lastProduct )
// this is the first row for this product
    lastBalance = column("Initial Stock") // initialise lastBalance to initial Stock Level
    lastProduct = column("Product") // remember the lastProduct

lastBalance = lastBalance - column("Client orders") +column("Incoming Stock")

// return the balance for current row

Cumulative Stock Calculation.knwf (73.0 KB)

for info on how this works, see