Amazon QuickSight – Replace
The replace function in Amazon QuickSight replaces all occurrences of a substring within a string expression with a specified replacement string.
Syntax
#Start# replace(expression, substring, replacement) #End#
This function takes three arguments:
- expression (required): the string expression that contains the substring to replace.
- substring (required): the substring to search for and replace.
- replacement (required): the string to replace each occurrence of the substring.
Suppose you have a dataset containing a field with a string expression that contains a specific substring that you want to replace with another string.
Assume the following dataset:
| Item | Description |
| Apple | Fresh apples |
| Banana | Ripe bananas |
| Orange | Sweet oranges |
| Pear | Juicy pears |
To replace the word “apples” with “fruit” in the Description field, you can use the replace function:
- Select the dataset that you want to create a calculated field for.
- Click on the “Add field” button.
- Enter the following expression:
Example
#Start# replace({Description}, "apples", "fruit") #End#
This expression replaces all occurrences of the substring “apples” in the Description field with the string “fruit.” The resulting calculated field will contain the modified string expressions.
- Click “Create field” to create the calculated field.
After creating this calculated field, you can use the modified string expressions for visualizations and analysis. In this example, the calculated field would have the following values:
| Item | Description | Modified Description |
| Apple | Fresh apples | Fresh fruit |
| Banana | Ripe bananas | Ripe bananas |
| Orange | Sweet oranges | Sweet oranges |
| Pear | Juicy pears | Juicy pears |
Note that the “Modified Description” column is a calculated field, and the values are not stored in the original dataset. Also note that the replace function is case-sensitive, so it will only replace the substring with the exact same case.