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
DW868990
Helper IV
Helper IV

Switch Help when multiple selections, to return single value

Hi,

 

I have the below switch statement - 

 

ErrorSwitch =

SWITCH(
    TRUE(),
    [PageFilters] = "Product1", 1,
    [PageFilters] = "Product1" & "Product2", 2
)
 
The measure within the above basically just returns what filters the user has applied, all the filters come from the same column/slicer.
 
Now i need the switch measure to return the single number value when the measure equals what products have been selected, it works fine for the first 1 when it is just one product, but does not when i specific more than one product in the switch statement.
 
Any advise appreciated
 
JJ
1 ACCEPTED SOLUTION

Hi,

You can expand on the logic e..g:


Measure 21 =
var _filter =
VALUES('Table (9)'[Item])

 

var _count = COUNTROWS(FILTER('Table (9)','Table (9)'[Item] in {"Peach","Pumpkin","Apple"}))
return

 

SWITCH(TRUE(),
_count=1 && MAX('Table (9)'[Item]) = "Peach",1,
_count=2,2,
_count=3,3
)


The basic idea with switch + true is the following:

SWITCH(TRUE(),
Condition1, Result1,
Condition2, Result2,
Condition3, Result3,
...)

So you can keep addiding conditions and scenarios to mathc your logic.




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
ValtteriN
Super User
Super User

Hi,

Here is one way to do this:

Measure 21 =
var _filter =
VALUES('Table (9)'[Item])

var _count = COUNTROWS(FILTER('Table (9)','Table (9)'[Item] in {"Peach","Pumpkin"}))
return

SWITCH(TRUE(),
_count=1 && MAX('Table (9)'[Item]) = "Peach",1,
_count=2,2)

 
Here I am filtering a table based on the products I want to evaluate then using countrows to determine how many items are selected in filters. This way If _count =1 and item is "Peach" result is 1 but with item = "Pumpkin" the value is blank. With 2 items the only possible result is 2 since the table only allows for these two values.

You can utilize and expand this logic to solve your issue:
ValtteriN_0-1666094411358.pngValtteriN_1-1666094423088.png


I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/







Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thank you @ValtteriN , nearly there but how does the switch statement when have to select 3 specified items and return a singular value?

Hi,

You can expand on the logic e..g:


Measure 21 =
var _filter =
VALUES('Table (9)'[Item])

 

var _count = COUNTROWS(FILTER('Table (9)','Table (9)'[Item] in {"Peach","Pumpkin","Apple"}))
return

 

SWITCH(TRUE(),
_count=1 && MAX('Table (9)'[Item]) = "Peach",1,
_count=2,2,
_count=3,3
)


The basic idea with switch + true is the following:

SWITCH(TRUE(),
Condition1, Result1,
Condition2, Result2,
Condition3, Result3,
...)

So you can keep addiding conditions and scenarios to mathc your logic.




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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.

Top Solution Authors