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
dedelman_clng
Community Champion
Community Champion

SWITCH(TRUE() .. vs SWITCH(Colname ...)

If I have individual values that I am looking for in a SWITCH statement (for example Dept = 1, 2, 3 or 4; no complex or multiple conditions), is there any downside to using

 

SWITCH(Dept, 1, Val1, 2, Val2, 3, Val3, 4, Val4, 0)

 

vs

 

SWITCH( TRUE(), Dept = 1, Val1, Dept = 2, Val2, ...)

 

Thanks,

David

1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @dedelman_clng

For your scenario,since you only have individual values, it is better and easier to use the first case though it is no difference to the result with two ways.

 

SWITCH has two types of structure.

 

The first usage of the SWITCH function has the following structure:

SWITCH(expression, 
   value1, result1,
   value2, result2,
    :
    :
    else
   )

expression is any DAX expression that returns a single scalar value (number, string, or date), where the expression is to be evaluated multiple times (for each row/context).

value1 is a constant value to be matched with the evaluated result of expression.

result1 is any scalar expression (i.e. one that returns a scalar value) to be evaluated if the results of expression match the corresponding value1.

else is any scalar expression to be evaluated if the result of expression doesn’t match any of the value arguments

expression, value1, and result1 are the only mandatory parameters in SWITCH. If the formula contains multiple value/result pairs, then the data types of result1, result2…resultn must be the same. In addition, if you include an else expression, the expression must evaluate to the same data type as result.

 

The second usage of the SWITCH function has the following structure:

SWITCH(TRUE(), 
    booleanexpression1, result1,
    booleanexpression2, result2,
    :
    :
    else
   )

where TRUE() is a DAX function, and booleanexpression1,booleanexpression2,…are any valid Boolean expressions (i.e. returns True or False). The first booleanexpression that evaluates to True will return the corresponding result as the formula result.

 

Best Regards

Maggie

View solution in original post

2 REPLIES 2
giri41
Helper II
Helper II

hi

 

switch function -- i couldnt link the original table values, they dont appear.. please help..

switch(true(),selected value="abc",[ def]----> this def value i cannot get it.. please help..

 

 

thanks

Giri

v-juanli-msft
Community Support
Community Support

Hi @dedelman_clng

For your scenario,since you only have individual values, it is better and easier to use the first case though it is no difference to the result with two ways.

 

SWITCH has two types of structure.

 

The first usage of the SWITCH function has the following structure:

SWITCH(expression, 
   value1, result1,
   value2, result2,
    :
    :
    else
   )

expression is any DAX expression that returns a single scalar value (number, string, or date), where the expression is to be evaluated multiple times (for each row/context).

value1 is a constant value to be matched with the evaluated result of expression.

result1 is any scalar expression (i.e. one that returns a scalar value) to be evaluated if the results of expression match the corresponding value1.

else is any scalar expression to be evaluated if the result of expression doesn’t match any of the value arguments

expression, value1, and result1 are the only mandatory parameters in SWITCH. If the formula contains multiple value/result pairs, then the data types of result1, result2…resultn must be the same. In addition, if you include an else expression, the expression must evaluate to the same data type as result.

 

The second usage of the SWITCH function has the following structure:

SWITCH(TRUE(), 
    booleanexpression1, result1,
    booleanexpression2, result2,
    :
    :
    else
   )

where TRUE() is a DAX function, and booleanexpression1,booleanexpression2,…are any valid Boolean expressions (i.e. returns True or False). The first booleanexpression that evaluates to True will return the corresponding result as the formula result.

 

Best Regards

Maggie

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.