Forum Discussion

Txtcher's avatar
Txtcher
Helper V
1 year ago
Solved

Help With SWITCH - d/n support comparing values...

I was using nested IFs for a calculated column creation. It was not working correctly. After doing a bit of research, I came across the SWITCH function, but I get an error stating SWITCH does not sup...
  • speedramps's avatar
    1 year ago

    Here are valid examples of SWITCH syntax

    test1 = 
    var myvalue = 6
    RETURN
    SWITCH(TRUE,
    myvalue = 5, "Five",
    myvalue = 6, "Six",
    myvalue = 8, "Eight",
    "other")

     

    test2 = 
    var myvalue = "S"
    RETURN
    SWITCH(TRUE,
    myvalue = "B", "Brazil",
    myvalue = "S", "Spain",
    myvalue = "I", "India",
    "other")

     

    This type of sysntax is invalid SWITCH need a scalar data type (a single value).
    The RS case [Status] is a table data type

    Test1 = 
    SWITCH(TRUE,
    'RS case'[Status]  = "OPEN", "Yes",
    "No"
    )

     

    You can use MIN or MAX  return a scalar value but take care with totals. See example

    Test2 = 
    SWITCH(TRUE,
    MAX('RS case'[Status])  = "OPEN", "Yes",
    "No"
    )

     

    Consider using VAR variables to make your syntax easier to read and test 

    For example

    var mylookup = LOOKUPVALUE('RS Events'[Survey_Date__c],'RS Events'[Id], 'RS Cases'[RS Event ID])
    var mycase = MIN('RS Cases'[Status])
    var myduedate = MIN(RS Cases'[Due By])
    
    SWITCH( TRUE,
        // if survey date null & status = Open and Due By is less than today's date, then date dif due by and today's date
        ISBLANK(mylookup)
    && myduedate="OPEN" 
    && mycase<TODAY(),
        DATEDIFF('RS Cases'[Due By], TODAY(),DAY),


    This code with perform faster because it only does the lookup once and then holds the anwer as a variable.

    Rather than your code which does the same lookup for each condition.
    You can also view and check each variable to debug and fix the problem.

     Please click thumbs up and accept solution