- Learning Tableau 2020
- Joshua N. Milligan
- 1081字
- 2021-06-11 18:31:01
Parameters
Before moving to some additional examples of row-level and aggregate calculations, let's take a little side trip to examine parameters, given that they can be used in incredible ways in calculations.
A parameter in Tableau is a placeholder for a single, global value such as a number, date, or string. Parameters may be shown as controls (such as sliders, drop-down lists, or type-in text boxes) to end users of dashboards or views, giving them the ability to change the current value of the parameter. Parameter values may even be changed with actions, as you'll see in Chapter 8, Telling a Data Story with Dashboards.
The value of a parameter is global so that if the value is changed, every view and calculation in the workbook that references the parameter will use the new value. Parameters provide another way to provide rich interactivity to the end users of your dashboards and visualizations.
Parameters can be used to allow anyone interacting with your view or dashboard to dynamically do many things, including the following:
- Alter the results of a calculation
- Change the size of bins
- Change the number of top or bottom items in a top n filter or top n set
- Set the value of a reference line or band
- Change the size of bins
- Pass values to a custom SQL statement that's used in a data source
Some of these are options we'll consider in later chapters.
Since parameters can be used in calculations, and since calculated fields can be used to define any aspect of a visualization (from filters to colors to rows and columns), the change in a parameter value can have dramatic results. We'll see some examples of this in the following sections.
Creating parameters
Creating a parameter is similar to creating a calculated field.
There are multiple ways to create a parameter in Tableau:
- Use the drop-down menu next to Dimensions in the data pane and select Create Parameter.
- Right-click an empty area in the data pane and select Create Parameter.
- Use the drop-down menu on a field, set, or parameter already in the data pane and select Create | Parameter....
In the last case, Tableau will create a parameter with a list of potential values based on the domain (distinct values) of the field. For fields in the data pane that are discrete (blue) by default, Tableau will create a parameter with a list of values matching the discrete values of the field. For fields in the data pane that are continuous (green), Tableau will create a parameter with a range set to the minimum and maximum values of the field that's present in the data.
When you first create a parameter (or subsequently edit an existing parameter), Tableau will present an interface like this:
Figure 4.13: The Create Parameter interface numbered with corresponding descriptions below
The interface contains the following features:
- Name will show as the default title for parameter controls and will also be the reference in calculations. You can also add a Comment to describe the use of the parameter.
- Data type defines what type of data is allowed for the value of the parameter. Options include integer, float (floating-point decimal), string, Boolean, date, or date with time.
- Current value defines what the initial default value of the parameter will be. Changing this value on this screen or on a dashboard or visualization where the parameter control is shown will change the current value.
- Value when workbook opens allows you to optionally change the default value of the parameter when the workbook opens based on a calculated value.
- Display format defines how the values will be displayed. For example, you might want to show an integer value as a dollar amount, a decimal as a percentage, or display a date in a specific format.
- The Allowable values option gives us the ability to restrict the scope of values that are permissible. There are three options for Allowable values:
- All allows any input from the user that matches the data type of the parameter.
- List allows us to define a list of values from which the user must select a single option. The list can be entered manually, pasted from the clipboard, or loaded from a dimension of the same data type.
- Range allows us to define a range of possible values, including an optional upper and lower limit, as well as a step size. This can also be set from a field or another parameter.
- In the example of the preceding screenshot, since we've selected List for Allowable values, we are given options to enter the list of possible values. In this example, a list of three items has been entered. Notice that the value must match the data type, but the display value can be any string value. You can drag and drop values in the list to reorder the list. If Range had been selected, the screen would instead show options for setting the Minimum, Maximum, and Step Size for the range.
- Also specific to List are a couple of additional options for populating the list:
- Fixed: You may manually enter the values, paste from the clipboard, or set them from the existing values of a field in the data. In any case, the list will be a static list and will not change even if the data is updated.
- When the workbook opens allows you to specify a field that will dynamically update the list based on the available values for that field when the workbook is first opened.
Click OK to save changes to the parameter or Cancel to revert.
When the parameter is created, it appears in the data pane in the Parameters section. The drop-down menu for a parameter reveals an option, Show Parameter Control, which adds the parameter control to the view. The little drop-down caret in the upper right of the parameter control reveals a menu for customizing the appearance and behavior of the parameter control. Here is the parameter control, shown as a single value list, for the parameter we created earlier:
Figure 4.14: The parameter control shown as a single select radio button list
This control can be shown on any sheet or dashboard and allows the end user to select a single value. When the value is changed, any calculations, filters, sets, or bins that use the parameter will be re-evaluated, and any views that are affected will be redrawn.
Next, we'll consider some practical examples that use parameters in calculations.