Example: Swap Measures Using Parameters

You may want to create a view that shows how the values of two measures compare against each other. But what if you also wanted to be able to choose which measures were being compared—or better yet, add a control to the view that would let any user select the measures to be compared. You can create such a view using parameters and calculated fields. The calculated fields replace the measures in the view and can be set interactively by the user with parameter controls or parameter actions. Using swap measures also allows you to use Dynamic Axis Titles.

General steps

  1. Create the parameters.

  2. Create calculated fields to change the measures in the view.

  3. Set up the view.

This article includes an additional section that you can use as an alternative to step 2:

  • Create calculated fields to change measures and specify aggregations

The following sections break these procedures down into specific instructions.

The scenario uses the Sample - Superstore data source provided with Tableau Desktop.

Create the parameters

Follow these steps.

  1. In the Data pane, click the drop-down arrow in the upper right corner and select Create Parameter.

  2. In the Create Parameter dialog box, do the following:

    1. Name the parameter Placeholder 1 Selector.

    2. Set the Data type to String.

    3. Skip down to the Allowable values field, and choose List.

    4. Type individual measure names in the List of values area: Discount, Profit, Quantity, and Sales.

      The Create Parameter dialog box should now look like this:

      The Create Parameter dialog

    5. Click OK to close the Edit Parameter dialog box.
  3. Create a second parameter, Placeholder 2 Selector, with the exact same configuration.

    There are different ways you can do this. The easiest way is to click Placeholder 1 Selector in the Data pane, choose Duplicate, and then change the name of the duplicated parameter to Placeholder 2 Selector.

Create calculated fields to change the measures in the view

Follow these steps.

  1. Choose Analysis > Create Calculated Field to open the calculation editor. Name the calculation Placeholder 1 and type or paste the following in the formula area:

    CASE [Placeholder 1 Selector]
    WHEN "Discount" THEN [Discount]
    WHEN "Profit" THEN [Profit]
    WHEN "Quantity" THEN [Quantity]
    WHEN "Sales" THEN [Sales]
    END

    Note: The calculated field must reference the Value entry for a given row, and not the Display As value.

  2. Click OK to exit the calculation editor.

  3. Create a second calculated field, Placeholder 2, with the same definition. Again, the easiest way to do this is to click Placeholder 1 in the Data pane, choose Duplicate, and then change the name of the duplicated field to Placeholder 2 . Then replace Placeholder 1 selector with Placeholder 2.

Set up the view

Follow these steps.

  1. Drag Placeholder 2 to Columns and Placeholder 1 to Rows.

    Because you dragged measures to both shelves, the default view is a scatter plot. For more on why Tableau does this, see Example: Scatter Plots, Aggregation, and Granularity.

  2. Drag Customer Name to Detail and Region to Color.

  3. In the Parameters area of the Data pane, click each Placeholder 1 Selector and choose Show Parameter Control. Then do the same for Placeholder 2 Selector.

  4. Tableau displays the parameter controls beyond the right side of the view by default. Drag them over to the left side to make them easier for your users to see.

Your view is now complete. The parameter controls let users select the measures to be used on the X and Y axes. For example, the view below on the left shows Quantity vs. Discount, while in the view the right the parameter controls have been used to show Profit vs. Sales.

Create calculated fields to change measures and specify aggregations

As an alternative to the Create calculated fields to change the measures in the view section, above, consider creating calculated fields that specify aggregations for individual measures. As written above, the calculated fields do not specify aggregations. Notice in the image above that Tableau automatically assigns an aggregation (SUM) to the Placeholder 1 and Placeholder 2 fields. But you know your data, and you may want to dictate which aggregation Tableau uses for your measures. So instead of the calculated field definition above, consider a definition like the following:

CASE [Placeholder 1 Selector]
WHEN "Discount" THEN SUM([Discount])
WHEN "Profit" THEN AVG([Profit])
WHEN "Quantity" THEN SUM([Quantity])
WHEN "Sales" THEN AVG([Sales])
END

It's up to you to decide whether to explicitly aggregate measures in your field definitions. The only thing you cannot do is mix-and-match: that is, you cannot define aggregations for some measures, but not for others.

Here's how the Profit vs. Sales scatter plot changes when you specify AVG as the aggregation for these fields, as opposed to not specifying an aggregation and letting Tableau default to SUM:

Profit vs. Sales with default aggregation Profit vs. Sales with explicit aggregation

Similar, but different.

Thanks for your feedback!Your feedback has been successfully submitted. Thank you!