Forcing loop iterations to act over the same rows

I’m using “Counting Loop Start” to allocate resources (number of itens) to stores.
In the example, I have 29 remaining items to allocate in 20 stores.
To simplifly it*, I want to allocate equally, 1 item to 11 stores and 2 items to 9 stores.

*In the original method it’s done accordingly to a score to each store.

I’m using the counting loop to perform this alocation. I’m running the loop twice with column expressions inside to allocate it with the expected result.

However, in the second iteration it duplicates the rows instead of using the same rows again. Anyone can help me with it?

This is the sample project:
KNIME_project_sample.knwf (18.4 KB)

And this is the result I expected:
image

I solved it using Recursive Loop instead of Counting loop

2 Likes

Hello @renatomendonca,

you don’t need a loop for this. You can do it with Column Expressions and following logic:

minAllo = floor(29 / 20) // Will give you 1

if(rowIndex() <=  mod(29,20)){
    minAllo + 1 // will give you 2 for first 9 rows
    }
else 
    {minAllo} // will give you 1 for next 11 rows

In code simply replace 29 and 20 with respective flow variables.

Br,
Ivan

4 Likes

Hi @ipazin ,

I agree with you, it’s a very clever solution. I"ll replace it with some adjustments because sometimes the remanining part will demand something greater than 1 as minimum to some, or also equal to zero.

Thank you!

Renato

1 Like