Comparing one to everything else

In this recipe, we will create a bar chart that shows how much more or less other athletes earned compared to a selected athlete.

Comparing one to everything else

Getting ready

To follow this recipe, open B05527_03 – STARTER.twbx. Use the worksheet called Compare one against everything else and connect to the Top Athlete Salaries (Global Sport Finances) data source.

Getting ready

How to do it...

The following are the steps to create a chart that allows a selected item to be compared with all other items in the view:

  1. Right-click the arrow beside the Dimensions section in the sidebar, and select Create Parameter.
  2. Create a string parameter called Compare salary with the following settings:
    How to do it...
  3. Click on the Add from Field button, choose the data source Top Athlete Salaries (Global Sport Finances), and then select Athlete.
    How to do it...
  4. Click on OK when done.
  5. Show the parameter control for Compare salary with. You can do this by right-clicking on the parameter, and selecting Show Parameter Control.
  6. Create a calculated field called Selected Athlete Salary, which uses the following LOD (Level of Detail) expression:
    How to do it...
  7. Create another calculated field called Difference in Salary, which uses the following formula:
    How to do it...
  8. Create one more calculated field called Message, which determines what string message to display depending on the calculated salary difference. The formula is as follows:
    How to do it...
  9. From Dimensions, drag Athlete to the Rows shelf.
  10. From Measures, drag Salary/Winnings $ to the Rows shelf, to the right of Athlete.
  11. Right-click on the SUM(Salary/Winnings $) pill in the Rows shelf, and select Discrete to show this as a text column beside Athlete.
  12. From Measures, drag Message to the Rows shelf, to the right of SUM(Salary/Winnings $).
  13. From Measures, drag Difference in Salary to the Columns shelf. This should produce a bar chart.
  14. This is what you should have now in your Rows and Columns shelves:
    How to do it...
  15. From Measures, drag Difference in Salary to Color in the Marks card.
  16. Right-click on the Athlete pill in the Rows shelf, and select Sort.
  17. Sort the Athlete field in Descending order, using the Difference in Salary field.
    How to do it...
  18. Click OK when done.
  19. Test the parameter. Select different athletes and confirm that the bars and message are adjusted based on the difference between the selected athlete's salary is compared to everyone else in the list.

How it works…

It is not uncommon to want to compare one item against all other items in a chart or viz. This has become easier to achieve in Tableau, thanks to the addition of LOD expressions starting in V9.

In this recipe, we wanted to compare how much more or less athletes are making compared to one athlete we select. This may seem to be a simple calculation, but it quickly becomes tricky if we recall how Tableau works. The grain (or granularity) of measures in Tableau by default depends on the dimensions that are present in the view. For example, the granularity of the following measure SUM(Salary/Winnings $) in the Columns shelf is per Sport.

How it works…

If another dimension is added, this means SUM(Salary/Winnings $) is further sliced. In the following example, it means that the measure is now by Sport and by Athlete.

How it works…

By default, the level of detail presented in a Tableau chart is affected by the data fields used in any of the cards and shelves. However, with LOD expressions, the level of detail can be different from what is in the canvas.

Let us start by attempting to display the salary differences between the selected athlete in the Compare salary with parameter and the rest of the athletes using a simple SUM formula:

How it works…

When we select Kobe Bryant, we can see that we are not able to display his salary side by side with other athletes using the non-LOD calculated field. His salary appears to be zero for everyone else but him.

How it works…

This is problematic because this will give us incorrect salary differences.

However, using a FIXED LOD expressed, we can keep this number static. We can use the previous formula and just enclose it in curly braces.

How it works…

The syntax of LOD expressions is:

{[FIXED | INCLUDE | EXCLUDE] <dimension declaration > : <aggregate expression>}

The FIXED keyword can be omitted if the LOD expression is not being fixed to any specific dimensions. When we display this in our view, we can see that Kobe Bryant's salary is persisted for each row.

How it works…

Calculating the difference is now easier, because the calculated field with LOD expression maintains the chosen athlete's salary.

How it works…

You are probably wondering why we are enclosing the Selected Athlete Salary calculated field in SUM when it is already a SUM. The reason is because as far as Tableau is concerned, an LOD expression is still a non-aggregated field. If we tried to use this without an aggregation and subtract this from an aggregated expression, we would get the classic error on mixing aggregate and non-aggregate fields:

How it works…

The good thing is we simply need to enclose the field with the LOD expression in an aggregated function, like a SUM, MIN, or MAX. The SUM, MIN, or MAX of one value is still that value, so it will not affect the calculation and it will allow us get around the mixed aggregation error.

One more thing we did in this recipe is create another calculated field for a text message we are displaying as a column. This calculated field, called Message, uses several IF…ELSEIF statements to see if the salary difference is more, less, or equal, and composes the appropriate string message to be displayed:

How it works…

There's more...

LOD expressions were introduced in Tableau V9—one of most anticipated, if not the most anticipated, features of this release.

LOD expressions are very powerful, and allow us to simplify what used to be complicated calculations from before their introduction. The problem we solved in this recipe might have required a blend, or a subquery at the data source, had we not had access to LODs.

The syntax for LOD expressions is as follows:

{[FIXED | INCLUDE | EXCLUDE] <dimension declaration > : <aggregate expression>}

While powerful, one of the limitations is that the dimension declaration must be a persistent field. We can definitely get around this, but this requires creating additional fields that may be used only within the LOD expression.

Tableau V10 now allows expressions to be used instead of dimension field names. For example, if you wanted to fix sales per year, you can simply use the YEAR function in your LOD expression:

{ FIXED YEAR([Order Date]):SUM([Sales])}

There are many scenarios where LOD expressions can prove to be useful and efficient. If you're new to LOD expressions, or looking to understand them better, I encourage you to check out the following resources:

See also

  • Please refer to the recipe in Appendix A, Calculated Fields Primer
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.212