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
Anonymous
Not applicable

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

I have a Slicer, i.e.

3.PNG

 

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

3a.PNG

In the multiple selections

3b.PNG

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

3c.PNG

 

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

2 ACCEPTED SOLUTIONS
v-yuta-msft
Community Support
Community Support

@Anonymous ,

 

You may create a measure using dax below:

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

Capture.PNG 

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.

 

View solution in original post

@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:

 

No selection.JPG

 

is C selected NO.JPG

 

is C selected YES.JPG

 

is C selected MEasure.JPG

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

6 REPLIES 6
v-yuta-msft
Community Support
Community Support

@Anonymous ,

 

You may create a measure using dax below:

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

Capture.PNG 

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
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
Community Champion
Community Champion

@Anonymous 

 

you can use SELECTEDVALUE in your measure. For example:

 

measure = IF(SELECTEDVALUE(table[project_status]) = “Successful”,  [measure 1], [measure 2])




Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Thank you.

 

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

@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:

 

No selection.JPG

 

is C selected NO.JPG

 

is C selected YES.JPG

 

is C selected MEasure.JPG

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Thanks.

 

In my case, need to change the last line into: if(calc>=1,"YES","NO")

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.