Meeting business requirements / Revenue Per Guest
Last updated
Last updated
Sometimes, data doesn’t exactly match what your organization wants. For example, the measures in the data may not be the exact metrics required to make key business decisions. Or dimension values may need to be grouped according to a certain set of rules. Although this kind of business logic is often applied as data is transformed or modeled prior to connecting to it with Tableau, you may find cases where you need to implement business logic on the fly.
In this example, consider that the measure Rent
is simply the base rent and does not include the discount or taxes. Those are separate fields in the data. If you needed to analyze the total Revenue
, you’d need to calculate it. That calculation might look something like this:
This formula takes the base Rent
, subtracts the Discount
, and then adds the Tax per Night
multiplied by the Nights Rented
. The parentheses aid readability but are not required because the multiplication operator, *
, has higher precedence and is evaluated before the addition, +
.
Up until now, we’ve created calculated fields that extend the data source. Sometimes, you just need a quick calculation to help in a single view. We’ll conclude by looking at these quick ad hoc calculations.
Ad hoc calculations allow you to add calculated fields to shelves in a single view without adding fields to the data pane.
Let’s say you have a simple view that shows the Revenue per Guest, like this:
Figure 4.21: The revenue generated from each individual’s stay
What if you wanted to quickly highlight any renters who had paid less than $1,500? One option would be to create an ad hoc calculation. To do so, simply double-click on an empty area of the Columns, Rows, or Measure Values cards, or on the empty space of the Marks shelf, and then start typing the code for a calculation. In this example, we’ve double-clicked the empty space on the Marks shelf:
Figure 4.22: Creating an ad hoc calculation on the Marks card
Here, we’ve entered code that will return True
if the sum of Rent
is less than $1,500, and False
otherwise. Pressing Enter or clicking outside the text box will reveal a new ad hoc field that can be dragged and dropped anywhere within the view. Here, we’ve moved it to the Color shelf:
Figure 4.23: Using the ad hoc calculation on Color
The ad hoc field is only available within the view and does not show up in the data pane. You can double-click the field to edit the code.
Dragging and dropping an ad hoc field into the data pane transforms it into a regular calculated field that will be available for other views that are using that data source.
Having seen a number of practical examples, let’s conclude by considering some ways to ensure good performance when using calculations.