Thanks for all, I've got the solution to my problem.
I've combined all your suggestions and I've used the Analytic Query
, Modified Javascript
and Group by
steps.
Although the question wasn't very well formulated, the problem I had was to calculate the stock level on each row (there was one row for each product, date and store combination).
First (obviously later than sort rows by product_id
, store_id
and date
ascending), I used the Analytic Query
step to group by product_id
and store_id
, because with this step I've got a new field previous_date
to identify the first row of each group (previous_date=null
on the row of the group where date was the oldest).
Then I needed to calculate the quantity_on_hand
of each group [product,store] at first row (first date of each group because it's sorted by date) because the initial_stock
is different for each group. This is because of (sum(quantity_received) - sum(quantity sold)) != quantity_on_hand
.
Finally (and the key was here), I used the Group by
step like @andtorg suggested and do it as the next image shows.
This link that @andtorg suggested was very useful. It includes even two .ktr
example files.
Thank you so much for help!