Online tableau training, tableau tips, & video tutorials

Advanced

Memberships

How to Make an Integrated Toggle Switch in Tableau

Preview

Instructor

Ryan Sleeper

Flip Between Two Dashboard States with the Click of a Button

In this video, Ryan shows you how to (1) make a toggle switch with custom shapes, (2) integrate the button with a Boolean parameter and parameter actions, and (3) use the button to turn values on and off across multiple worksheets.

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 that allows your users to flip back and forth between two parameter options that are integrated with multiple worksheets on the same dashboard.

For this example, we’re going to be rebuilding this example from my Tableau Public portfolio that looks at sales by US state from the Sample Superstore data set. As you can see, it has two sheets on it, a map view and a top 10 view. By default, both of those views are just showing SUM of sales by state. However, notice in the top-right corner there’s a toggle switch that implies to the user that they can flip something on if they were to click on that.

And what this particular example does is, if I click on that button, the formula moves from SUM of sales to sales per capita, which at times might be a better way to look at the data. For example, if I didn’t look at this as per capita, states like California, New York, Florida, those would probably always be leading the way when the value is summed up simply because they have a larger population. I’m giving the user the choice to toggle the per capita on as well as back off. If I click that again, notice it goes back to SUM of sales. And I see a different image for that button.

This is just one of infinite examples. But what I’m about to show you will be helpful any time you want to toggle between two choices. Step 1 is you need to find your images that will generate this button. I’ve got two buttons because there’s only two choices. When it’s off, you see this toggle with the X on it, and it’s got kind of a blue outline. When I click the button, the contrast reverses. It’s more of a solid color. And we see a check mark, implying that the button is turned on.

You can choose any images you want for this. I recommend, when you create the images or find the images, that you save the images with a file type of dot PNG. That allows the backgrounds to be transparent and gives you the most flexibility when you’re integrating these images on a background. In my particular example here, the background’s already white, so I didn’t technically need that. But I do recommend you try to create your images as PNGs with transparent backgrounds.

My favorite source for these types of images is a site called thenounproject.com. You can use these images royalty free. I also have a pro membership to this that costs me $39.99 per year, which allows me to download as many of these as I want, change the colors on them, generate PNGs or SVG file types and start using them right away.

Here’s just one example over at The Noun Project where I searched for “toggle”, and there are over 1,800 results. If I were to click into one of these, notice, it comes right out of the box in the exact format that I need for Tableau. These kind of checkered gray and white backgrounds are what is showing me what portion of the image will be transparent when I download it as a PNG.

One extra advantage of The Noun project is you can actually change the color of these images as well before you download them. You can also handle that over on the Tableau Desktop side, but it’s a nice little bonus. This site gives you a lot of flexibility when you’re creating your image types. And you don’t have to reinvent the wheel and create them from scratch.

Once you’ve chosen the images that you want to use for your toggle switch, you must take those images and add them to your Tableau Repository. Here’s the location on my PC. If you go to “This PC” and then “Documents”, when you install Tableau, you will automatically get this new folder installed called My Tableau Repository.

There are several folders within your Tableau Repository. These are all files that make Tableau work. One of those folders is called Shapes. These Shapes folders are what correspond with the shape palettes when you’re using a mark type of Shape in Tableau Desktop. You’ll see several folders in there. Most of them, if you’re doing this for the first time, those will be your default shape palettes. But you can also create your own folders and thus your own shape palettes.

So I’ve added a new folder called Boolean Toggle, and I’ve added those two PNG images that I just found at thenounproject.com. Once I’ve got those, I’m ready to jump into Tableau Desktop and recreate that example that I showed you.

The first step to creating a toggle switch in Tableau is to create a parameter. So I’m going to right-click in this blank space on the Data pane and click Create Parameter. And I will just call this my Toggle. The data type for this parameter will be Boolean because there are exactly two options, True or False. So there is the entire parameter. That’s step 1. I’m going to go ahead and click OK.

The next step is to create a calculated field, and this will give Tableau instructions for what to display based on the parameter selection. The current value of that Toggle parameter is currently True. So right now, it’s at True. Just keep that kind of in the back of your mind as you make this. And I will call this my Toggle Button because this calculated field will eventually be used to generate the button, the image that I just grabbed from The Noun Project.

Because there are two outcomes, I’m going to write the formula using the Immediate IF statement. That’s just the letters IIF, open parentheses. And I’m going to say, when that Toggle parameter equals False, then show True. Otherwise, show False. The logic is a little tricky to wrap your head around here. but what this will allow us to do is have one-click access to flipping back and forth between those True and False values.

So the Immediate IF statement, how it works is everything before the first comma, that’s your criterion. That’s your rule. That’s Boolean right now. It’s saying, Toggle– so the parameter Toggle equals False. When that is the case, do whatever is after the first comma. That’s going to flip it to True. If this statement is not true– that’s why it’s so tricky, because it has the word “False” in it. But when Toggle equals False is false, if you will, it’s going to show False. This is going to allow us to flip back and forth. But that is the whole formula. I’ll also provide it in the related content below the video. I’m going to click OK.

Next thing I need to do, if I’m creating my button, is I’m going to change the mark type to Shape. That will allow me to map those custom shapes. And I’m going to drag the Toggle Button calculated field onto the Shape Marks Card. Give that a second to load.

So we’re seeing a shape option for False, and a circle is the default shape. Remember, the current value of the Toggle is True. And this is saying that when it’s True, show the False shape. This is a little tricky, but– it’s going to feel counterintuitive at the moment. But what I highly recommend you do is you map your On button to the False shape.

This will come together when we get through this. I know it’s a little counterintuitive at the moment. But I’m going to map my On shape for the False outcome by clicking the Shape Marks Card and looking for my palette that I set up. All right, and there is my custom palette called Boolean Toggle. I’ll click on that so that I can start to map these shapes.

By the way, if you’re following along and you’re adding shapes to your Tableau Repository for the very first time, these new shapes won’t show up until you click this Reload Shapes button. I had already had those in there from a previous tutorial, so they’re good to go. I can start using them right away.

Again, I’m going to map the On button to False. I can do that by first clicking the False dimension member and then choosing the shape that I want to map to it. You can always click the Apply button to preview the change. That looks good. I’ll go ahead and click OK.

So that I can flip to the other option, I’m going to go ahead and show my parameter control by right-clicking on the Toggle and clicking Show Parameter. Again, the default value is True. Now I can flip that to False. Because this calculated field says, when it equals False, show True, we’re now seeing the True outcome. And I can now go in and edit the True outcome to my Off button. Click Apply to preview the change, and we’re looking pretty good. Click OK, and we’ve got our toggle switch.

The second step is we need to make a calculated field that also integrates that Toggle parameter with our outcomes. So for this one, we’re flipping between sales and sales per capita. Again, you can do this for anything you want to do this with. Any time you’ve got two choices, you’re going to have to apply your own logic. But for my use case, I’m going to make a calculated field, and I’ll call this Sales or Sales Per Capita.

This also has only two outcomes. So I’m going to go ahead and use the Immediate IF statement. And I will say that when the Toggle equals True, then I’m going to show SUM of Sales divided by SUM of Sales– I’m sorry– SUM of Deduplicated Population. That’s what gets me sales per capita.

By the way, this Deduplicated Population, that was created by joining in the population per US state as of the 2010 census. I show you on another video why you need to deduplicate that if you’d like further information on that. But that’s what I’ll display when the toggle is turned on. When the toggle is turned off, I’m going to show the default SUM of Sales. All right, I missed one parentheses there. Close that and we should be in good shape.

All right, and I’ll also provide this in the related content below the video. Again, this is just one of infinite use cases. But if this is of interest to you or if you want to take this Immediate IF logic as the default of your calculation, then plug in your own criterion, go for it. I’ll put it in the related content.

We’ll call this good for now. I’m now going to build out those two sheets. I kind of laid the framework for these before the video. But here’s my map. I’m going to throw sale– first, I’ll change this to a mark type of Map. And then I’ll put my Sales or Sales per Capita onto the Color Marks Card.

So right now, these values are telling me that we’re looking at SUM of Sales. That aligns with my toggle switch, which looks like it is turned off right now. So when the toggle’s turned off, I’m showing the default SUM of Sales. That is exactly what I want.

Now that I have the confidence in that, I’ll also build my top 10 bar chart with the same measure. I’m going to sort this in descending order. And I’ve also got a little trick here that filters those values to the top 10 rows. I show you how to do that on a different video, as well, using the INDEX() function, if that’s of interest.

Now that I’ve got my two charts, let’s go ahead and give this some better default formatting before I move over to the dashboard. I’ll click Number Format, make this Currency (Custom) with two decimal places. All right, now let’s take a look at the dashboard. And I’ve got my map. Let’s start over on that little piece. But I’ve got my map and my top 10. And I’ll hide this axis. I’ve got my top 10 Sales or Sales per Capita by US state.

Now I will add my toggle switch. I’ll make this a floating object. Drag it into the top-right corner. I’ll leave the parameter control just to illustrate that this is working. But I don’t need my Shape legend. It’s kind of muddying up the view. I’ll also hide the title, kind of make this a better size, and put it directly next to where it says Per Capita. I will also fit the entire view so that we’ve got this nice little button there in the top-right corner.

So the button is turned off. And I also see that the parameter value is False. That value is feeding the Sales or Sales per Capita calculated field that is generating the map color and the top 10 by state over here on the right. If I were to click this to True, we would flip over to sales per capita.

Instead of using the parameter control, we’re going to use this new toggle switch. But there’s one last step we need to do to get that to work, and that’s to add a parameter action to this dashboard. To add a parameter action, click Dashboard in the top navigation and Actions. Click Add Action. This is a parameter action, so I’ll click Change Parameter. And I’ll call this my Toggle Switch.

And if you click on the Toggle Switch– so I’ve got this running on Select, which is synonymous with Click– I want you to target the Toggle parameter on the Toggle Button field. That Toggle Button field, remember, that’s the very first calculated field we made that’s allowing us to flip between True and False. So it only ever has two values, and that value is what is feeding the Toggle parameter. So I’ll click that.

There’s no aggregation because it’s Boolean. We’re not summing up anything, so you can leave that as None. I’ll click OK. Click OK again. Throw this into Presentation Mode so we can see if it’s working. Now, if I click on this button, you’ll see the image change to the On button. You also saw things shift, and these numbers on the bar chart are much smaller now. We’re looking at sales per capita.

What happened there is because I set up the dashboard action, that changes the parameter value. When I clicked the Off button, it passed the value of True to my Sales or Sales Per Capita calculated field, which fed my map and my top 10 chart. If I click the button again, it passes the value of False. It flips to the off position. And now I’m back to looking at SUM of sales.

You might have just caught– there’s one thing that’s really bugging me. There’s actually two things that are bugging me about this button that I’m going to show you how to correct as well. The first thing that’s bugging me is I had to do two clicks to change it. So notice, when it’s already selected, I have to click it first to deselect it, and then I can click it to flip it back on. If I want to flip it off, I also have to do two clicks. Deselect it first. Click it again to deselect the button.

I also don’t love that border around there. I went through the trouble of finding those PNG images with the transparent background. But that’s being kind of muddied up. And that formatting has no flexibility, so it’s always a black outline. It doesn’t always fit well with the branding and imaging that I’m looking for.

I’m going to show you a simple trick that fixes both of those things. I’m going to click Escape to exit Presentation Mode. Go to Dashboard Actions. And I’m going to add one more action. This time, it’s going to be a Filter action. And I’m going to set it up to be on the Toggle Switch. I’ll call this Don’t– I’ll call it, actually, Automatically Deselect.

And we’ll say that when you click on the Toggle Switch, go and filter the Toggle Switch Button sheet. That’s the key differentiation here. Most of the time when you use dashboard actions, you’re targeting a sheet that’s on the same dashboard. But if I change this to go straight to the Toggle Switch sheet, this will correct those two issues that I’m having. So I’ll click OK. Click OK again. Test this out one more time in Presentation Mode.

So here’s where we started. If I click the button, it flips right away to sales per capita. It’s automatically deselected. So I don’t see the border. And now I have one-click access to turning it back off. And we’re now looking at SUM of sales again.

This has been Ryan with Playfair Data TV – thanks for watching!

Related video: How to Use Tableau Parameter Dashboard Actions
Related video: How to Add a Show/Hide Button to a Tableau Layout Container
Related video: How to Deduplicate Joined Rows in Tableau
Related video: Using Tableau’s INDEX() Function for Easier Top N Filtering
Related video: How to Turn Normalization On and Off in Tableau
Related blog post: How to Make a Boolean Toggle in Tableau

Calculated field that toggles between Boolean values:
IIF([Toggle Parameter]=False,True,False)

Calculated field that toggles between non-normalized and normalized measures:
IIF([Toggle Parameter]=True,SUM([Sales])/SUM([Deduplicated Sales]),SUM([Sales]))