Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

How to write a DAX function when a Slicer value is selected/included?

I have a Slicer, i.e.

 

I want to write a logic DAX function using IF() like:

  • If "Successful" is selected, then do A, otherwise do B
  • "Successful" could be selected as a single or one of multiple selections, e.g.

Singe

In the multiple selections

  • When "Successful" is not selected, e.g...

 

How to write the DAX function to identify whether or not "Successful" is selected?

  • Anonymous ,

     

    You may create a measure using dax below:

    Result = IF(CONTAINSSTRINGEXACT(CONCATENATEX(ALLSELECTED('Table'), 'Table'[project_status], ""), "Successful") = TRUE(), "A", "B")

     

    Community Support Team _ Jimmy Tao

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

     

  • Anonymous 

     

    I see v-yuta-msft  has provided a solution (I wasn't even aware of the function CONTAINSSTRINGEXACT!)

     

    Just as an alternative, I worked on a different approach:

     

     

     

     

     

7 Replies

  • v-yuta-msft's avatar
    v-yuta-msft
    Community Support

    Anonymous ,

     

    You may create a measure using dax below:

    Result = IF(CONTAINSSTRINGEXACT(CONCATENATEX(ALLSELECTED('Table'), 'Table'[project_status], ""), "Successful") = TRUE(), "A", "B")

     

    Community Support Team _ Jimmy Tao

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

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Note that this method breaks if you have two rows in the slicer table that contain the same text, e.g.

       

      "Sales"

      and "Sales (adjusted)". When "Sales (adjusted)" is selected in the slicer, the code with think that the "Sales" option was selected, when it wasn't.

       

      Result = IF(CONTAINSSTRINGEXACT(CONCATENATEX(ALLSELECTED('Table'), 'Table'[project_status], ""), "Sales") = TRUE(), "A", "B")

      The COUNTROWS method that PaulDBrown posted might be more robust.

  • PaulDBrown's avatar
    PaulDBrown
    Community Champion

    Anonymous 

     

    you can use SELECTEDVALUE in your measure. For example:

     

    measure = IF(SELECTEDVALUE(table[project_status]) = “Successful”,  [measure 1], [measure 2])
    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you.

       

      But it only works for a single selection. If I select multiple values including "Successful", it can't work...

      • PaulDBrown's avatar
        PaulDBrown
        Community Champion

        Anonymous 

         

        I see v-yuta-msft  has provided a solution (I wasn't even aware of the function CONTAINSSTRINGEXACT!)

         

        Just as an alternative, I worked on a different approach:

         

         

         

         

         

  • Git's avatar
    Git
    Frequent Visitor

    Here is simpler one: it works if we have to keep a check if any option is selected, If nothing selected or selected both case need to be worked, then remove Is filtered and use if satetment.

    To make sure Slicer is filtered:

    Check Selection = SWITCH(ISFILTERED('Table'[Value]),
     "Successfull" IN ALLSELECTED('Table'[Value]),"y","n")



    When nothing selected/Something selected bot scenarios to cover:
    Filtered/NotFIltered=If("Successfull" IN ALLSELECTED('Table'[Value]),"y","n")