Excel has huge number of functions, but there's one unsung hero that often flies under the radar: the SUBSTITUTE function. SUBSTITUTE function is amazing which lets you streamline your data manipulation tasks.
SUBSTITUTE function substitutes one piece of text for another within a larger text string. Its syntax is straightforward:
=SUBSTITUTE(text, old_text, new_text, [instance_num])
Here's a breakdown of each argument:
- `text`: This is the original text string where you want to perform substitutions.
- `old_text`: This is the text you want to replace.
- `new_text`: This is the text you want to replace `old_text` with.
- `[instance_num]` (optional): Specifies which occurrence of `old_text` you want to replace. If omitted, all occurrences are replaced.
If there are multiple values that you want to replace in excel cells then you can create nested SUBSTITUTE queries which we have covered in our video tutorial here.
Here are some use-cases of the SUBSTITUTE function:
1. Cleaning Data : When dealing with messy datasets, it's common to encounter inconsistencies or errors in text. SUBSTITUTE comes in handy for correcting these issues. For example, if you have a list of product names where "iPhone" is misspelled as "iPhon," you can use SUBSTITUTE to correct it uniformly across the cells in your Excel sheet.
2. Formatting: SUBSTITUTE isn't just for correcting mistakes; it's also useful for formatting text. Say you have a dataset with phone numbers formatted inconsistently (e.g., "(123) 456-7890" and "123-456-7890"). You can use SUBSTITUTE to standardize the format, making it easier to analyze or import into other systems.
3. Replacing Characters: Need to remove certain characters from a text string? SUBSTITUTE can handle that too. Whether it's stripping out excess spaces, removing punctuation, or replacing special characters, SUBSTITUTE gives you the flexibility to tailor text strings to your needs.
4. Conditional Substitutions: With the optional `instance_num` argument, you can specify which occurrence of `old_text` to replace. This allows for more granular control over substitutions. For instance, if you only want to replace the second occurrence of a word within a text string, SUBSTITUTE makes it possible.
5. Dynamic Text Generation: By combining SUBSTITUTE with other Excel functions like CONCATENATE or TEXTJOIN, you can dynamically generate text strings based on specific criteria. This is particularly useful for creating custom reports or formatting data for presentation.
Overall, SUBSTITUTE function can be a real time saver for you as it makes it easier for you to clean the messy datasets without the knowledge of coding in SQL, Python languages.