I have written a few articles in the past that toy with the ideas of changing visibility and text colour based on selection. I started to wonder if it was possible to make a visual appear (or not) based on a selection from the user. There is no out of the box way to do that today. It is possible to use bookmarks to show an hide an object, but the user must click a specific button to do this. I want the user to be able to interact with a report and see (or not see) a chart based on some valid selection across the report. Microsoft is already working on building expression based formatting across the breadth of Power BI however as of now the only item you can change is the header in a chart and more recently the background in a card.
My solution to solve this problem involves.
- Create a card to work as a mask for the visual.
- Create a measure to display a user message on the card based on the user selection.
- Write a measure that returns either the background colour or “transparent” based on the user selection, and set that to conditionally format the background colour of the card.
I have a sample report below. I want the matrix on the right to appear if the user selects an item in the category slicer on the left. If there is nothing selected in the slicer, I don’t want to see the matrix at all, but instead see some instructions on what to do.
The key to this solution is the expression based formatting of colours using DAX. As I described in my article about conditional based text colours linked above, you can set the result of a measure as being any HEX colour you want using conditional colour formatting. But as it turns out, you can change the transparency of a HEX colour like #FFFFFF by adding an optional additional 2 characters to the end (00 being 100% transparent). So while #FFFFFF is white, #FFFFFF00 is 100% transparent (white).
The next trick was to have a box that I could colour white or transparent – enter the Card.
Create the User Instructions
The first thing I had to do was to create a measure that displayed a message to the user on the card. I wanted to show the matrix when the user had selected an item in the Product[Category] slicer and display the message otherwise. This was easy as follows.
Message = IF(ISFILTERED(Products[Category]), "","You must first select a Product Category from the slicer to see the results")
Create a Card to Work as a Mask
I placed a card visual with the measures [Message] placed in the “Values” section. I made the card big enough to cover the visual. I turned off the card category label. Now the trick is to use conditional formatting for the card background colour using rule based expressions.
Write the Transparent Measure
This was also pretty easy. I wanted to show the matrix when the user had selected an item in the Product[Category] slicer and hide the matrix otherwise. The measure I wrote therefore was this.
Make Transparent = IF(ISFILTERED(Products[Category]),”#FFFFFF00″,”White”)
In other words, if there is a value selected in the slicer, make the “colour” transparent, otherwise make it white (the colour of the report background) so that it hides the matrix.
Format the Card
Then I applied the measure to format the card background colour. To do this, click on the Card, then select format\Background and switch it on. Then click on the ellipses on the right-side of Color as shown below.
Click on Conditional formatting, Select Format by as “field value” and then select the measure.
Importantly, the order of the visuals (front to back) needs to be:
- The card
- The visual to show/hide.
The Final Result
And here is the final result.
You can download my sample workbook here.