You are learning Power Query in MS Excel
How to extract specific parts from text strings using functions in Power Query?
In Power Query, you can extract specific parts from text strings using various functions that manipulate text values. These functions allow you to split, find, replace, and extract substrings based on specific patterns or positions within your text data. Here’s how you can extract specific parts from text strings using Power Query:
Extracting Specific Parts from Text Strings
1. Using Text Functions
- Text.Middle: Extracts a substring from the middle of a text value, given a starting position and length.
```m
Text.Middle(TextColumn, start, length)
```
Example:
```m
Text.Middle("Hello World", 3, 5) // Outputs "llo W"
```
- Text.Start: Extracts a specified number of characters from the start of a text value.
```m
Text.Start(TextColumn, numberOfCharacters)
```
Example:
```m
Text.Start("Hello World", 5) // Outputs "Hello"
```
- Text.End: Extracts a specified number of characters from the end of a text value.
```m
Text.End(TextColumn, numberOfCharacters)
```
Example:
```m
Text.End("Hello World", 5) // Outputs "World"
```
- Text.BeforeDelimiter and Text.AfterDelimiter: Extracts text before or after a specified delimiter.
```m
Text.BeforeDelimiter(TextColumn, delimiter)
Text.AfterDelimiter(TextColumn, delimiter)
```
Example:
```m
Text.BeforeDelimiter("John-Doe", "-") // Outputs "John"
Text.AfterDelimiter("John-Doe", "-") // Outputs "Doe"
```
2. Using Custom Functions
You can also create custom functions in Power Query using the Advanced Editor or the formula bar to perform more complex text extraction operations. Here’s a basic example:
- Custom Function: Extracts the first word from a text string.
```m
(text as text) => Text.BeforeDelimiter(text, " ")
```
- Define this function under `Home` > `Advanced Editor` and create a new function using the `#shared` variable.
3. Example: Extracting Parts from Text
Suppose you have a dataset `CustomerData` with a column `FullName` containing full names in the format "First Name Last Name". To extract the first and last names separately:
1. Open Power Query Editor:
- Select `Home` > `Transform Data` to open the Power Query Editor.
2. Extracting First Name and Last Name:
- First Name:
```m
#"First Name" = Text.BeforeDelimiter(CustomerData[FullName], " ")
```
- Last Name:
```m
#"Last Name" = Text.AfterDelimiter(CustomerData[FullName], " ")
```
3. Load Data:
- After applying transformations, click `Close & Load` to load the modified data back into Power BI or Excel.
Additional Tips:
- Handling Text Cases: Use `Text.Upper`, `Text.Lower`, and `Text.Proper` functions to convert text cases as needed.
- Trimming Whitespace: Remove leading or trailing spaces from text values using `Text.Trim`.
- Error Handling: Ensure your text manipulation functions account for potential errors, such as missing delimiters or unexpected text formats.
By utilizing these text extraction functions and techniques in Power Query, you can effectively parse and extract specific parts from text strings, facilitating data cleansing, transformation, and analysis tasks within your datasets.