Ryan shows you how to use a custom shapes palette, calculated fields, and parameter actions to create “America’s Best Toggle”. In this example, learn how to toggle between three different dimension members with the click of a button!
Hi, this is Ryan with Playfair Data TV, and in this video, I’m going to show you how to make a toggle switch in Tableau, even if your toggle requires more than two selections. By the end of this video, you’ll be able to make what you see on the screen here, which is toggling a line graph between the Furniture, Supplies, and Technology categories from the Sample Superstore dataset. Of course, you’ll have to plug in your own selections into your own toggle, but so that you can follow along, we’re going to jump over here to Tableau Desktop using the Sample Superstore dataset.
To accomplish this toggle with more than two selections, we’re going to use a technique that I call button swapping. The first step to this button swapping technique is to create a parameter with the choices as your allowable values. In our case, the three choices come from an existing dimension called Category, so to create that parameter, I will simply right-click on the dimension called Category, hover over Create, and click Parameter.
And that is a little bit of a shortcut. It creates a parameter, but it took a couple of extra steps for me. It first set the correct data type, which is String. It also populated the list of allowable values with the dimension members from that dimension that I created the parameter from. So that was just a little bit of a shortcut, and that is in fact exactly what I need. So I’ll go ahead and click OK. Step one is done. Just take note that the name of that parameter is Category Parameter.
The second thing we will create is a calculated field that will highlight the selection and/or filter the selection. It’s a simple Boolean calculated field that I will set up by first clicking this down arrow in the top-right corner of the Data pane and click Create Calculated Field. And I will call this Category Equals Category Parameter, and that actually is the entire formula, Category– it will turn orange because it’s a field from the dataset– Equals Category Parameter, and that will turn purple because it is a parameter in our dataset. That’s the entire formula there. I’ll go ahead and click OK again.
The next thing we need is a calculated field that really makes this whole thing go, and it flips what’s being highlighted on your toggle selector. It will require another calculated field, and I will call this Category Selected. And the formula this time is going to be Category Equals Category Parameter, so think about– we’re going to stop here and think about this for just a second.
Only one category can equal the current allowable value of that parameter at a time, and because– notice, next to the calculated field, there is a little T pipe F icon. That’s Tableau indicating that this is a Boolean calculated field, meaning there are only two outcomes. It’s either True or False. In this case, you either match the parameter allowable value or you do not, and it will create only two results, either a T or an F. That’s what we’ve typed in here so far.
I’m then going to type a plus sign and then the original Category, and this will temporarily break on me. Let’s take a look at the error message to get a hint. It says we can’t add Boolean and String values. The issue is that this portion of my calculated field is a data type of Boolean, and this portion of my calculated field is a data type of String.
Very easy fix here. If I just wrap that first field in the function STR, it will convert those outcomes, T or F, to string values, and now we’re able to add a string to a string. So for each category, it’ll say the name of the category, and then there will either be a T or an F preceding it. That’s what we have so far.
I will click OK. Now I’m going to set up the scaffold for my three selections. I’m going to do that by dragging the Category dimension to the Columns Shelf, and I’ll make this a little bit bigger so we can see it. And I’m going to change the mark type to Shape, and I’m going to map some custom images to each of the three buttons.
If you’re not familiar with this, whenever you install Tableau, you will automatically see a new folder called My Tableau Repository appear in your Documents folder. Within your Tableau Repository, there is a folder called Shapes, and if you double-click on that, you will see a folder for each of the palettes that you can access via the Shape Marks Card. So for each one of these pallettes, you will see an associated folder within your Tableau Repository.
You can actually add your own custom palettes by simply creating a new folder, so I’ve done one called– I’ve nicknamed this technique America’s Best Toggle because it has a lot of bells and whistles, but these are the six images that I’m going to use for my buttons. I’ve created those in a separate program with a file type of .png. By using a file type of .png, I can have a transparent background, which gives me a little bit more flexibility on where I put this button. I can put it on a dark background, for example, and it will be seamlessly integrated with that background.
But what you see here is there is a button in the selected state and a button in the unselected state for each of my three categories. I have placed those in my Tableau Repository within a custom folder called America’s Best Toggle, and now, over here in Tableau Desktop, I will also see a folder called America’s Best Toggle with those associated images.
Now I’m ready to map these images. I’m going to temporarily close this, and this is where that formula that I mentioned makes everything go comes into play. I’m going to use this calculated field on the Shape Marks Card, which will generate a unique shape for each possible combination of Category Equals Category Parameter plus Category. Let me show you what I mean by that.
If I drag that field onto the Shape Marks Card, we should see a unique shape for each possible combination, and take a look over here in the shape legend. I’ll move that over here for us. There is only one of the three that are preceded by a T. I am assuming that that’s going to match the current value of this Category Parameter.
I will edit that parameter to confirm, and sure enough, Furniture is the current value. Therefore it’s preceded with a T because it’s looking at this Category Equals Category Parameter. That is true. It generates a T plus the name of the category. The other two are false because you could only have one of those categories matching the parameter at a time.
Now I’m ready to go in and map some of these shapes, which you can do by clicking the Shape Marks Card, and it will open this palette again. I’ll go to America’s Best Toggle, and I’ll map False Office Supplies to my deselected supplies custom shape. I’ll click F, Technology, and click the unselected Technology button. And for T furniture, I will map the selected state and click Apply.
We’ve mapped the first three. A couple things I’ll change here is I will make the size of those buttons a little bit bigger so they kind of fit and make this kind of seamless selector sheet for me. And we give it a little, tiny bit of space. It looks pretty good.
Now I will show the parameter control, which will allow me to change the current value and therefore change what button is selected, so instead of Furniture, I’ll flip this to Office Supplies. We see some shapes go away. It’s because we have two new things appear. We now have False Furniture and True Office Supplies, so I need to go map those shapes as well. For False Furniture, I’ll choose the unselected Furniture button, and for True Office Supplies, I will choose the selected state for Office Supplies.
I’ll click Apply again, see how it’s looking. We’re back to looking good. I’ll close this one more time because there’s one more option, which is Technology. When I choose Technology, that’s the only combination that I have yet to map, so it turns into one of those default shapes. I need to click the Shape Marks Card one more time and map true technology to the selected state of the Technology button, click apply one more time.
And just to confirm we mapped every possible combination, I’ll flip through the three options one more time. Furniture– when I choose that, the selected Furniture button appears. The other two are in their unselected state. When I click Office Supplies, the blue should move in the middle, which is my custom shape. The other two are unselected, and when I click Technology, we see the Technology button being highlighted.
That is the foundation. That is our control sheet. This will be used to allow the user to just, with a click of a button, change which category is being used throughout the rest of the dashboard. You can now integrate this with as many sheets as you want.
Just to give you a couple of examples, I’m going to make a bar chart and a line graph. First, the bar chart– I’m going to put the Category dimension onto the Columns Shelf, and I’ll choose Sales as my measure. And for this example, I will put the first calculated field we made onto the Color Marks Card. That will color the selection one color and the other two unselected categories a second color. I might change these to be red for True and a gray for False.
Because I changed the current value of the parameter back to Furniture before I created this bar chart, it is Furniture that’s being highlighted. The other two don’t meet this criterion, therefore they’re labeled as False, and they are colored gray.
I will also make a line graph very quickly that looks at Sales by continuous month of Order Date, and just to illustrate that this is working, I will put Category onto the Rows Shelf just so we can see all three at a time.
But then this time I’m going to use that Category Equals Category Parameter as a filter, so I will drag that Boolean filter to the Filters Shelf, which should keep, currently, only the Furniture row. I’ll let go and choose True, and sure enough, we’re left with just the Furniture row.
Just laying some foundation here. Both of these charts are eventually going to be controlled by the selection that we laid out on the control sheet that I’m calling Category Toggle. The next thing I’ll do is put all of this on a dashboard together. To use this toggle, it requires a dashboard because we’re going to use a dashboard action to pass the parameter value to the bar chart and the line graph.
So I will put on my bar chart and my line graph. I’ll clean this up just a little bit, maybe make the bar chart a little bit thinner, maybe add some blank space at the top, which will give me some room to put my Toggle Control Sheet in. And for my Toggle Control Sheet, I’m going to make that object floating, and I will place that near the top.
And I’ll hide some of these titles, some headers just to clean this up a little bit. I’ll fit the entire view, and I’ll move this up to the top-left corner. And then we make this background transparent so that it doesn’t overlap with my bar chart. And I’ll delete the shape legend, but I’ll keep the original parameter control that came over when I added furniture or when I added this Category Toggle to the view, just another confirmation that it’s working.
So now that everything’s on the same dashboard, there’s one last step to get this to work. I need to add a parameter dashboard action by going to Dashboard in the top navigation and then click Actions, and I will click Add Action. It is a parameter action, so I will choose Change Parameter. And relatively simple logic, but I will call this my Category Selector to give it a name.
And I will say that if you click on the Category Toggle– so I’ll de-select the other two sheets on the view. I will leave this as the default, which is Select, which is synonymous with click, so so far I’ve said if you click the Category Toggle, I want you to target the Category Parameter. And the most important selection is, what value do I want to overwrite the current value of that Category Parameter with?
I want it to overwrite the Category Selected. Actually, the Category. I apologize. So Category is going to pass one of these three names when it’s clicked on. If I chose Category Selected, remember, that’s going to pass not only the category name, but the T or the F that precedes it. Close, but not quite what we want.
The allowable values are simply the three names of my categories, so I’ll choose the first option. That’s it for my parameter or for my parameter action. I will click OK, click OK again to close this.
You don’t see anything change yet, but I’ll go to Presentation Mode to test this out. If I click the Supplies button, that Supplies value is passed to my calculated field that’s computing whether or not it matches the parameter. Notice that it changed not only the highlight because we’re highlighting the bar chart by that field that shows the T or the F plus the name of the category.
Also, that same field is being used to filter my line graph. Notice the row header changed from Furniture to Office Supplies. If I click Technology, that Technology category is passed to the parameter, which is then passed to the calculated field that is highlighting the left and filtering the right, and we’ve just made a custom toggle with more than two selections using a technique that I call button swapping.
This is a really nice tactic. There’s only one little, small thing that’s bugging me, and it’s that when I selected Furniture, it not only selected that button, but it highlighted it so that these others are a little bit too grayed-out for my liking.
If that’s bugging you as well, I show you on another video how to avoid highlighting dashboard selections, but for now, this has been Ryan with Playfair Data TV – thanks for watching!