If Then Else Statement

Hi All, I need help in writing out a syntax for a IF THEN ELSE statement. I have below formula that is currently in a Alteryx work and wanted to know what would be equivalent in Knime.

Basically what the formula is doing is replacing a COST field depending on a specific billing type. So where ever it matches that specific billing it overwrites the COST field to 0. Thank you in advance.
IF [Billing Type] = ‘G2’ THEN 0
ELSEIF [Billing Type] = ‘ZG2’ THEN 0
ELSEIF [Billing Type] = ‘ZL2’ THEN 0
ELSEIF [Billing Type] = ‘IG’ THEN 0
Elseif [Billing Type] = ‘L2’ THEN 0
ELSE [Cost] ENDIF

@Asimkz you could use column expressions, rule engine or Java Snippet

1 Like

I tried the column expressions but I can’t figure out the correct syntax. This is what I have currently have which doesn’t work. Is there an ELSEIF function in Knime?
IF (column(“Billing Type”) = “G2”)
{ 0 }
Else
(column(“Billing Type”) = “L2”)
{ 0 }
Else
(column(“Cost”))

@Asimkz yes there is “else if”

1 Like

Mind that you need to use .equals("") for such string comparisons.

Meaning:

if (column("Billing Type").equals("G2")) {
    0
} else if (column("Billing Type").equals("L2")) {
    0
} else {
    column("Cost")
}

Since the output for both conditions is the same, you can even opt to embed the second one with an OR statement (||).

if (column("Billing Type").equals("G2") || column("Billing Type").equals("L2")) {
    0
} else {
    column("Cost")
}
2 Likes

Thank you everyone. The below formula worked. I need to remember that the function needs to be in lower case if vs If or IF
if (column(“billing_type”) == “G2”)
{ 0 }
else if (column(“billing_type”) == “ZG2”)
{ 0 }
else if (column(“billing_type”) == “ZL2”)
{ 0 }
else if (column(“billing_type”) == “IG”)
{ 0 }
else if (column(“billing_type”) == “L2”)
{ 0 }
else
{ column(“cost”) }

I WOULD use the rule engine node I.O the column expression.
its more intuitive to me .
Not sure why but the sementic the column expression node differs from the rest of the native nodes. which is quite confusing if you are giving the first steps.
Also, when you open the node Rule engine You will see a comment explaining the logic.

2 Likes

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