You are learning Power Query in MS Excel
How to filter and transform data based on text patterns or regular expressions in Power Query?
Power Query doesn't have a built-in function for regular expressions, but there are a few workarounds to filter and transform data based on text patterns:
1. Text Functions:
* Text.Contains: This function checks if a text string contains another substring. You can use it for simple pattern matching.
```M
let source = Table.FromList({"Apple", "Banana", "Cherry Pie"}),
filtered = Table.SelectRows(source, each Text.Contains([Column1], "e")) // Filters rows with "e"
in filtered
```
* Text.StartsWith & Text.EndsWith: Similar to Contains, these functions check if a text string starts or ends with a specific pattern.
2. List.AnyTrue:
This function iterates through a list and returns true if any condition within the list evaluates to true. It can be combined with Text.Split to check for multiple patterns within a cell.
```M
let source = Table.FromList({"Apple Pie", "Orange Juice", "Plain Yogurt"}),
filtered = Table.SelectRows(source, each List.AnyTrue({Text.StartsWith([Column1], "App"), Text.EndsWith([Column1], "ice")})) // Filters rows starting with "App" or ending with "ice"
in filtered
```
3. Custom Columns with Conditional Logic:
You can create a new custom column with logic based on text patterns.
```M
let source = Table.FromList({"Apple", "Banana", "Cherry"}),
filtered = Table.AddColumn(source, "IsFruit", each if Text.Contains([Column1], "e"), "Yes", "No"),
filtered = Table.SelectRows(filtered, each [IsFruit] = "Yes") // Filters rows with "e" based on the new column
in filtered
```
4. Advanced - Splitting and Combining Text:
For complex text manipulation, you can split text into parts based on delimiters and then use conditional logic to filter or transform the data.
Remember: These methods don't offer the full power of regular expressions. However, they can handle many common text manipulation tasks in Power Query.
Additional Considerations:
* Case Sensitivity: By default, Power Query text functions are case-sensitive. Use Text.Lower or Text.Upper to ensure consistent matching.
* Wildcards: You can use wildcard characters like "*" to represent any combination of characters in Text.Contains.
For more complex text manipulation scenarios, consider using third-party Power Query functions that might offer regular expression capabilities. However, ensure these functions are compatible with your Power Query environment and security policies.