If Statements

David_Mellamphy
Level 2
If Statements

Hi, Im trying to write an if statement based on a number of variables. 

I have the following formula written

On my GPA_Tool column I only want to take the first 3 digits.

if(arrayContains(splitByLengths(GPA_Tool,3),"166"),"China","ok")

 

However I have other values I would like to include, more numbers other than the 166. for example if the if(arrayContains(splitByLengths(GPA_Tool,3),"166" or then if it is "183","187",189" etc.....

 

0 Kudos
3 Replies
MiguelangelC
Dataiker

Hi David,

When having more than 5 else ifs on a statement, using a switch case() statement instead becomes a better option due to speed, clarity, etc. (Note this assertion is not DSS specific, but generally coding wise).

As a result, an option is to create a python recipe where starting in Python 3.10 the 'case' keyword was added. If you do not have that Python version, another option is to use the 'Switch case' processor in a Prepare recipe.

For example:

switchcase.PNG

David_Mellamphy
Level 2
Author

Thanks very much

0 Kudos
tgb417

@David_Mellamphy ,

Another way you might lookup these values is by doing join to a detail table with the replacement values.  Create a short table with your numeric values in one column and your replacement/new values in the second column. Then join the first column of this new  dataset to the original value in the original tables.  This can be particularly performant if your data is stored in a SQL database. If the list of replacements is long it can also be somewhat easier to manage these replacements over time.

just a thought.  

--Tom