Forum Discussion

JoachimSA's avatar
JoachimSA
Icon for Helper II rankHelper II
4 years ago
Solved

ERROR() triggered in SWITCH() Function for no reason

Hi everybody,

 

I am currently trying to add some meaningfull error messages to my code to help me figure out quickly what is wrong if i.e. new data "break" my logic.

For this I mostly use Switch:

 

 

Result = SWITCH(
   Condition 1, Result1,
   Condition 2, Result2,
   ERROR("usefull error message to let me know that none of my conditions are true)
)

 

Somehow, the error message always gets triggered even though it is in the "else" part. Did i get something wrong here ?

 

 

  • Hi JoachimSA 

    You can use if() function instead of switch().

    Because, for Switch() function, all result expressions and the else expression must be of the same data type.

    for IF() function, it can return a variant data type if value_if_true and value_if_false are of different data types, but the function attempts to return a single data type if both value_if_true and value_if_false are of numeric data types. In the latter case, the IF function will implicitly convert data types to accommodate both values.

     

    The error() function will give the specific error prompt and stop the calculation when the conditions are met. 

    The reason is the expression should not return processed values when encountering specific errors, these values are incorrect, it should generate invalid reports instead of unreliable numbers that may be considered correct. Also, that will lead to unnecessary resources waste.

     

     

    Best Regards,

    Community Support Team _Tang

    If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    HI JoachimSA ,

     

    The syntax should be

     

     

    Result = SWITCH(
    TRUE(),
       Condition 1, Result1,
       Condition 2, Result2,
       "Error"
    )

     

     

    https://dax.guide/switch/

     

    Regards,
    Harsh Nathani
    Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

  • Dear harshnathani,

     

    thanks for the answer! 
    This is a good solution when you use it in a measure, where you can actually see the "error" in a visualization.
    I am using this primarily in calculated columns. That "error" would get lost somewhere in thousands of rows without me noticing. Also, it is not possible if Result1/2 are in the number format because the switch statement will produce inconsistent data types.
    Why can I not use the error() function there ? 

  • v-xiaotang's avatar
    v-xiaotang
    Icon for Community Support rankCommunity Support

    Hi JoachimSA 

    You can use if() function instead of switch().

    Because, for Switch() function, all result expressions and the else expression must be of the same data type.

    for IF() function, it can return a variant data type if value_if_true and value_if_false are of different data types, but the function attempts to return a single data type if both value_if_true and value_if_false are of numeric data types. In the latter case, the IF function will implicitly convert data types to accommodate both values.

     

    The error() function will give the specific error prompt and stop the calculation when the conditions are met. 

    The reason is the expression should not return processed values when encountering specific errors, these values are incorrect, it should generate invalid reports instead of unreliable numbers that may be considered correct. Also, that will lead to unnecessary resources waste.

     

     

    Best Regards,

    Community Support Team _Tang

    If this post helps, please consider Accept it as the solution to help the other members find it more quickly.