Forum Discussion

DW868990's avatar
DW868990
Helper IV
3 years ago
Solved

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

3 Replies

  • ValtteriN's avatar
    ValtteriN
    Community Champion

    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:


    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/



    • DW868990's avatar
      DW868990
      Helper IV

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

      • ValtteriN's avatar
        ValtteriN
        Community Champion

        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.