Help Diagnosing a Nested IF() Formula

Answered

Comments

6 comments

  • Konsta Danyliuk

    Hello Mario.
    If I properly understand the requirements, you have the following initial dataset with column types INT, INT, INT, DATE, STRING, and STRING respectively.

    I believe the following formula returns the desired result. 

    IF(#TEST==-62135575764000 && #SHIPPER==9670 && #DEST_FAC_NBR==9598;#CREATE_DATE+6h;IF(#TEST==-62135575764000 && #SHIPPER!=9670 && #DEST_FAC_NBR!=9598;#CREATE_DATE+24h;ASDATE(CONCAT(#LATEST_PICKUP_DATE;" ";#LATEST_PICKUP_TIME);"yyyy-MM-dd HH:mm:ss")))

    To break it down:

    • If the condiction #TEST==-62135575764000 && #SHIPPER==9670 && #DEST_FAC_NBR==9598 met - add 6h to #CREATE_DATE
    • If the condiction #TEST==-62135575764000 && #SHIPPER!=9670 && #DEST_FAC_NBR!=9598 met - add 24h to #CREATE_DATE
    • In all other cases concat LATEST_PICKUP_DATE and LATEST_PICKUP_TIME and convert the resulting value into a date.

    Please let me know if this helps.

    2
    Comment actions Permalink
  • Mario Valdes II

    Thanks for the response, Konsta!

    Yes, you've got the right idea, but I got my original formula to work by simply dropping the "does not equal" logic out altogether (in bold below.) My guess (and if someone could confirm I'm not incorrect, that'd be great,) is that once the specific 9670/9598 records pass the first logic of the first IF() adding 6h to their CREATE_DATE, any other records with -62135575764000 in the TEST field are taken care of by the second IF() and have 24h added to the CREATE_DATE.

    IF(AND(#Sheet2!TEST==-62135575764000;#Sheet2!SHIPPER==9670;#Sheet2!DEST_FAC_NBR==9598);#Sheet2!CREATE_DATE_Mstr_LOCSSHIPMENT+6h;IF(AND(#Sheet2!TEST==-62135575764000;#Sheet2!SHIPPER!=9670;#Sheet2!DEST_FAC_NBR!=9598);#Sheet2!CREATE_DATE_Mstr_LOCSSHIPMENT+24h;ASDATE(CONCAT(FORMATDATE(#Sheet2!LATEST_PICKUP_DATE;"MMM dd, Y");" ";#Sheet2!LATEST_PICKUP_TIME);"MMM dd, Y HH:mm:ss")))

    1
    Comment actions Permalink
  • Konsta Danyliuk

    Hello Mario.
    What was the initial problem with this formula, did it return an error or an incorrect result? If there was an error, would you be able to provide the screenshot, please?

    Your formula works fine for me even with "does not equal" operations. I simply replaced the concatenation part with null to simplify the test.

    IF(AND(#TEST==-62135575764000;#SHIPPER==9670;#DEST_FAC_NBR==9598);#CREATE_DATE+6h;IF(AND(#TEST==-62135575764000;#SHIPPER!=9670;#DEST_FAC_NBR!=9598);#CREATE_DATE+24h;null))

    As well as it is.

    IF(AND(#TEST==-62135575764000;#SHIPPER==9670;#DEST_FAC_NBR==9598);#CREATE_DATE+6h;IF(AND(#TEST==-62135575764000;#SHIPPER!=9670;#DEST_FAC_NBR!=9598);#CREATE_DATE+24h;ASDATE(CONCAT(FORMATDATE(#LATEST_PICKUP_DATE;"yyyy-MM-dd");" ";#LATEST_PICKUP_TIME);"yyyy-MM-dd HH:mm:ss")))

    The logic of nested IF functions, in this case, is the following:

    1. Check the first condition AND(#TEST==-62135575764000;#SHIPPER==9670;#DEST_FAC_NBR==9598), if it is met add 6h to CREATE_DATE value.
    2. For all records that do not satisfy the first condition (ELSE part), check the second condition AND(#TEST==-62135575764000;#SHIPPER!=9670;#DEST_FAC_NBR!=9598).
    3. For all those records that match the second condition add 24h to CREATE_DATE value.
    4. In case a record doesn't match any of the mentioned conditions - build a data value from LATEST_PICKUP_DATE and LATEST_PICKUP_TIME values.

    Datameer considers a certain IF condition as a complete expression. You could test every condition as a separate formula to confirm that it returns the expected result. 

    • AND(#TEST==-62135575764000;#SHIPPER==9670;#DEST_FAC_NBR==9598)
    • AND(#TEST==-62135575764000;#SHIPPER!=9670;#DEST_FAC_NBR!=9598)

     

    2
    Comment actions Permalink
  • Mario Valdes II

    In my data set, I’ve got the following fields:

    • SHIPPER (Integer)
    • DEST_FAC_NBR (Integer)
    • CREATE_DATE (Date)
    • LATEST_PICKUP_DATE (Date)
    • LATEST_PICKUP_TIME (String)
    • TEST (Integer, calculated field)
    • LATEST_PICKUP (Date, this is where my formula is)

    When there's not a valid LATEST_PICKUP_DATE in the data, a fake value of "1/1/0001 12:00:00" acts as a placeholder. In the TEST field, I use the INT() function to convert the LATEST_PICKUP_DATE to an integer, and the result for each fake value is "-62135575764000."

    My goal with my formula was to, in the field LATEST_PICKUP, concatenate the LATEST_PICKUP_DATE/TIME fields for the valid values, but where TEST=="-62135575764000", add 24 hours to the CREATE_DATE, except when SHIPPER=="9670" and DEST_FAC_NBR=="9598", then add only 6 hours.

    With my original formula (the one in my first post,) the goal was to end up with a LATEST_PICKUP field with nothing but valid dates, but I was seeing some invalid values of "1/1/0001 12:00:00." My hunch was the "AND/!=" logic was allowing certain records to slip through because things like misuse of "AND" (versus  OR) and using "!=" with multiple criteria has bitten me in the butt before. I removed that piece of it, and the results cleared up.

    1
    Comment actions Permalink
  • Konsta Danyliuk

    Hello Mario.
    Thank you for additional clarification on the use-case. I was not able to reproduce any unexpected behavior playing with these formulas in my lab.

    Were you able to completely resolve the initial concern or there are still outstanding questions I could help to address? 

    2
    Comment actions Permalink
  • Mario Valdes II

    Yes, I think my issue is resolved, but thank you very much for your help and attention, Konsta!

    1
    Comment actions Permalink

Please sign in to leave a comment.