Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
JoachimSA
Helper II
Helper II

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 ?

 

 

1 ACCEPTED SOLUTION
v-xiaotang
Community Support
Community 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. 

vxiaotang_0-1640763258661.png

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.

View solution in original post

3 REPLIES 3
v-xiaotang
Community Support
Community 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. 

vxiaotang_0-1640763258661.png

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.

JoachimSA
Helper II
Helper II

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 ? 

harshnathani
Community Champion
Community Champion

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)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.