This Tableau article explains using the string parameter to work on dimensions, including displaying a particular country’s sales, performing wildcard searches, and multidimensional wildcard searches with an example.
To demonstrate the Tableau String Parameter values, we used the Dimproduct, DimGeography, and FactInternetSales tables in the AdventureWorksDW database. Please visit the SQL Source and SQL Tutorial article to understand the Data Source.
As there is less data for the blue, Grey, Multi, NA, Silver/Black, and White color products, we added them to the Colors group. Next, we need a Tableau parameter to store the string values and a calculated field or filter shelf to write the expression for utilizing or calling parameter values inside a report to fulfill this requirement.
Tableau String Parameter Example
Drag and drop the Color group to the Columns shelf, and the Sales Amount Measure to the Rows Shelf. Please change the Marks type to Bar for the Tableau String Parameter demonstration. Next, add the State Province Name to the Color Shelf to create a Stacked bar Chart. To add data labels to it, add Sales Amount to the Label Shelf. For the remaining charts in Tableau, please click here.
In this Tableau example, our goal is to demonstrate the full potential of the string parameter. So, right-click the empty space below the # Measure Values and choose the Create Parameter option from the context menu.
It will open the following window. Please change the name to Country Param, change the data type to string, and set the Allowable values to List. You can manually type the list of items, click the down arrow beside the Add value from the button, choose the Data Source, and then select the Field name. For demonstration, let me choose the English Country Region Name.
We need a calculated field to utilize this Tableau string parameter in the report. So, click the down arrow (red marked) and select the Create Calculated Field.. option to write the below expression.
The following Choose Country Calculated Field expression uses the IF ELSE statement. If the user selects the parameter value and that value exists in the English Country Region Name, Show. Otherwise, don’t. For all the built-in functions >> Click Here!
IF [English Country Region Name] = [Country Param] THEN 'Show' ELSE 'Dont' END
Please add the newly created Choose Country Calculated Field to the Filters Shelf. When you drag to the Filters Shelf, the following window will open to choose the option. Please checkmark the Show option and click OK.
Next, Click the down arrow beside the Country Param and select the Show Parameter option to see them. Let me choose Australia as the Tableau string Parameter value and see the stacked bar chart changes. It shows the sum of sales by color group belonging to Australia.
Within the report, click the down arrow beside the Country Param, and by default, it will display the Compact List.
- Single Value List: It displays all the values with radio buttons. Using those radio buttons, you can select the required parameter value.
- Compact List: Drop-down list of string parameter values.
- Slider: Use the slider to drag forward and backward to select the item.
- Type In: Manually type the country name. When you type the first letter, it automatically picks up the related term (autofill option).
For the Tableau String Parameter demonstration purpose, let me choose the Single Value List.
Please select the United States to see the sales of the product color belonging to the USA.
In Tableau, we don’t need a Calculated Field to use the string parameter. You can try the Filters action to get the same result.
Let me remove the Calculated Field and add the English Country Region Name to the Filters Shelf to show you the same. It will open the following pop-up window. Please navigate to the Condition tab and write the expression below with the Formula section. It is the same that we used in the calculated field.
[English Country Region Name] = [Country Param]
Now, you see, we have selected France as the string parameter value, and the Tableau report shows the sales of that particular Country.
Click the down arrow beside the [English Country Region Name] on the Filter shelf and choose the Edit filter option. Instead of displaying the matching country sales in the Tableau report, display all the Countries with less than or equal to the string parameter text. Let me add the Country Name to the Label to show the same.
[English Country Region Name] <= [Country Param]
The right-side (stacked bar chart) shows the result: countries less than or equal to France (alphabets less than or equal to F).
Let me try one more time, and this time, sales of the countries greater than or equal to Canada.
[English Country Region Name] >= [Country Param]
Wildcard Search using the Tableau String parameter
In Tableau, you can use the built-in CONTAINS function and the string parameter to perform a wildcard search. To demonstrate the same, click the down arrow beside the Country Param, change the allowable values from List to All, and add any letter/character as the current value.
Next, edit the Choose Country Calculated Field and write the below expression. It checks whether the substring of the parameter value is present in the country column (dimension). If yes, it returns boolean True; Otherwise, it returns false.
CONTAINS([English Country Region Name], [Country Param])
Next, add it to the Filters shelf and choose True on the pop-up window. If you type Un, the report shows all the product color sales belong to the United States and the United Kingdom.
Multidimensional Wildcard Search
In Tableau, you can use the same string parameter to work with multiple dimensions. For instance, you can search one or more fields or dimensions using a single parameter. The following expression searches the user-entered parameter substring inside the Country, Color, and State dimensions (table columns). If there is any matching, it displays the result.
CONTAINS([English Country Region Name], [Country Param]) OR CONTAINS([Color (group)], [Country Param]) OR CONTAINS([State Province Name], [Country Param])
Let me type Mi, as there is no matching in Country and Color dimensions; it looks for the state-provided Name and returns the result for the four states containing Mi.
If you type BI as the string parameter value, Tableau displays the states containing the BI at any index position.
When you type ra, there is a match in Country, Australia, and France.