MIP Logo

Change colours in a Tableau chart from user input

This post was originally published on The Data School blog between 2018 and July 2025, before our program was renamed to MIP’s Analytics Career Accelerator. References throughout this article to “The Data School” or “DS” all refer to what is now MIP’s Analytics Career Accelerator. The program, its people, and its commitment to launching outstanding analytics careers remain the same – just under a new name.

If you are having trouble viewing this article, please report it here

Creating bar charts in Tableau is simple. You can drag and drop relevant fields in the Columns and Rows shelves. But what if you want to add some interactivity to your bar charts? In this post, I explain how the colours of a bar chart can change based on user-defined inputs.

 

If you’d like to follow along, click here to download the data and follow the steps:

Open Tableau and connect to the dataset. Drag Category to Columns. Drag Total Spent to Rows. It will aggregate to Sum which is what we want and we will get a bar chart like this:

bar chart

Next, we have to create a reference line. Right click on the y-axis and click on Add Reference Line.

screenshot showing add reference line option on y axisThe Value option shows Sum(Total Spent). But we want a reference line that is not based on the Total Spent hardcoded into the visual, we want one that can be changed by the user with a parameter. So click on the drop down and for Value and choose Create a New Parameter.

Option to create a new parameter

A new window will pop up for the new parameter. Add in the following details:

Under Name you can call it whatever you want, I have called it Target. Since we are basing the parameter on the Total Spent values, Tableau has already defined the Data type for us, which is Float. Keep the Allowable values to All and change the Display Format by choosing Currency (Custom) and remove the decimal places. Since we are dealing with large numbers, the decimal places are not relevant. Also, choose the option to Include Thousand Separators. Keep the Current Value to 100,000. Finally, click on OK.

You should be back to the reference line window on which the Value will be the name of the new parameter you just created:

The newly created Target parameter will show up at the bottom of you Data pane. Right click on it and choose Show Parameter. It will then show up on the right side of your visual.

Show parameter option in Tableau

You can change the value from 100,000 to say, 200,000 to see if the target reference line moves. This will help us make sure the parameter is working. Next, we have to create a calculated field to change the colours of the bars based on the value. So, if the Total Spent for a particular category did not meet the target value, the bar should appear in a colour different from the those categories that did.

For this, let us create a calculated field. Go to the drop down arrow on the Data Pane and click on Create Calculated Field.

Write the following calculated field:

if
SUM([Total Spent]) > [Spent Target]
THEN “Above Target”
ELSE
“Below Target”
END

What we are defining here is a new field that computes if a Total Spent is greater than the Total Spent chosen by the user in the parameter; and if it is, then it is called “Above Target” and if not, it is “Below Target”. Remember, Tableau shows parameters in purple in calculated fields. So it is easy to identify which is the parameter.

Now drag the newly created calculated field onto your Colours Marks Card.

The colours of the bars will change based on the value you have kept in the Target parameter. You can change values in the Target Parameter to see the bar colours change dynamically.

 

Share this post