If Statements

David_Mellamphy
David_Mellamphy Registered Posts: 9

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.....

Tagged:

Answers

  • Miguel Angel
    Miguel Angel Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 118 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
    David_Mellamphy Registered Posts: 9

    Thanks very much

  • tgb417
    tgb417 Dataiku DSS Core Designer, Dataiku DSS & SQL, Dataiku DSS ML Practitioner, Dataiku DSS Core Concepts, Neuron 2020, Neuron, Registered, Dataiku Frontrunner Awards 2021 Finalist, Neuron 2021, Neuron 2022, Frontrunner 2022 Finalist, Frontrunner 2022 Winner, Dataiku Frontrunner Awards 2021 Participant, Frontrunner 2022 Participant, Neuron 2023 Posts: 1,598 Neuron

    @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.

Setup Info
    Tags
      Help me…