Creating a Parameter Control to Sort By Dimension
When you try to sort based on a dimension , Tableau sorts the values of that dimension within the categories specified to its left in the shelf. For example, if you had two rows with the same value for [Number] and [Date Generated], but with different values for [Importance], clicking on the "Sort" icon over [Importance] in the table would re-arrange the two rows either ascending alphabetically or descending alphabetically by the value of [Importance], but without changing the order of the numbers or dates.The below steps are based on Sample - Superstore data.
Step 1: Create a parameter
- Right-click in the Data pane and select Create Parameter.
- In the Create Parameter dialog, do the following, then click OK:
- Name: Sort By
- Data Type: String
- Allowable Values: List
- For Value, enter the following list: Category, Sub-Category, Product Name and Region.
- Right-click the Sort By parameter and select Show Parameter Control.
Step 2: Create a calculated field
- Select Analysis > Create Calculated Field.
- Name the field Sort by Dimension, enter the following formula, then click OK:
CASE [Sort By]
WHEN 'Category' THEN [Category]
WHEN 'Sub-Category' THEN [Sub-Category]
WHEN 'Product Name' THEN [Product Name]
WHEN 'Region' THEN [Region]
END
Step 3: Build your view
- Drag Product Name, Sub-Category, Category, and Region to Rows.
- Drag Sales to Columns.
- Drag Sort by Dimension to Rows, placing it to the left of Product Name.
- Right-click Sort By Dimension on Rows, and select Sort. Choose the following options, then click OK:
- Sort order: Ascending
- Sort by: Alphabetic
- Right click Sort by Dimension on Rows, then un-check Show Header.
If you want to mix data types or add an 'Ascending or Descending' parameter to the sort, a RANK() can be added to any non-number fields. See the attached "Sorting by ANYTHING" workbook.
Use a calculation similar to the following instead of the calculation in the above instructions:
IF [Ascending/Descending]='Ascending' then 1 else -1 END
*
IF [Sort by]='Category' then -rank(min([Category]))
elseif [Sort by]='Sub-Category' then -rank(min([Sub-Category]))
elseif [Sort by]='Sales' then SUM([Sales])
elseif [Sort by]='Profit' then SUM([Profit])
END
RANK() can be used on any data type to convert it to a number and retain the original order. For 'Descending', the entire calculation can be multiplied by -1.
Use a calculation similar to the following instead of the calculation in the above instructions:
IF [Ascending/Descending]='Ascending' then 1 else -1 END
*
IF [Sort by]='Category' then -rank(min([Category]))
elseif [Sort by]='Sub-Category' then -rank(min([Sub-Category]))
elseif [Sort by]='Sales' then SUM([Sales])
elseif [Sort by]='Profit' then SUM([Profit])
END
RANK() can be used on any data type to convert it to a number and retain the original order. For 'Descending', the entire calculation can be multiplied by -1.
Comments
Post a Comment