# if with multi condition

hi every one .
i have a table with 5 column and 100row .
i wanna set a condition for each cell that check this condition =>
if length of that cell = 1 then replace something with something else .
how i can do this .?

Hello @alrz,

you can use Unpivoting to get everything in one column, apply your logic and follow it with Pivoting node. See here how to do it:

For if/else logic in Column Expressions node check here:

Br,
Ivan

4 Likes

hi @alrz , I’m assuming these are string columns. You may also be able to use String Manipulation (Multi Column) :

select only the columns that you want to act on, and use the following expression to replace “x” with “y”:

``````string(
1 == length(\$\$CURRENTCOLUMN\$\$)
?replace(\$\$CURRENTCOLUMN\$\$,"x","y")
:\$\$CURRENTCOLUMN\$\$)
``````

see the “conditional expressions” paragraph here for further info:

Mind you, given the cell length to check for is 1, I think that replacing “x” with “y” could be done as a simple literal replacement, using regexReplace:

``````regexReplace(\$\$CURRENTCOLUMN\$\$,"^x\$","y")
``````
3 Likes

hi @ipazin
thanks for your help bro . its work .

but please look at my pic

i write this and when run node the result be this

can you tell me where is the problem?

1 Like

hi @takbb
thanks bro . it was really my answer

do you found where is my mistake?

Hi @alrz, to match the above logic, using String Manipulation (Multi column) would be:

``````1== length(\$\$CURRENTCOLUMN\$\$)
?join("0",\$\$CURRENTCOLUMN\$\$,":00")
:2== length(\$\$CURRENTCOLUMN\$\$)
?join(\$\$CURRENTCOLUMN\$\$,":00")
:\$\$CURRENTCOLUMN\$\$
``````

re the use of Column Expressions question to @ipazin, I think if you could supply the sample of data and the column expression that you have in your picture, it would be easier to assist.

The Column Expression you have shown doesn’t currently appear to match the result you have shown (e.g. in the output, the column is called “result”, and in the CE screenshot it is called “new”), and I don’t see how that Column Expression would have derived what you have in “result” from the displayed input data. Can you double-check this is actually the output produced.

yes i rename new to result to after send pic . otherwise new is same result .
yes your code is true and work . but i wanna found why that condition i write not true .
in below send true pic

main column

code that i write in column expreession

lQQU6X.png)

result

Hi @alrz, Can you actually upload a small sample workflow containing that data and the column expression.

Unfortunately a screenshot is no good, since from the screenshot I can see no way that that column expression could append “00” onto the front of any data item, so if it is really doing that then there is something very subtle that I cannot see from looking at that picture, or else there is something weird going on.

yes bro . the sample data =>
“09:47
09:31
09
08:27
09:33
08:27”
and the column expression code = > “if (length(column(“ورود”) == 1)) {
join(“0”, column(“ورود”), “:00”)
} else if (length(column(“ورود”)) == 2) {
join(column(“ورود”), “:00”)
} else {
column(“ورود”)
}”
and then result => “009:47:00
009:31:00
009:00
008:27:00
009:33:00
008:27:00”

the type of each column is string

It doesn’t though… that’s why I asked if you could upload a workflow that demonstrates it.

That column expression produces this:

What version of KNIME are you using?

Oh hang on… there is something odd… I’ll be back!!

OK… I’m back…

There is a subtle mistake in your code.

My code:

``````if (length(column("ورود")) == 1) {
join("0", column("ورود"), ":00")
} else if (length(column("ورود")) == 2) {
join(column("ورود"), ":00")
} else {
column("ورود")
}
``````

``````if (length(column("ورود") == 1)) {
join("0", column("ورود"), ":00")
} else if (length(column("ورود")) == 2) {
join(column("ورود"), ":00")
} else {
column("ورود")
}
``````

Look at the placement of the closing brackets in the first line
So replace your code with my code, and it should work

1 Like

version 5.2.3
sorry i cant upload workflow .

Please see the edit at end of my above comment.

oh oh very bad mistake .
really thanks bro for your help .

2 Likes

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