# How do I loop through same ID in many rows?

**URL:** <https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316>\
**Category:** KNIME Analytics Platform\
**Created:** [September 19, 2022, 11:55am UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316 "2022-09-19T11:55:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonatasflausino](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jonatasflausino](https://forum.knime.com/u/jonatasflausino)\
**Post date:** [September 19, 2022, 11:55am UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316/1 "2022-09-19T11:55:00Z")

</div>

Hello,

I have one simple database where the same number appears in two columns and I would like to get the full chain.

Database example (also .xlsx attached):

**INPUT**

| Cod1 | Cod2 |
| --- | --- |
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 5 | 6 |
| 6 | 7 |
| 8 | 9 |
| 10 | 11 |
| 11 | 12 |

**RESULT THAT I LOOK FOR**

| Col1 | Col2 | Col3 | Col4 |
| --- | --- | --- | --- |
| 1 | 2 | 3 | 4 |
| 2 | 3 | 4 | |
| 3 | 4 | | |
| 5 | 6 | 7 | |
| 6 | 7 | | |
| 8 | 9 | | |
| 10 | 11 | 12 | |
| 11 | 12 | | |

I appreciate the assistance.  
[Example.xlsx](https://forum.knime.com/uploads/short-url/sI0Izcok2UmosRdP2xDFcKT04Hr.xlsx) (9.9 KB)

---

<div class="post-metadata">

**Author:** ![Daniel\_Weikert](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@Daniel\_Weikert](https://forum.knime.com/u/Daniel_Weikert)\
**Post date:** [September 19, 2022, 4:38pm UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316/2 "2022-09-19T16:38:43Z")

</div>

I could think of using a lag node for column 1 and check if col2 = lag col 1.  
Maybe someone comes up with a better idea  
br

---

<div class="post-metadata">

**Author:** ![iCFO](https://forum-cdn.knime.com/user_avatar/forum.knime.com/icfo/32/36036_2.png) [@iCFO](https://forum.knime.com/u/iCFO)\
**Post date:** [September 19, 2022, 5:21pm UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316/3 "2022-09-19T17:21:15Z")

</div>

Or a “negative lag” to look down Cod2 in order to test if it falls sequentially so that it can be moved to a new column.

> [@Lag Column with Negative value?](https://forum.knime.com/t/lag-column-with-negative-value/7622/7):
>
> Hi there! You are right! Using Moving Aggregation you can simulate negative lag interval in Lag Column node. Window type is Forward, window length-1 determines the lag interval and aggregation is Last on lagging column. Additionally there is another way using just a bit of filtering where number of excluded rows determines the lag interval wink Workflow is attached so check it out and if any questions feel free to ask. [2019\_04\_03\_ReverseLagColumnOption.knwf](https://forum-cdn.knime.com/uploads/default/original/2X/3/3560e52a862564fef08188c4b4e193b3a6e5c4b1.knwf) (16.2 KB) Rega…

This negative lag style approach may also now be possible just using the Column Expression node with the recent addition of multi-row support, but I haven’t had time to delve into it yet…

---

<div class="post-metadata">

**Author:** ![iCFO](https://forum-cdn.knime.com/user_avatar/forum.knime.com/icfo/32/36036_2.png) [@iCFO](https://forum.knime.com/u/iCFO)\
**Post date:** [September 19, 2022, 5:27pm UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316/4 "2022-09-19T17:27:11Z")

</div>

I guess you could also loop through rows in one Cod1, append it to a “transformed” Cod2, test for when the columns fail to fall in sequential order, then delete the remaining columns, then concatenate your row results into a table at loop end. Slower processing, but it should work as an alternate option.

---

<div class="post-metadata">

**Author:** ![bruno29a](https://forum-cdn.knime.com/user_avatar/forum.knime.com/bruno29a/32/28802_2.png) [@bruno29a](https://forum.knime.com/u/bruno29a)\
**Post date:** [September 20, 2022, 2:40am UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316/5 "2022-09-20T02:40:25Z")

</div>

Hi @jonatasflausino and welcome to the Knime Community.

Is it similar to this?

> [@Parent-Child Hierarchy](https://forum.knime.com/t/parent-child-hierarchy/37698):
>
> Dear Knime community! I would like to know whether a node exist that can create the column “path” with the columns “Child ID” and “Parent ID” as inputs: [image] This is a standard function in DAX (PowerBI /SAAS language), which is very useful for Business Intelligence applications. Thank you very much for any tips! Dimitri

---

<div class="post-metadata">

**Author:** ![jonatasflausino](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/858c86/32.png) [@jonatasflausino](https://forum.knime.com/u/jonatasflausino)\
**Post date:** [September 20, 2022, 1:35pm UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316/6 "2022-09-20T13:35:16Z")

</div>

Hello, @bruno29a.

This worked like a charm, thanks a lot.  
I just needed to split later the cell into columns.

Many thanks for your support!

---

<div class="post-metadata">

**Author:** ![iCFO](https://forum-cdn.knime.com/user_avatar/forum.knime.com/icfo/32/36036_2.png) [@iCFO](https://forum.knime.com/u/iCFO)\
**Post date:** [September 20, 2022, 1:49pm UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316/7 "2022-09-20T13:49:03Z")

</div>

I wasn’t thinking about it along the lines of Parent / Child relationship without the context in the sample data. Great approach @bruno29a! I am going to incorporate this into projects as well!

---

<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:** [September 27, 2022, 1:49pm UTC](https://forum.knime.com/t/how-do-i-loop-through-same-id-in-many-rows/47316/8 "2022-09-27T13:49:26Z")

</div>

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