To achieve your goal in Power Query, you can follow these detailed steps:
Step 1: Load Your Data into Power Query
Assuming your data is in an Excel table or imported into Power BI, load it into Power Query Editor.
Step 2: Group the Data
Go to theHometab and click onGroup By.
In , set the following:Group by: SelectObjectandDependency Text.New column name: EnterGroupedData.Operation: ChooseAll Rows.
Group by: SelectObjectandDependency Text.
New column name: EnterGroupedData.
Operation: ChooseAll Rows.
ClickOK.
Go to theHometab and click onGroup By.
In , set the following:
Group by: SelectObjectandDependency Text.
New column name: EnterGroupedData.
Operation: ChooseAll Rows.
Group by: SelectObjectandDependency Text.
New column name: EnterGroupedData.
Operation: ChooseAll Rows.
ClickOK.
This will group your data by unique combinations ofObjectandDependency Text, and theGroupedDatacolumn will contain a table of the grouped rows.
Step 3: Create a Custom Function
Go toHome>New Source>Blank Query.
In the formula bar, paste the following code:powerqueryCopy codelet
ReplaceValues = (TextInput as text, ReplacementTable as table) =>
List.Accumulate(
Table.ToRecords(ReplacementTable),
TextInput,
(currentText, record) => Text.Replace(currentText, record[Value], record[Formatted Value])
)
in
ReplaceValues
Rename this query .
Go toHome>New Source>Blank Query.
In the formula bar, paste the following code:
────────────────────────────────────────────────────────────────────
powerqueryCopy codelet
ReplaceValues = (TextInput as text, ReplacementTable as table) =>
List.Accumulate(
Table.ToRecords(ReplacementTable),
TextInput,
(currentText, record) => Text.Replace(currentText, record[Value], record[Formatted Value])
)
in
ReplaceValues
Rename this query .
Step 4: Apply the Custom Function
Return to your main query.
Go to theAdd Columntab and click onCustom Column.
In theCustom :New column name: EnterResulting Text.Column formula: Enter the following formula:powerqueryCopy codeReplaceValues([Dependency Text], [GroupedData])
New column name: EnterResulting Text.
Column formula: Enter the following formula:powerqueryCopy codeReplaceValues([Dependency Text], [GroupedData])
ClickOK.
Return to your main query.
Go to theAdd Columntab and click onCustom Column.
In theCustom Columndialog:
New column name: EnterResulting Text.
Column formula: Enter the following formula:powerqueryCopy codeReplaceValues([Dependency Text], [GroupedData])
New column name: EnterResulting Text.
Column formula: Enter the following formula:
────────────────────────────────────────────────────────────────────
powerqueryCopy codeReplaceValues([Dependency Text], [GroupedData])
ClickOK.
Step 5: Clean Up
You can remove theGroupedDatacolumn if you no longer need it.
Ensure all steps are applied correctly in theApplied Stepspane.
You can remove theGroupedDatacolumn if you no longer need it.
Ensure all steps are applied correctly in theApplied Stepspane.
Result
You will now have a new column calledResulting , for each uniqueObjectandDependency Text, any occurrence ofValuewithinDependency Textis replaced with the correspondingFormatted Value. If theValueis not found, theDependency unchanged.
Example
Given the following data:
['Object', 'Dependency Text', 'Value', 'Formatted Value']
['Obj1', 'The quick brown fox', 'fox', 'FOX']
['Obj1', 'The quick brown fox', 'brown', 'BROWN']
['Obj2', 'Jumped over the lazy dog', 'dog', 'DOG']
['Obj2', 'Jumped over the lazy dog', 'cat', 'CAT']
TheResulting Textcolumn will be:
ForObj1: "The quick BROWN FOX"
ForObj2: "Jumped over the lazy DOG"
ForObj1: "The quick BROWN FOX"
ForObj2: "Jumped over the lazy DOG"
Note
This solution assumes that:
ValueandFormatted Valueare text columns.
Replacement is case-sensitive. If you need case-insensitive replacements, you can modify theText.Replacefunction toText.Replace(state, current[Value], current[Formatted Value], Comparer.OrdinalIgnoreCase).
ValueandFormatted Valueare text columns.
Replacement is case-sensitive. If you need case-insensitive replacements, you can modify theText.Replacefunction toText.Replace(state, current[Value], current[Formatted Value], Comparer.OrdinalIgnoreCase).
By following these steps, you'll achieve the desired result without the issues you've encountered previously.