Column expression if statement

Good Day Knimers :green_heart:

I am trying to use if statement in Column Expression Node and now I have faced some problem with this statement.
When I am comparing two data, it is not giving a right answer. Maybe the way how I am writing an expression is not right enough :thinking:
Can you look at this, pleaseā€¦ :relaxed:

There are my screenshots:
I am comparing two data, if they are equal it should set ā€œCurrentā€
image

The output:
image

When I tried to use with Rule Engine, it worked finely. But rule engine does not support some math operations as I know. Taking difference from some data
image

Many Thanks,
Karlygash

Hi @Karlygash , I think this has to do with the fact that the columns are of date format.

You can convert them to string during comparison like this:

if(string(column("compare_red_year_month")) == string(column("compare_filedate"))) {
    "Current"
}

Results:
image

EDIT: Additional info, as you know, the Column Expressions is based on Javascript. In js, you can compare if date1 > date2 or if date1 < date2, but not if date1 is equal to date2, at least not directly as you found out with your issue.

Hereā€™s a quick test. I added this expression as a new column:

var date1 = new Date(column("compare_filedate"));
var date2 = new Date(column("compare_red_year_month"));
if(date1 > date2) {
    "Greater"
} else if (date2 > date1) {
    "Less"
} else {
    "Same"
}

Results:
image

As you can see, it has no problem detecting that date1 > date2 or date1 < date2. The only way I could get the results where the 2 dates are the same is basically the default case if none of the 2 conditions are true (date1 > date2 or date1 < date2), which would by default means they are equal.

So, another way to do this without converting to string would be:

var date1 = new Date(column("compare_filedate"));
var date2 = new Date(column("compare_red_year_month"));
if(!(date1 > date2 || date2 > date1)) {
    "Current"
}

Results:
image

7 Likes

Thanks a lot!
Updated answer was very helpful :innocent:

No problem @Karlygash

1 Like

Sorry, one more question
Column expression it is from Knime Lab, is it ok if I use all the time? I know that nodes which are in Labs, havent deployed it. I mean wont be there are some bugs?
Personally, I really like this nodeā€¦ All operations in one node

Yes, itā€™s ok to use.

2 Likes

Sorry, one more question :face_with_hand_over_mouth:

This expression is not accepted, right?

var date1 = new Date(column(ā€œcompare_filedateā€));
var date2 = new Date(column(ā€œcompare_req_year_monthā€));
if(date1 == date2) {
ā€œCurrentā€
}

Thatā€™s correct @Karlygash , this will not work. You can try it for yourself as a separate expression to see.

1 Like

ok, thanks a lot :blush:
I will try it out

Hello there!

Date comparison in Column Expressions node was discussed in this topic:

and regarding nodes from Labs see bottom two topics:

Br,
Ivan

3 Likes

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