Bar chart widths can become a headache when swapping Date Levels from say a specific year to a certain month. You may have tried some common techniques like changing the Date to Discrete, but the width is still not right.

You can avoid this headache and achieve the beautiful, pixel-perfect bar charts we all love with these simple instructions.

Step 1: Create the date level calcs

You will need to create Date Level Calcs, so begin by selecting the Parameter of your Date Level.

To give you an example, we used a String List Parameter with our different Date Level choices including year, quarter, month, week and day (shown above).

You will notice the Parameter values on the left are in lowercase. This saves time since you will be using the parameter with a Datetrunc instead of having to write a CASE statement, or an IF/THEN statement.

Now, create a calculation and name it (in this case it’s been named DateSwap). The calculation should be the following formula.

Step 2: Plot the chart

In this step, you will plot the DateSwap field on the columns shelf and select Exact Date. This will plot the marks in the correct places. Now change the Mark Type to Bar.

You’ll see something like this:

Since this viz has the Parameter currently set to Year, and the Date field is configured to be exact date, the result is four bars plotted on a much larger axis. However, the viz is still far from beautiful.

To make the bar widths look better, you’ll need to either drag the size selector all the way to the right, or change the size mark to fixed (shown below). But there is still another step to perfect your bar widths.

Step 3: Create pixel perfect bar width

Thanks to the Data + Tableau + Me website, we have the secret to get the perfect bar widths. Once you complete Step 2 and set the size mark to fixed, drag a parameter onto the size mark to make the units dynamic. This technique works for the calc/field as well.

When we tried this out for ourselves, we had the X-Axis as each day across the range of our data. As we change date levels, we plot the bar chart on the 1st day of the date level. For instance, the year would be January 1st, the month would be the 1st of the month, the week would be the 1st day of the week, and so on.

However, not all years have the same number of days, just like not all months have the same number of days. So, we adjusted the calc to tell us how many days are in each unit as the Date Level changes.

The new calculation is as follows:

The calc above finds the number of days in any year, quarter, month, etc… by taking the date difference in days from the date in question, and the next unit in line.

Here, the [DateSwap] changes our dates to the level configured in the parameter [DateLevel], and calculates the difference in days to give the correct number of days in a particular year, quarter or month (week and day also apply, however, those values are static).

Once we have that calc, our job is super simple. Change the size mark to fixed, and drag the size calc onto the mark.

In order for this to work, after you have put the Size calculation on the view, right click and set it to a continuous dimension.

And that's it!

You can expand on this however fits your needs. You can put Parameters on there for Marginal Histograms, or whatever other calcs you need, just make sure it's Continuous!

Now you can change Date Levels, and have pixel perfect width Bar Charts along the way! Play around with the dashboard below.

こちらもご覧ください

コメント

When I don't want to see the gap interval between static/computed date values, I change the date axis to "discrete" and tableau does the magic itself.

Hello Allan. Using Discrete is a very valid option, and it works great. This solution aimed to solve an aesthetic problem, i.e. using Discrete will leave you with white spaces between the bars (you can manually adjust the size but that can sometime leave results that are less than desirable).

One thing to note, if you have a continuous date with a bar chart with nothing on size shelf, this should automatically work when you drill down/up. You'll notice that bar charts with continuous dates will automatically default to Fixed sizing and Fixed tab on size uber tooltip will say Automatic when there is nothing on the size shelf. This indicates that Tableau is generating similar calculations under the cover to show the right size bars reflecting 31 day vs 28 day months etc.

Great point Bora! You are correct that it works perfect when using the Date Axis to drill up/down. This tip came out of me needing to use a Parameter to control the date level of multiple charts on a Dashboard. Since I couldn't rely on the user manually going to each sheet to drill to the desired level, I ended up doing this instead. That being said, I hope that there are other elements to this post that people can benefit from, such as, finding the number of days in a period, or using the lowercase parameter in the calculation.


新規コメントの追加 

ブログの更新情報を受け取る