Applying an if-else condition in Knime

Hi all,
I need to execute an If-else condition as follows -
if(valueofcell==“Fault”)
{if(valueofcell2 ==“Reason”)
{
display valueofcell2;
}
elseif(valueofcell3==“Reason”)
display valueof cell3;
… and so forth}

Is there any way to do so???
THanks in advance.

Hey I think you are looking for the rule engine node, there you can specify exactly that, just in an easier Syntax. Otherwise you could always use a Python/Java/R Snippet Node.

2 Likes

Hi Jennifer,
Thanks for the reply.I tried using the Java snippet node but there is no way to access multiple cells in a single code. If i am accessing valueofcell,i cant access valueofcell2 and other cells…

Hi @ajey -

I believe you can do this in a Java snippet. I’ve attached a workflow with some generated data and dummy logic. Also, here’s a screenshot of the dummy logic:

Is this getting closer to what you need?

IfElseJavaExample.knwf (6.4 KB)

4 Likes

I think you have to clarify what you mean by single cells. Are all cells in a column, or row or just somewhere in your table?
Just like Scott showed you access a column by its name in a snippet. If you need a specific value you can just use indexing of these columns.

Hi Jennifer,
By single cells I meant the next row in the same column.

Hi Scott,
Thanks for the reply… i was looking to access the next row value in the same column and other row values in different columns.

Hi if you tell KNIME a column name it will automatically iterate through the whole column and use every single value, at least thats what the rule engine node does. For R and Python the column is just treated as an iterable list, so you can just loop over the values.
In Scotts snippet it looks like the Java is doing that on its own, or he just has one row of values.

Does that clarify your issue a bit?

If I’m understanding you correctly, this is a much trickier problem. You might benefit from a transformation of your data before processing.

If you like, post an example of your data along with what you’re trying to do, so we can understand a little bit better.

Hi Ajey,

See attached workflow, it might help you. The point of this solution is collecting values
from multiple rows into a list, then splitting the list into multiple columns and performing rules
on them.

Martin K.

if_else_wf.zip (32.4 KB)

3 Likes

Yeah I considered transposing the Table and then applying the Java Snippet but then i lose access to different columns.

Hi Martin,
Thanks for the reply.This should work…thanks

Hi Ajey

If I may, I would suggest to use a Lag Column node to implement your solution. Whenever it is possible, I try to minimize the use of loops in workflows because they slow down the speed of workflow execution. Please find attached below the Lag Column Node version. Hope this is of help.

Best

Christophe

if_else_wf_with_lag_column.knwf (49.2 KB)

For example, in Python, you can shift data in a column in both directions.

— Robert

1 Like

Hi All, please may you help me!!

i have this IF statment on math formula:

if ($LAST YEAR end of month$ = 0) {
// $LAST YEAR end of month$
} else {
// (($ESTIMATION end of month$/$LAST YEAR end of month$)-1)*100
}

where in the true condition the result that i’d like to have is the column itselfm, but the formula is not correct.

how Can i do the same formula, even with others nodes?

thank you in advance :slight_smile:

Hi there @SimoneDePaoli,

welcome to KNIME Community!

I would suggest to use Column Expressions node.

Example workflow here:

Br,
Ivan

A post was split to a new topic: Condtionally Replacing Values in a Column

Hi everyone, I sove this issue with you help. Thank you a lot. Sorry for the delay.

1 Like