# How can I achieve the following logic

**URL:** <https://forum.knime.com/t/how-can-i-achieve-the-following-logic/45526>\
**Category:** KNIME Analytics Platform\
**Created:** [July 28, 2022, 5:34am UTC](https://forum.knime.com/t/how-can-i-achieve-the-following-logic/45526 "2022-07-28T05:34:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamasewidan](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lamasewidan](https://forum.knime.com/u/lamasewidan)\
**Post date:** [July 28, 2022, 5:34am UTC](https://forum.knime.com/t/how-can-i-achieve-the-following-logic/45526/1 "2022-07-28T05:34:12Z")

</div>

This is what the initial data would look like.  
 ![Capture](https://forum-cdn.knime.com/uploads/default/original/3X/9/b/9b65f42d40b30145035b9aa9a7d1737a506a250f.png)

I want to look ONLY at row #1 and do the following: Stock-orders and the result would be identified by “gap” (maybe a new column named gap).  
And then we move to row #2, if the gap of row#1 is \>=0 subtract the orders of row1 from the stock of row 2 and this would be my new stock for row#2, else keep the stock of row#2 as it is  
then calculate the gap for row#2 like we did for row#1, stock-orders (stock of row#2-orders of row#2 ofcourse)  
and then we move to row#3  
if the gap of row#2 is \>=0 subtract the orders of row2 from the stock of row 3 and this would be my new stock for row#3, else keep the stock of row#3 as it is  
then calculate the gap for row#3 like we did for row#2 and 1, stock-orders (stock of row#3-orders of row#3 ofcourse)  
and then we keep doing the same thing if there are more rows.  
I had 2 issues with doing this logic in knime; the first is going through every row individually which only the chunk loop does, the second is bringing back the gap of a row to be used in the math of the row after it and this can only be achieved by the recursive loop  
but I’m note sure how to merge the 2 loops together in knime  
Please let me know if you have any ideas!

---

<div class="post-metadata">

**Author:** ![dora\_gcs](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/e9c0ed/32.png) [@dora\_gcs](https://forum.knime.com/u/dora_gcs)\
**Post date:** [July 28, 2022, 8:55am UTC](https://forum.knime.com/t/how-can-i-achieve-the-following-logic/45526/2 "2022-07-28T08:55:48Z")

</div>

Hello @lamasewidan ,

welcome in the Forum.

Please check this Forum post from the past I believe that is what you are looking for:

> [@Roling Calculation](https://forum.knime.com/t/roling-calculation/5730):
>
> Hi all How can I do some rolling calculations ? The ideia is to get something like the following table Data1 Data2 DataVal calculated row1 x 1 0 row2 y 3 calc(row2)=dataval(row1)-dataval(row2) row3 z 4 calc(row3)=dataval(row2)-dataval(row3) row4 c 7 calc(row4)=dataval(row3)-dataval(row4) Thanks !

Good luck!

---

<div class="post-metadata">

**Author:** ![lamasewidan](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lamasewidan](https://forum.knime.com/u/lamasewidan)\
**Post date:** [July 28, 2022, 10:29am UTC](https://forum.knime.com/t/how-can-i-achieve-the-following-logic/45526/3 "2022-07-28T10:29:49Z")

</div>

Hello , I looked at that forum post but I’m not sure how it could be used to achieve what im looking for. The difference between my post and that forum post is that I have a column that is being updated after every row iteration. So not all columns should go through the math’s at once as the stock column is updated after every row. I hope I explained well enough for you to get the picture  
@dora_gcs

---

<div class="post-metadata">

**Author:** ![ScottF](https://forum-cdn.knime.com/user_avatar/forum.knime.com/scottf/32/61316_2.png) [@ScottF](https://forum.knime.com/u/ScottF)\
**Post date:** [July 28, 2022, 2:30pm UTC](https://forum.knime.com/t/how-can-i-achieve-the-following-logic/45526/4 "2022-07-28T14:30:49Z")

</div>

Hi @lamasewidan -

It appears this is a duplicate of the discussion already going on in your other thread here:

> [@how to use recursive and chunk loop in the same time](https://forum.knime.com/t/how-to-use-recursive-and-chunk-loop-in-the-same-time/45518/2):
>
> Hi @lamasewidan , could you share some data and show what the expected results for the first 3 lines, and of course explain how you got to these values? This will help illustrate what you are trying to do, and there might be a more straight forward way to do this instead of using 2 loops nested in one another.

Please continue the discussion there. I will close this thread to help keep the forum tidy.

---

<div class="post-metadata">

**Author:** ![ScottF](https://forum-cdn.knime.com/user_avatar/forum.knime.com/scottf/32/61316_2.png) [@ScottF](https://forum.knime.com/u/ScottF)\
**Post date:** [July 28, 2022, 2:31pm UTC](https://forum.knime.com/t/how-can-i-achieve-the-following-logic/45526/5 "2022-07-28T14:31:05Z")

</div>


