"Carry value forward until reset" logics with Knime

Hello Knime experts!
I need an elegant way on how to carry the value in the column forward (down the table) until it meets a reset condition.
Here is a simple example:
column1 has flag values (1, 0, or -1).
column2 has values for the rows where column 1 is equal to 1, otherwise has zero
I need to calculate column3 with following logics:
if (column1 = 1 then column2 same row value,
else if column1 = 0 then column3 previous row value (above value),
else 0)

An exact example, to make it 100% clear:
column1: 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 1, 0
column2: 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 13, 0
column3: 0, 0, 0, 0, 0, 0, 10, 10, 10, 0, 0, 13, 13

All ideas are welcomed!

Update: it seems I have found a woraround, but this solution requires the usage of a legacy node.

I could not find a way how to replace zero values with null other than using a legacy Column Expressions node. If someone has an alternative solution how to do this, please, share!

Sharing it below, but new ideas are very welcome!
step1: Column Expressions (legacy) node, creating column3 and replacing zeroes with null values:
if (column(“column2”) == 0) {
null
} else {
column(“column2”)
}
step2: Missing Value node, replacing missing values with previous value in column3
step3: calculating column4. I use Moving Aggregator node to calculate cumulative, rolling sum of column1 since the start of the table (in my case it works well as first observation with column1 can be always a 1 and never a -1, ensuring a positive 1 or a 0 as a rolling sum ).
step4: Rule Engine node, bringing column3 values to zero when column4 is at 0.Here I achieve the necessary result described in the post.

Hello @Nad_Ves ,

Thank you for the interesting topic. I’ll share my solution, but it is redundant. I think yours is the best one. I used Recursive loop and Lag column.


https://hub.knime.com/s/s6VvOrv8x-1jLjmS

Recursive loop carries over the table from the end of the loop to the start, so you can use the previous table values in the current step. Lag column allows you to bring the previous row values into the current row.

As for replacing zero with null, Rule engine might be another option, but it’s a bit of an indirect way.
The following expression will return null if the rows in column2 contain zero.

image

Thanks.

1 Like