Sort by Calculated Field in Tableau : Bijay Kumar

Sort by Calculated Field in Tableau
by: Bijay Kumar
blow post content copied from  SalesForce FAQs
click here to view original post



### Summary of Sorting Calculated Fields in Tableau In this tutorial, we explore how to sort calculated fields in Tableau, which can be tricky compared to sorting standard fields like Profit and Sales. The tutorial outlines three main methods for sorting calculated fields: 1. **Standard Sort for Non-Table Calculations**: - You can sort fields that don’t use table calculations (like RANK() or INDEX()). For example, to sort Sales by Profit Ratio, create a calculated field for Profit Margin using the formula `SUM([Profit])/SUM([Sales])`, then use the Sort option in the Sub-Category dropdown. 2. **Sorting with Table Calculations**: - For calculations that use table functions (like WINDOW_SUM()), you can’t directly sort them. Instead, create a calculated field that represents each row's percentage of total sales. This allows you to sort based on this hidden field. 3. **Dynamic Sorting with Parameters**: - This method lets you change the sorting measure dynamically (between Sales, Profit, and Discount) using a parameter. Create a parameter named "Sort by Parameter" and use it in a calculated field to adjust the sorting based on the selected measure. ### Additional Context Sorting calculated fields in Tableau is essential for effective data analysis, especially when dealing with complex metrics. Understanding how to manipulate these fields can greatly enhance your ability to derive insights from your data visualizations. The methods described not only improve sorting capabilities but also allow for more interactive dashboards, making your reports more user-friendly. ### Hashtags for SEO #Tableau #DataVisualization #CalculatedFields #DataAnalysis #TableauTutorial #SortingInTableau #BusinessIntelligence #DataScience #TableauTips #Analytics By utilizing these sorting techniques, users can effectively manage and visualize their data in Tableau, leading to better decision-making based on accurate insights.


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.

Apply sorting in Tableau Calculated fields

This way, we can sort the values for a calculated field in Tableau by following the steps above.

Method-2: Apply Sorting For Calculated Fields with Table Calculation in Tableau

In this method, we will sort the calculated fields with quick table calculations like RANK(), INDEX(), and WINDOW_SUM(). In this example, we will create a calculated field using the WINDOW_SUM() table calculation.

In Tableau, we cannot directly sort the calculated field that has table calculations. In Tableau, table calculations are calculated after Tableau does the normal aggregations, so they won’t appear in the standard Sort dialogue.

In this example, we will create a calculated field that will calculate the percentage each row contributes to the whole sales, and then we will use that as a hidden field to control the sorting.

  1. Create a calculated field “% of Sort Order” using the formula below.
SUM([Sales]) / WINDOW_SUM(SUM([Sales]))

In case you want to sort values in ascending order, then use the formula below.

-1* (SUM([Sales]) / WINDOW_SUM(SUM([Sales])))
  1. Now, to create the chart view, add ‘Sub-category‘ to the rows and ‘Sales‘ to the columns.
Sorting by calculated fields in Tableau
  1. Drag the calculated field to the Columns and convert it to a discrete field. For conversion, click on the field in columns and select Discrete.
How to sort calculated fields in Tableau
  1. Move the calculated field to the left of the Sub-Category dimension. After this, we will see the sales of Sub-Category by the order of their share percentage in total sales.
Enable Sorting for calculated field in Tableau
  1. To remove the column of the calculated field, click on the field in the Columns and uncheck Show Header.
Use calculated field for sorting in Tableau

This way, we can use a calculated field as a key to sort the values in Tableau by following this method.

Method-3: Dynamic Sorting of Calculated Fields With a Parameter

In this example, we will implement dynamic sorting for the calculated fields with a parameter. Using this sorting, we can dynamically change the sorting measures such as Sales, Discount and Profit.

Now, follow the steps below to implement dynamic sorting using a calculated field in Tableau.

  1. First, create a parameter through which we can change the dimensions from Sales, Profit and Discount. Now, create the parameter with the details below.
    • Enter the parameter name as Sort by Parameter.
    • Select the Data type as String.
    • In the Allowable values, select List and enter the Values as Sales, Profit and Discount.
    • At last, click on the OK button.
Dynamic sorting for calculated field in Tableau
  1. Using this parameter, create a calculated field with the formula below. This formula will change the sort dimension as we change the value in the parameter.
CASE [Sort by Parameter]  
WHEN "Sales" THEN SUM([Sales])  
WHEN "Profit" THEN SUM([Profit])  
WHEN "Discount" THEN SUM([Discount])  
END
  1. To create the view, add the Sub-Category and calculated field to the Rows. After this, add a measure such as Sales to the Columns.
  1. Now, change the calculated field from Continuous to Discrete.
  1. Shift the calculated field to the left side of the Sub-Category in Columns. To remove the column from the table, click on the calculated field and uncheck ‘Show header‘.
Sort Values using Calculated fields in Tableau
  1. To make the parameter values visible on the worksheet, go to the Data pane, click on the parameter and select Show parameter.
Use calculated field to sort values in Tableau
  1. Now, as we change the values in the parameter, the sort order will display the value for the selected measure.
How to sort calculated fields dynamically in Tableau

This way, we can dynamically sort the values in Tableau using the calculated fields.

Conclusion

In this Tableau tutorial, we have learned how to sort calculated fields in Tableau . In the above example, we have sorted values for non-table calculations like Profit Ratio; we can use the standard sort option.

For table calculations such as % of Total Sales, we created a calculated field and used it as a hidden discrete header to control the sort order. At last, we implemented dynamic sorting with parameters, which allows us to choose whether to sort by Sales, Profit, or Discount. By applying these methods, we can easily handle sorting for both simple and advanced calculations in Tableau.

You may also like to read:

The post Sort by Calculated Field in Tableau appeared first on SalesForce FAQs.


October 06, 2025 at 04:10AM
Click here for more details...

=============================
The original post is available in SalesForce FAQs by Bijay Kumar
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce