I need to duplicate data from the row above for a single column based on a condition

Hi,

I am pretty new to the KNIME Platform and was wordering if there is a way I can write a condition where based on the string input I am able to either select data from the column data from the row right above it or leave it as the same.

Using String Manipulation I was able to get this:
regexMatcher($Customer Name$, “XYZ”).equals(“True”)?string($$CURRENTCOLUMN$$($$ROWINDEX$$)):string($Customer Name$)

This only gives me the row index of the column above and not the data itself.
How I can I change this expression to give me data from Customer Name -1 row?

Apologies,
The code is as follows:

regexMatcher($Customer Name$, “XYZ”).equals(“True”)?string(($$ROWINDEX$$-1)):string($Customer Name$)

Hi @anush_mohammed,

welcome to the Knime Community. You can leverage the Column Expression node to access previous or next rows:

If you can provide some example data, I could help you crafting the corresponding expression in case you struggle with that.

Cheers
Mike

1 Like

Hi Mike,

Thanks for the quick reply!

Example:
RowID CustomerName
Row 1 ABC
Row 2 XYZ
Row 3 CDF
Row 4 CDF
Row 5 XYZ

The data does not follow any particular sort of pattern.
Whenever ‘XYZ’ is found column Customer Name, the supposed value of the row should actually be the value found the previous row of customer name.

In the above example, Row 2 and 5 has ‘XYZ’ and should be replaced with data from Row 1 (‘ABC’) and Row 4 (‘CDF’)


I tried using the following expression within the string manipulation node excluding everything but the Customer Name Column on top:
regexMatcher($Customer Name$, “XYZ”).equals(“True”)?string(($$ROWINDEX$$-1)):string($Customer Name$)

But whenever it identifies XYZ it just returns the Row ID of the row 2 above the one it finds it in.

Appreciate any help with this!

Thanks,
Anush

Try this:
Column Expressions Replace Row w Previous Row.knwf (80.0 KB)

Here’s a version which allows input of the name to be changed rather than having it hard coded in the Column Expressions node.
Column Expressions Replace Row w Input.knwf (95.7 KB)

4 Likes

Thanks @rfeigel, the first solution worked perfectly!

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