Recently I saw some examples of Dynamic Number Formatting on Twitter, so I though I'd share a solution I came up with a few years ago.
This solution uses some simple math to determine the base 10 of a number. Using that base 10 we can determine how to round it, and which numeric symbol it should have. Solution also removes 0 decimals, and handles negatives.
Copy/Paste this calc and change [Value] to the field you want to have dynamically formatted. NOTE* we are aggregating in the calc, so if you want to dynamically display a value that is MAX/MIN/AVG/ETC, change the aggregation in the formula.
//Determine if the value is negative
IF SUM([Value]) < 0 THEN '-' ELSE '' END
+
'$' //Dolar Symbol
+ //Get Int Amount by Dividing the Value by its' Base 1000 Value
STR(INT(ROUND(ABS(SUM([Value]))/POWER(1000,INT(LOG( ROUND(ABS(SUM([Value]))/10,0)*10 ,1000))),1)))
+ //Determine if you need to show a decimal //If the Modulo is zero, we just want to show blank (Ex 159.0K = 159K) //Else we want to show the decimal value (Ex 159.4K)
IF (ROUND(ABS(SUM([Value]))/POWER(1000,INT(LOG(ABS(SUM([Value])),1000))),1)*10)%10 = 0 THEN ''
ELSE '.' + STR((ROUND(ABS(SUM([Value]))/POWER(1000,INT(LOG(ABS(SUM([Value])),1000))),1)*10)%10)
END
+ //Determine the Unit to display based on the Log Base 1000
CASE INT(LOG(ROUND(ABS(SUM([Value]))/10,0)*10,1000)) //Unit Symbol
WHEN 1 THEN 'K'
WHEN 2 THEN 'M'
WHEN 3 THEN 'B'
WHEN 4 THEN 'T'
ELSE ''
END
I recently saw a great chart from Pew Research via Steve Wexler on Twitter.
This shows a 2X2 Matrix of survey data in 4 quadrants of favor/oppose. This is a really great chart, and it can 100% be created in Tableau.
Ramon Martinez quickly showed how you can use Polygons to create it.
After looking at this for a minute, I realized that there are bar chart features in Tableau that would allow us to create this chart without data-prep.
Steps
When we look at the chart, there are things we can immediately take away, for one, there are 4 quadrants around a 0,0 center point. From this point, there are squares sized by area around it.
While we can't use the "Square" shape mark to do this, we can use Bar Charts to simulate a Square.
The key here is to use "Fixed" size for Bar Chart. We can do this by setting up a Continuous X Axis that will let us place the Bars according to a specific value (in this case Sales).
I am using a conditional calc to control where on the X Axis the SQRT of Sales will fall. For the right side of my quadrant, I want the SQRT of sales to be positive, for the left side I want them to be negative.
CASE MIN([Region])
WHEN 'Central' THEN -SQRT(SUM([Sales]))
WHEN 'East' THEN SQRT(SUM([Sales]))
WHEN 'West' THEN -SQRT(SUM([Sales]))
ELSE SQRT(SUM([Sales]))
END
Using the Circle Mark, you can see how this places the values.
Now that we have the X Axis, the next step is to get the Height. We are going to use a similar conditional calc to get this. For our Top 2 Quadrants, we want to get the Positive value for the SQRT of Sales, and the Bottom Quadrants we want to the Negative value for the SQRT of Sales.
CASE MIN([Region])
WHEN 'Central' THEN SQRT(SUM([Sales]))
WHEN 'East' THEN SQRT(SUM([Sales]))
ELSE -SQRT(SUM([Sales]))
END
Placing that on the Rows Shelf, we can start to see the shape of the chart.
The next step is to change the Chart Type to Bar, which will look a bit funny....
One of the great features to come out of Tableau in the version 10 series is the "Fixed" size option for Bar Charts. While the original intention for them was to create marimekko chart, but we can use the feature laterally to do other things (I do this alot). This let's use define a Width for the Bar Charts according to a measure. Since our X/Columns shelf is using a Continuous Dimension (which we got using LoDs), we take the opposite value of it an place it on the Size Mark and set it to Fixed.
Alternative Approach:
Quickly after writing this, I realized there was another option. Instead of using the Width Calc on the Columns Shelf, and then using - Width on the Size Mark, we can use MIN(0) on the Column Shelf and Width on the Size Mark. By turning Stack Marks Off, we can get the same result.
IF YOU HAVE NOT READ http://drawingwithnumbers.artisart.org/using-a-filter-action-as-a-parameter/ BY JONATHAN DRUMMEY, STOP READING NOW!
Everything I am going to show is based on Jonathan's technique to simulate a Parameter using a Dashboard Action. For my examples, I am using the Union Scaffold Technique. After you have read/understand his post, come back here to see some examples on how to use it?
After you have read Jonathan's blog post, let's start with some examples!
Connect Scatter Plot on Hover
As Jonathan describes in his post, the first thing I am going to do is Setup a Union scaffold of my datasource. This will allow me to set a Target and Source field to work off of.
Begin by setting up the sheet with Category and Year, then bringing over the blended Sales and Profit
Next, we need to setup a field that we will filter on, to identify which marks we need to do additional work with.
[Category Include]
[Category] + [IncludeExclude]
Note* Here [Include-Exclude] is driven by the Union, i.e. I have one Table be set to Include, and the other set to Exclude.
Add this to the detail shelf.
Next add the sheet to a Dashboard so we can setup a Dashboard Action. Then create a Dashboard Action like so.
Here we are doing a few things. One, we are setter the Filter to Select and Leaving it, we need this so the Action Filter Set remains in place on the Worksheet, so we can adjust it.
We are also setting up the Action so that it directly affects the sheet in question, i.e. I am using the sheet to filter the sheet.
Finally, I am using the Category-Include field as the filter.
Go back to the Sheet and click on a mark.
As you would expect, the filter is working as we might expect. We clicked on a Category, and the sheet filtered it. So what's the trick?
Now on the Action Filter, notice that each Category has two values, one for Include and one for Exclude. By setting the Action to Exclude, we only remove one instance of the Category. This means we can use an LoD to identify which Category has been excluded.
So I am going to setup a Dual Axis that only renders when there is one category in the in view.
[Dual]
IF MAX({ EXCLUDE [Category-Include] : SUM(1) } = 1) THEN SUM([Sample - Superstore].[Profit]) END
*The trick: By excluding the [Category-Include] field, we can test to see how many "Categories" are in the view. If there is only 1, we know that we have excluded the other mark. So we can take that and tell Tableau to grab another instance of Profit.
Add this to you sheet as a Dual Axis and Synchronize, remove Measure Names and change the Mark to Line.
Now we have what we need. So the last step is to go back to the Dashboard Action and set it to Hover and Show All Values
This will allow you to hover over marks, trigger the filter, then instantiate the Dual Axis.
And viola! Connect a Scatterplot on Hover!
Other Options
The following are gifs of other examples on how to utilize this trick. They all work on the same premise, just in slightly different ways.
Halo on Hover
Super Bump Chart Highlight
Dynamic Reference Bands with Highlight
Self Highlighting Bar Chart
Here are the examples in a workbook. Feel free to download it and play around with the concepts.
Note* This is a hack, and a bit buggy on Tableau Public.
I've been working a lot with Polygons lately, trying to build more "templates" in Tableau that can be reused by the community. My last post on Polygon Hex Maps is a lot easier to implement than this one, if you follow it step by step. So instead of going through the hassle, here is a shortcut if you just want to use this, but don't care to go through building it yourself.
Shortcut:
This is going to be a really long post, because of all the calculations, so to make things easy, I'm going to give you a shortcut up front.
Then add your datasource to it, blend on Date, and drag whatever measure you are interested in onto the Color mark of the Polygon.
How it works:
If you are sticking around to see all of the Calculations for this, get ready, it's a lot.
Step 1: The Data
As always, we need to start with the Data. I had a few ideas in mind on how I wanted to set this up, but ultimately landed on a model I thought would be the most flexible.
The primary sheet to this data set has 4 columns: DayOfYear | Type | Type Major | Join.
There are 3 TypeMajors: Month | Weekday | Poly
That gets further broken down by the type within it, either the Month Name or Weekday name.
The DayOfYear is a bad naming convention, but I already wrote it and don't feel like changing it. This value is specific to the Typ, for the Polygon, it actually is the DayofYear (1-366), but for Month and Weekday, it is those corresponding values, January = 1, Wednesday = 3, etc.
This dataset then gets joined to a scaffold, used mostly for the Polygon Marks.
Now that we have our Data, we want to setup some Parameters. These are what will allow us to have a configurable Calendar.
Step 3: Base Date Calculations
After we have our Parameters, we need to start building a lot of calculations around it.
[Date] =
DATE(DATEADD('day', [Day Of Year], MAKEDATE([Year],[Month Start],1)-1 ))
This is a dynamic calculation to let us build up a year of dates using the DayofYear field, and the Year/Month chosen in the Parameters. The idea of this is to make a date that can cross-years, which serves to benefit individuals working off a Fiscal Calendar.
[Month Switch] =
CASE [TypeMajor]
WHEN 'Poly' THEN DATEPART('month', [Date])
WHEN 'Weekday' THEN [Point]
WHEN 'Month' THEN [Day Of Year]
END
Here we are using a simple case statement to change get the correct value based on the TypeMajor from our dataset.
Building on top of the [Month Switch] calculation, we determine what the proper ordering of the months are based on the Fiscal Month start configured in the Parameter.
Now that we have our Month calcs, let's do the same for Weeks
[WeekSwitch] =
CASE [TypeMajor]
WHEN 'Poly' THEN DATEPART('weekday', [Date])
WHEN 'Weekday' THEN [Day Of Year]
WHEN 'Month' THEN [Day Of Year]
END
Pretty much the same thing as [Month Switch]
[Param Weekday Order] =
CASE [Week Start]
WHEN 'Sunday' THEN 1
WHEN 'Monday' THEN 2
WHEN 'Tuesday' THEN 3
WHEN 'Wednesday' THEN 4
WHEN 'Thursday' THEN 5
WHEN 'Friday' THEN 6
WHEN 'Saturday' THEN 7
END
Just setting up a calculation to determine the base days of the week driven off the parameter chosen by the user.
Similar to [Month Order], we need to figure out the correct order of the weekdays based on the selection from the user.
[Filter] =
{MAX(DATEPART('dayofyear', [Date]) )} >= [Day Of Year]
Since our datafile has 366 days in it (to handle leap years), this just allows us to remove an "extra" date that might pop in.
Step 4: Positioning Calculations
Now that we have our date calculations in order, we need to build off of them to properly construct our calendar. We start by getting the overall base setup by months.
[PushX] =
([Month Order]-1) % 3
[PushY] =
INT(([Month Order]-1) / 3)
If this looks familiar, it's because this is the same functions as setting up a trellis, but here we are driving it by the [Month Order]
Setting up the Polygon Points
[PointAdjusterX] =
IF [TypeMajor] = 'Poly'
THEN
IF [Point] = 1 OR [Point] = 4 THEN 1
ELSEIF [Point] = 2 OR [Point] = 3 THEN 2
END
ELSE [Point]
END
[PointAdjusterY] =
IF [TypeMajor] = 'Poly'
THEN
IF [Point] = 1 OR [Point] = 2 THEN 2
ELSEIF [Point] = 3 OR [Point] = 4 THEN 1
END
ELSE [Point]
END
These calculations simply define the base points of the polygon. This is the same for all, but we will "move" by the data.
Bringing it all together
Now it is time to bring all of this together so that we can plot it in Tableau. We will start with the calculations used for the Polygons
[PolyX] =
IF [TypeMajor] = 'Poly'
THEN (([PushX]*7) + [WeekdayOrder]) + [PointAdjusterX]
ELSEIF [TypeMajor] = 'Weekday'
THEN ([PushX]*7) + [WeekdayOrder] + 1 + 0.5
ELSE ([PushX]*7) + 5.5
END + [PushX]
This is where things get fun. Essentially, this calculation is "moving" the polygons based on the Data and what has been configured in the Parameters. This will allow us to plot all of our dates on the X Axis, while having a nice space between them.
We also include logic for our other two TypeMajors Weekday | Month. This will allow us to use a Text Mark in a Dual Axis to work as Fake Headers.
We are doing something similar for the Y, except we also figure out what the "WeekOfMonth" by subtracting the Week of our Date (by the weekstart) from the Min Week of that Month. All of this is driven by the parameters, so these dates are completely customized.
We only grab the marks for Poly so that we don't get weird polygons from our other data, we will bring that in on a Dual Axis.
Step 4: Plot the Polygon Calendar
1. Drag [PolyX] onto the Columns Shelf
2. Drag [PolyY] onto the Rows Shelf
3. Place [Date] on the Detail Shelf.
4. Change mark type to Polygon
5. Drag Point onto the Path Shelf
6. Drag [Filter] onto the Filter Shelf and Select True
Looking good! However, at this point, I have no idea what is what. So let's add some labels.
Step 5: The Headings
First, we need to create the calculation that will allow us to plot the Headings. We already have the points for the X defined on the PolyX calculation, so the next step is to get the Y coordinates.
[Y2] =
IF [TypeMajor] = 'Weekday'
THEN ([PushY]*8) + 1
ELSEIF [TypeMajor] = 'Month'
THEN ([PushY]*8)
END - 0.5
This is giving us rows for the Month, and then the individual Weekdays. Since only the Order of the Weekdays will change on the X axis, here we just figure out where to place them on the Y.
[Label] =
IF [TypeMajor] = 'Weekday'
THEN LEFT([Type],1)
ELSEIF [TypeMajor] = 'Month'
THEN [Type]
END
This will give us cleaner (smaller) names to work with so that we can fit everything in a nice space.
1. Drag [Y2] onto the Rows Shelf
2. Dual Axis and Synchronize
3. Change Mark Type to Text
4. Add Labels to the Text Mark
5. Remove Measure Names after you Dual Axis.
6. Hide Headers
At this point you could be done. And I'm sure alot of people would want to be. But I'm going to add just a bit more styling to this.
7. Drag TypeMajor onto the Size Mark
8. Drag TypeMarjor onto the Color Mark
9. Configure both so the Month is larger and darker than Weekday
Awesome! That's really nice. Now at this point, we can just use this as a calendar (if we wanted to), we can configure the Year to be whatever we wanted, set out Fiscal Month start and whatever day of the Week we run our business on.
But, where this comes in nicely is blending. We can take this and Blend on the Date, so show how various measures are performing on a full calendar view. We could use this for other things as well, such as Holidays.
Example shows blending, along with Proper Dates and values by Fiscal Starts.
Long post, lots of calcs. But I hope this comes in handy. And remember, you can always just download the Workbook, and bring your dataset in, if you don't feel like going through the steps.
A while back, Matt Chambers wrote a really good post about how to Create Hex Maps in Tableau using Hex Shape marks. This was a derivative of the work Brittany Fong did on Tile Maps. I've always enjoyed both of these map alternatives, as it allows users to map geographic data, while preventing the Texas effect.
I have used Matt's technique/data file for a while to create Hex Maps. But one thing that I always struggled with was sizing of the Shape Mark. No matter how hard I tried, it was always difficult to get it "just right". So I decided to put together a new file that allows me to create a Polygon Hex Map.This has the benefit of reducing headache with Shapes, and how Tableau sizes them based on the the size mark, and the viz size on the Dashboard. It also ensures the placement of the Hex are always exactly in the correct place.
In this post, we are going to talk about how to plot two Ranks, and show the deviations between them. This post will be done using Superstore, and does not require any data modelling.
The goal of this viz is to allow users to compare two measure ranks among dimensions.
Step 1: Setting up the Ranks
The first thing we need to do is create a Parameter that allows the user to determine which measure they want to use as the Primary Rank (The 45 degree line). To do this, we want to first setup a simple Parameter.
Once we have our Parameter, we want to create a calculation to grab the rank of the measure selected.
[Primary Rank]
IF [Choose Primary Rank] = 'Sales'
THEN RANK(SUM([Sales]))
ELSE RANK(SUM([Profit]))
END
This calculation is simply switching between the Rank of Sales or Profit, based on the user selection.
Now that we have our Primary Rank, we need to find the Secondary Rank, or the rank of the measure we want to plot the deviation to.
[Secondary Rank]
IF [Choose Primary Rank] = 'Sales'
THEN RANK(SUM([Profit]))
ELSE RANK(SUM([Sales]))
END
This is the inverse of the Primary Rank, we simply grab the rank of whatever measure the user did not select.
Step 2: Plotting
Now that we have our calculation, the next step is to start plotting them.
We begin by placing our Primary Rank on the Columns Shelf, and Sub-Category (or whatever dimension you want to compare ranks on) onto the detail shelf. Set the Primary Rank to compute using that dimension.
Perfect, now we want to repeat this same process, placing the Primary Rank again on the Rows Shelf. This will create our 45 degree line of the Primary Rank, showing where those fall.
We also want to Reverse the Column Axis so that is starts at our lowest rank, and moves to the highest (1).
Well at this point, all we have is a line...
The next trick is to drag the Secondary Rank onto the Axis of the Row Shelf. The will generate the Measure Values/Names onto Tableau.
Once that is generated, Set the Secondary Rank to compute using the Dimension, in this case, Sub-Category. Also, reverse the Axis on the Rows shelf (same as column) so that 1 (our highest rank) is at the top.
Perfect, now that we have the bones of the Plot, we just need to do some cleanup.
Step 3: Cleanup
First thing we want to do is remove Measure Names from the Color Mark, and place it instead on the detail shelf. We are going to create another calculation to determine the color.
Now onto the color. We want a calculation that will auto-magically give us the difference between the Primary and Secondary Rank, so that we can better highlight the deviations.
[Color]
[PrimaryRank] - [Secondary Rank]
By taking the difference from the Primary and Secondary Rank, we will always get the Increasing/Decreasing value based on the user's Parameter Selection, as both of these calcs are determined by it.
Place that onto the Color Mark, compute using the Dimension (Sub-Category).
You can change color pallets if you wish, but using a Diverging palette, be sure to set the Center to 0, so that increase/decrease deviations have two different color.
The last thing we need to do is handle how to tell the user what is what. Since Axis Titles are not Data-Driven, we cannot change those at will. So instead we will rename our Axis to be Primary and Secondary Rank.
Then we will create a calculation we can place in the title that will inform our users on which is which.
[Title]
IF [Choose Primary Rank] = 'Sales'
THEN 'Primary Rank: Sales | Secondary Rank: Profit'
There are many different ways you could do this, but I wrapped my into a calc for ease of use.
Place that on the Detail Mark, then simple add it to the Title.
And that's it. You can adjust this in whatever way you need for your data. You can even expand this by having multiple measures, and letting the user select both the Primary and Secondary Rank using two parameters.