Sort by Calculated Field in Tableau

While working on the product sales analysis, I created a report using Tableau. In this report, I also wanted to sort the records in increasing or decreasing order. The issue came when I tried to sort the calculated field using the sort button.

In Tableau, it is easy to sort the fields like Profit and Sales, but we cannot directly sort the calculations like Profit Sales ratio {SUM([Profit]) / SUM([Sales])}.

In this Tableau tutorial, I will explain how to sort values for a calculated field in Tableau for the table and non-table calculations.

Sort Values by Calculated Fields in Tableau

In Tableau, there are three ways to implement sorting for the calculated field values, which are as follows.

  • Standard sort for non-table calculations
  • Sorting with table calculations
  • Dynamic sorting of calculated fields with a parameter

To show the sorting in the calculated fields, we will use the Tableau Superstore dataset. Now, connect the Tableau with the datasource and follow the methods below.

Method-1: Standard Sort for Non-Table Calculations in Tableau

In this method, we will see the sorting for the calculations that do not include any table calculations like RANK(), INDEX() and WINDOW_SUM().

In this sorting, we can sort the values for non-table calculations like Profit Ratio or Average Discount. In this example, we will sort the Sales for the Sub-Category according to the profit ratio.

  1. To create the view, drag the Sub-Category to the Rows and Sales to the Columns.
Sort Calculated field values in Tableau
  1. Create a calculated field “Profit Margin” using the formula below. Based on this field, we will sort the sales for the sub-category.
SUM([Profit])/SUM([Sales])
  1. For sorting, click on the Sub-Category dimension dropdown and select the Sort option.
Sort Values by calculation in Tableau
  1. In the sort setup, select Sort By as Field, and then select the Sort Order. Then, in the Field Name, select the calculated field by which we need to sort values.
Sort Calculated Field in Tableau

Now, we can see the sales values sorted by the calculated field Profit Ratio for the Sub-Category dimension.