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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

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 I
Helper I

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
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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