# Java Snippet - Next Row Value

**URL:** <https://forum.knime.com/t/java-snippet-next-row-value/15517>\
**Category:** KNIME Analytics Platform\
**Tags:** java\
**Created:** [June 17, 2019, 11:13pm UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517 "2019-06-17T23:13:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![John-k](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@John-k](https://forum.knime.com/u/John-k)\
**Post date:** [June 17, 2019, 11:13pm UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/1 "2019-06-17T23:13:31Z")

</div>

Hi,

I’m trying to figure out how to bring back the ‘Order\_id’ value of the next row into the current row. I found a workflow example here that shows how to bring back the ‘Order\_id’ value of the previous row into the current row (as seen in the below picture), though I’m unable to figure out how to modify this to bring back the ‘next’ row. Any thoughts on how to do this in a Java Snippet Node?

Below is what i have so far. The first two columns are the data, and the last column is from the Java snippet  
 ![A](https://forum-cdn.knime.com/uploads/default/original/2X/0/0c51a560da9d0acbc4f6b8fa8e28153689e7699c.jpeg)

 ![B](https://forum-cdn.knime.com/uploads/default/original/2X/9/90888d41f6fc1460aa9354f687b7bd7f49c656bb.jpeg)

The below highlighted column (filled this in manually for example) are the values i’m looking to bring back via java.  
 ![C](https://forum-cdn.knime.com/uploads/default/original/2X/0/0fc31bb428da2a454a57d223453cd973872c351c.jpeg)

---

<div class="post-metadata">

**Author:** ![HansS](https://forum-cdn.knime.com/user_avatar/forum.knime.com/hanss/32/6979_2.png) [@HansS](https://forum.knime.com/u/HansS)\
**Post date:** [June 18, 2019, 6:09am UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/2 "2019-06-18T06:09:48Z")

</div>

Hi @John-k

My solution would be to sort the table descending then execute the lag column and finally sort your table ascending again.  
 ![java_snippet_next_row_valuel](https://forum-cdn.knime.com/uploads/default/original/2X/a/a1937e284e8dbbe0847e57c66cfaba81080e8c41.jpeg)  
gr. Hans

---

<div class="post-metadata">

**Author:** ![ipazin](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ipazin/32/806_2.png) [@ipazin](https://forum.knime.com/u/ipazin)\
**Post date:** [June 18, 2019, 8:30am UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/3 "2019-06-18T08:30:12Z")

</div>

Hi there,

additionally to simulate Lag Column node with negative value one can use Moving Aggregation node. Here is example:

> [@Lag Column with Negative value?](https://forum.knime.com/t/lag-column-with-negative-value/7622/6):
>
> Hi Iris, after 4 years, do you have implemented this features? It would be very useful for me… I cannot sort desc-lag-sort asc a table of 500k elements every single time the workflow is executed: it is so sloooooooow. The Gabors solution is not useful in my case. Using the Moving Aggregation can I lag a column in the opposite way? How? Andrea

Br,  
Ivan

---

<div class="post-metadata">

**Author:** ![John-k](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@John-k](https://forum.knime.com/u/John-k)\
**Post date:** [June 18, 2019, 10:51pm UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/4 "2019-06-18T22:51:50Z")

</div>

Thank You @HansS and @ipazin, both your solutions work. Would a solution be possible in the Java snippet, or would the Lag and Moving Aggregation nodes be the only way to accomplish this?

---

<div class="post-metadata">

**Author:** ![ipazin](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ipazin/32/806_2.png) [@ipazin](https://forum.knime.com/u/ipazin)\
**Post date:** [June 19, 2019, 8:04am UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/5 "2019-06-19T08:04:20Z")

</div>

Hi there,

I’m sure it can be done in Java Snippet node but not my field so can’t help you there. Maybe @quaeler can help you with it.

Br,  
Ivan

---

<div class="post-metadata">

**Author:** ![quaeler](https://forum-cdn.knime.com/user_avatar/forum.knime.com/quaeler/32/3944_2.png) [@quaeler](https://forum.knime.com/u/quaeler)\
**Post date:** [June 19, 2019, 6:30pm UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/6 "2019-06-19T18:30:49Z")

</div>

[snippet previous row.knwf](https://forum-cdn.knime.com/uploads/default/original/2X/9/9f4603eac2de85065a53a10b20a9f0623e0605d7.knwf) (6.3 KB)

---

<div class="post-metadata">

**Author:** ![John-k](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@John-k](https://forum.knime.com/u/John-k)\
**Post date:** [June 19, 2019, 10:07pm UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/7 "2019-06-19T22:07:25Z")

</div>

Thanks @quaeler - this works good. Is there a way to reverse the logic so it brings back the next row instead of the previous row?

---

<div class="post-metadata">

**Author:** ![quaeler](https://forum-cdn.knime.com/user_avatar/forum.knime.com/quaeler/32/3944_2.png) [@quaeler](https://forum.knime.com/u/quaeler)\
**Post date:** [June 19, 2019, 10:10pm UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/8 "2019-06-19T22:10:36Z")

</div>

No there isn’t. (Rows are consumed in a specific order; the reason we can reference the last row is because we’ve already had it given to the node (and so the reason we cannot reference the next row is because we have not been given it yet.))

---

<div class="post-metadata">

**Author:** ![John-k](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@John-k](https://forum.knime.com/u/John-k)\
**Post date:** [June 19, 2019, 10:25pm UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/9 "2019-06-19T22:25:31Z")

</div>

Thank You for explaining this @quaeler - that makes sense. Much appreciated.

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [June 26, 2019, 10:25pm UTC](https://forum.knime.com/t/java-snippet-next-row-value/15517/10 "2019-06-26T22:25:33Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
