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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
RodrigoMachado
Frequent Visitor

SWITCH funcion not returning all conditional statements

Hello community,

 

I've found some similar issues searching the forum, but nothing quite like this situation.

 

Context: I got a table with data about vaccines per State, budgets, etc.

 

I want to show a card with a dynamic message to the user, depending on how many options (States) he selects, like this: "Hi! You are currently viewing the state X", or "Hi! You are currently viewing the states X and Y", or "Hi! You are currenty viewing multiple states"

 

So I got something like this:

 

VAR number_of_selections = COUNTROWS(VALUES('Table'[States]))
VAR none_selected = "Hi! You are viewing " & FIRSTNOBLANK('Table'[States], TRUE()) //this one shows the first State in case none is selected
VAR one_selected = "Hi! You are viewing " & SELECTEDVALUE('Table'[States])
VAR two_selected = "Hi! You are viewing " & CONCATENATEX(VALUES('Table'[States]), [States], ",")
VAR multiple_selected = "Hi! You are viewing multiple States"

RETURN
SWITCH(
TRUE(),
number_of_selections = 2, two_selected,
ISBLANK(SELECTEDVALUE('Table'[States])), none_selected,
number_of_selections = 1, one_selected,
number_of_selections >2, multiple_selected
)

 

The problem is the function won't return all the conditions, the one I marked in blue does not return at all. Whenever I select >2 values, 3 for example, the message shows only the first value selected. Also I had to bring the statement in green to the top, otherwise it wouldn't show up.

 

Any advice? Thanks in advance!

 

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

Hi  @RodrigoMachado ,

This problem occurs because your later judgment has been overwritten by the previous one, so no data will appear. You can change dax to try as follows:

VAR number_of_selections = COUNTROWS(VALUES('Table'[States]))
VAR none_selected = "Hi! You are viewing " & FIRSTNOBLANK('Table'[States], TRUE()) //this one shows the first State in case none is selected
VAR one_selected = "Hi! You are viewing " & SELECTEDVALUE('Table'[States])
VAR two_selected = "Hi! You are viewing " & CONCATENATEX(VALUES('Table'[States]), [States], ",")
VAR multiple_selected = "Hi! You are viewing multiple States"

RETURN
 SWITCH(
    TRUE(),
ISBLANK(SELECTEDVALUE('Table'[States])), none_selected,
number_of_selections >2, multiple_selected,
number_of_selections = 2, two_selected,
number_of_selections = 1, one_selected,   
)

 

 

Best Regards,

Liu Yang

If this post helps, then 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-yangliu-msft
Community Support
Community Support

Hi  @RodrigoMachado ,

This problem occurs because your later judgment has been overwritten by the previous one, so no data will appear. You can change dax to try as follows:

VAR number_of_selections = COUNTROWS(VALUES('Table'[States]))
VAR none_selected = "Hi! You are viewing " & FIRSTNOBLANK('Table'[States], TRUE()) //this one shows the first State in case none is selected
VAR one_selected = "Hi! You are viewing " & SELECTEDVALUE('Table'[States])
VAR two_selected = "Hi! You are viewing " & CONCATENATEX(VALUES('Table'[States]), [States], ",")
VAR multiple_selected = "Hi! You are viewing multiple States"

RETURN
 SWITCH(
    TRUE(),
ISBLANK(SELECTEDVALUE('Table'[States])), none_selected,
number_of_selections >2, multiple_selected,
number_of_selections = 2, two_selected,
number_of_selections = 1, one_selected,   
)

 

 

Best Regards,

Liu Yang

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

Angith_Nair
Helper V
Helper V

Hi @RodrigoMachado ,

Try with if condition like this..

Measure =
VAR number_of_selections = COUNTROWS(VALUES('Table'[States]))
VAR none_selected = "Hi! You are viewing " & FIRSTNOBLANK('Table'[States], TRUE()) //this one shows the first State in case none is selected
VAR one_selected = "Hi! You are viewing " & SELECTEDVALUE('Table'[States])
VAR two_selected = "Hi! You are viewing " & CONCATENATEX(VALUES('Table'[States]), [States], ",")
VAR multiple_selected = "Hi! You are viewing multiple States"
RETURN
IF (
    number_of_selections = 2,
    two_selected,
    IF (
        ISBLANK ( SELECTEDVALUE ( 'Table'[States] ) ),
        none_selected,
        IF (
            number_of_selections = 1,
            one_selected,
            IF ( number_of_selections > 2, multiple_selected )
        )
    )
)

If this works, kindly mark it as a solution. Appreciate with kuddos.

Thanks @Angith_Nair ! The same problem still occurs, when I select 3 values for example, I get only the name of the first State. So weird... In my data modeling I am using a many to many relationship, I wonder if that's intefering in the measure.

Helpful resources

Announcements
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.