Ready for Dataiku 10? Try out the Crash Course on new features!

# Nested IF with OR inbetween

Solved! Level 2
###### Nested IF with OR inbetween

Hi guys, I'm struggling to write the following formula:

If the number in Historical FX is empty,

then check whether Contract Currency has value USD or CNY, if so, take the number from "Depreciation CC", else take "Depreciation CC" and divide it by "End Rate"

else, take "Depreciation CC" and Divide it by "Historical FX"

//So the middle paragraph is a nested if statement

My current code is:

if(numval("Historical FX") == "",

if(strval("Contract Currency") == "USD" || "CNY",

numval("Depreciation CC"), (numval("Depreciation CC") / numval("End Rate")))),

(numval("Depreciation CC") / numval("Historical FX"))

)

Any help would be greatly appreciated.

Thank you.

Joe

1 Solution  Dataiker

Hello Joe,

The logical OR operator ||  will work for two boolean statements but you are using one boolean and one string, therefore only the first condition is evaluated.

``if(strval("Contract Currency") == "USD" || "CNY",``

should be

``if((strval("Contract Currency") == "USD") || (strval("Contract Currency") == "CNY")),``

Best,
Arnaud

5 Replies  Dataiker

Hello Joe,

Your logic looks fine. However it looks like you have one parenthesis too much at line 3 at end of line:

``````numval("Depreciation CC"), (numval("Depreciation CC") / numval("End Rate")))),

``````

should probably be

``numval("Depreciation CC"), (numval("Depreciation CC") / numval("End Rate"))),``

Arnaud Level 2
Author

Hi Arnaud,

Unfortunately the nested if statement (the middle two lines) still aren't returning anything. I'm receiving error "ExpressionError: Cannot parse to number: null". The outer if statement appears to be working.

Thanks again,

Joe  Dataiker

Hello Joe,
This means you have nulls in one of your columns  "Depreciation CC", "End Rate", "Historical FX". You can add a previous step "Fill empty with..." according to what you want, to avoid that

Arnaud Level 2
Author

Hi Arnaud

Thank you for your help so far!

Joseph  Dataiker

Hello Joe,

The logical OR operator ||  will work for two boolean statements but you are using one boolean and one string, therefore only the first condition is evaluated.

``if(strval("Contract Currency") == "USD" || "CNY",``

should be

``if((strval("Contract Currency") == "USD") || (strval("Contract Currency") == "CNY")),``

Best,
Arnaud 