Show or Hide a Power BI Visual Based on Selection

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.

Solution Overview

My solution to solve this problem involves.

  • Create a Treemap with 1 large and 1 small value.
  • Write a measure that returns either the background colour or “transparent” based on the user selection, and set that to conditionally format the colours of the Treemap.
  • Create a measure and card to display a user message.

Requirement

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.

Transparent Colours

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 Treemap

Create a Treemap

This was the obvious contender but I had to do some experimenting with it to work out how to do what I wanted.  I started out writing a measure VALUE = 1 and added that to the Treemap, however it seems that you can’t conditionally format the colours of the treemap if there is only 1 value displayed.  The solution I settled for was to create a simple table of data with 1 very large number and 1 small number.  I created a new table using enter data as follows:

ID     Value
A      10000
B          1

I loaded the table and added the ID to Group and Value to “Values”.  This gave me a Treemap with 1 very large box, 1 very small box (so small you can’t see it) and it would accept conditional formatting of the colours using rule based expressions.

image

Write the Transparent Measure

This was pretty easy.  I wanted to show a matrix when there 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).

Format the Treemap

Then I applied the measure to the Treemap.  To do this, click on the Treemap, then select format\data colors\advanced controls, then select format by “field value” and then select the measure.

image

While I was in the Treemap formatting pane, I turned off all the optional formatting such as borders, titles, data labels etc.

Create the User Instructions

The last thing I had to do was to create a measure that displayed a message to the user.  This was also easy as follows.

Message = IF(ISFILTERED(Products[Category]),
     "","You must first select a Product Category from the slicer to see the results")

I added this measure to a card, turned off all the card category and placed it on top of the Treemap.  Importantly, the order of the visuals (top to back) needs to be:

  1. Card
  2. Treemap
  3. Visual to show/hide.

Note 1: that the selection by the user is based on the Slicer that directly filters the treemap.  It will not work with highlighting.  You can edit the cross filter behaviour from “edit interactions”.

Note 2: as pointed out by a reader, you could also use the new custom title feature to display the message instead of the card.

The Final Result

And here is the final result.

transparent measure

You can download my sample workbook here.

Share?

Comments

  1. Nice workaround. Coming from the Qlikview world and having used that for the past 8 years, it’s amazing to me that simple SHOW/HIDE conditions are not available yet in PBI. This conditional formatting they are starting to roll out makes me feel like they are starting to get it and PBI is getting more and more Qlik like with every release.

  2. This is awesome Matt! I’ve been trying to find a way to do this as well, the treemap approach is pretty brilliant.

    One thing to note regarding optimization: the matrix is still calculating even though you can’t see it. That means a potentially large DAX query is still running in the background…possibly slowing down other visuals.

    You may want to adjust your matrix measure to the following:
    [Matrix Measure with Check] :=
    IF(
    ISFILTERED( Products[Category] )
    ,[Matrix Measure]
    ,BLANK()
    )

    This will return BLANK() if there’s no selection, saving the other measure from being evaluated.

    • Yes, this would be another approach. I was really trying to find a problem to match my solution – the idea of using transparency to hide or show an object on the canvas. I am sure my solution has other applications too. For example, you could create an instructions section on the page, and overlay multiple text boxes with multiple instructions, all set to transparent text and transparent background. When certain conditions are met, you could display the appropriate instruction by changing the transparency.

  3. Could this work to highlight a sub-group in a scattergram? i.e. make the unselected sub-groups more transparent than the selected one.

    • The principle of a measure that formats a data point is the same, I think. So as long as you can write the DAX, you will be able to do what you suggest. But what you will need to do is have your slicer either with an inactive relationship, or no relationship at all, otherwise the slicer will filter the visual. I assume that changing the cross filter behaviour between slicer and visual will not work in this case, but I can’t be sure.

      • Great thanks, I solved it as you say by overlaying two scattergrams with the same set axes and one which has an inactive relationship to the slicer

  4. Hi Matt – Great Article, when i was experimenting with the file i realized that instead of writing the hex code for transparent as #FFFFFF00, one could just write “transparent” and it works.

  5. Is it possible to do the trick on two slicers with an OR function or similar? So the message would disappear if one selected the first slicer or if they just selected the second slicer?

    • Another way to accomplish this is to use visual level filters combined with dynamic titles. Create a measure that is 1 if filtered, 0 if not. Drag that measure to filter pane and set to only show when measure=1.

      You can also use that measure within the dynamic title to have it display a message. I use this for data tables when I don’t want all of the information showing until a user has filtered it.

      • @Stu I use that trick too. But this method of obscuring both the data and the headers means you can do additional stuff like overlay different types of visuals on top of each other, and trigger a bookmark-like experience from a Slicer, which is very cool (and should be allowed natively).

  6. Hey Matt…I just spent ages trying to replicate your trick to no avail, until I finally realised that I was trying to trigger it from a Table (not a Slicer) and the default behaviour was to crossfilter the Treemap rather than filter it. Meaning it didn’t work. Perhaps you could include another step in your instructions above showing how to change the “Edit Interactions” behaviour of the TreeMap from Crossfilter to Filter, so this cool trick can be used for other types of trigger visuals.

    But I love it, man.

  7. Dang: This doesn’t work for me when saved to the PBI service and rendered in IE (version 11.0.120).
    Works fine in chrome. But our users have IE as the default.

    Anyone manage to get it working in IE?

  8. Hi Matt — really enjoy your articles — have your website on my regular “to read” list. Your insight of using Treemap as a controllable color shape was brilliant and an eye opener for me. I had a project where I needed the background color of a matrix to be controlled by filters. So I adapted your approach: I made the background of the grid 100% transparent — and then put the treemap “background” behind it that responded to the filters via DAX formula. It worked perfectly. Never would have thought of using treemap as a controllable background without this article. Many thanks!

  9. Hi, Instead of Slicer selection, I tired with Matrix row selection. With the Matrix row selection, I am not able to achieve the same thing. Could you please let me know how to use with Matrix row selection (SelectedValue) instead of a Slicer? Thank you in Advance.

  10. My guess is that you have cross filtering set to “highlight”. If that is the issue, change “edit interactions” to “filter”. It is on one of the menus visible when you select the matrix.

Leave a Reply