Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
WALEED
Advocate II
Advocate II

Replace Text using a Lookup Table

Gents,

 

It's hard to explain my problem using words so I'll list a few examples here:

 

I have 2 tables:

1 holding the data:

TAGDATA SHEETBLOCK DIAGRAM
BOB101AAA;BBB,CCCAAA;BBB,CCC
BOB102AAA;BBB,CCCAAA;BBB,CCC
BOB103AAA;BBB,CCCAAA;BBB,CCC

 

1 holding the rules for text replacement

AttributeOld TextNew Text
DATA SHEET;|
DATA SHEET,|
BLOCK DIAGRAM;|
BLOCK DIAGRAM,|

 

I'm after the results below:

TAGDATA SHEETBLOCK DIAGRAM
BOB101AAA|BBB|CCCAAA|BBB|CCC
BOB102AAA|BBB|CCCAAA|BBB|CCC
BOB103AAA|BBB|CCCAAA|BBB|CCC

 

The dataset is huge and so is the set of rules, they're on 2 separate excel files.

 

I'm after a dynamic approach where I can simply add lines to the rules file and see the changes after a refresh.

1 ACCEPTED SOLUTION

Changed the whole approach to a function. Many thanks @vanessafvg

I had hoped that a merge would do the trick. It works perfectly for a full replacement of a value but this one's trickier.

 

 

(InputAttr,InputValue)=> 
let
	SubTable = Table.SelectRows(REPLACE_TABLE,each [ATTR] = InputAttr),
	DoReplacement = List.Generate(
		  ()=> [Counter=0, MyAttr=InputAttr, MyText=InputValue], 
		  each [Counter]<=List.Count(SubTable[OLD VAL]), 
		  each [Counter=[Counter]+1,
				MyText=Text.Replace(
						 [MyText], 
						 SubTable[OLD VAL]{[Counter]}, 
						 SubTable[NEW VAL]{[Counter]})], 
		  each [MyText]),
	GetLastValue = List.Last(DoReplacement)
in
	GetLastValue

 

 

Used this function to add a column; using the GetLastValue of the function as a Value for each row.

InputAttr is the Attribute/Field Name (after pivoting the table).

InputValue is the text that needs to be partially replaced.

List.Generate iterates through the text changes (in order of the RULES_TABLE).

 

Days of trial and error. not sure if I fully understand what I did there. Steps below:

REPLACE_TABLE

ATTROLD VALNEW VAL
DATA SHEET;|
DATA SHEET,|
BLOCK DIAGRAM;|
BLOCK DIAGRAM,|

 

DATA_TABLE

TAGDATA SHEETBLOCK DIAGRAM
BOB101AAA;BBB,CCCAAA;BBB,CCC
BOB102AAA;BBB,CCCAAA;BBB,CCC
BOB103AAA;BBB,CCCAAA;BBB,CCC

 

DATA_TABLE(UNPIVOT)

TAGInputAttrInputValue
BOB101DATA SHEETAAA;BBB,CCC
BOB101BLOCK DIAGRAMAAA;BBB,CCC
BOB102DATA SHEETAAA;BBB,CCC
BOB102BLOCK DIAGRAMAAA;BBB,CCC
BOB103DATA SHEETAAA;BBB,CCC
BOB103BLOCK DIAGRAMAAA;BBB,CCC

 

DATA_TABLE(Add Column with Function Above)

TAGInputAttrInputValueChanged Text
BOB101DATA SHEETAAA;BBB,CCCAAA|BBB|CCC
BOB101BLOCK DIAGRAMAAA;BBB,CCCAAA|BBB|CCC
BOB102DATA SHEETAAA;BBB,CCCAAA|BBB|CCC
BOB102BLOCK DIAGRAMAAA;BBB,CCCAAA|BBB|CCC
BOB103DATA SHEETAAA;BBB,CCCAAA|BBB|CCC
BOB103BLOCK DIAGRAMAAA;BBB,CCCAAA|BBB|CCC

 

Straightforward from there; delete old column and re-pivot using unique TAG and InputAttr.

View solution in original post

7 REPLIES 7
vanessafvg
Super User
Super User

@WALEED why dont you just do a replace in powerquery (M)?

 

right click on the column in the query editor and select replace values?

 

 

 





If I took the time to answer your question and I came up with a solution, please mark my post as a solution and /or give kudos freely for the effort 🙂 Thank you!

Proud to be a Super User!




That would be ideal for a simple dataset.

 

My problem is that the list of rules is ever changing, adding/removing as new problems arise.

And the number of columns I have is 243 (can grow wider in the future)

 

The rules excel sheet is stored in a shared area where the team can add rows without opening the super heavy PBI file.

@WALEED sounds like you need to create a function then and pass parameters?

 

https://blog.learningtree.com/creating-functions-m-power-query-formula-language/

 

https://www.mattmasson.com/2014/11/converting-a-query-to-a-function-in-power-query/





If I took the time to answer your question and I came up with a solution, please mark my post as a solution and /or give kudos freely for the effort 🙂 Thank you!

Proud to be a Super User!




If only I could cook up the code to feed the function 😄 please help!

@WALEED email me ill give it a bash vanessafvg@gmail.com





If I took the time to answer your question and I came up with a solution, please mark my post as a solution and /or give kudos freely for the effort 🙂 Thank you!

Proud to be a Super User!




@vanessafvg

 

1. I've unpivoted the data table. All column names are now under "Attribute", and all values are under "Value"

2. I think I'm close. The formula is valid but the results are unchanged:

Custom1 = 
Table.ReplaceValue(LastStep,
each if [Attribute] = RULE_TABLE[Attribute] then RULE_TABLE[OLD VAL] else RULE_TABLE[NEW VAL],
each RULE_TABLE[NEW VAL],
Replacer.ReplaceText,
{"Value"})

Maybe I'm misplacing the "each"?

Changed the whole approach to a function. Many thanks @vanessafvg

I had hoped that a merge would do the trick. It works perfectly for a full replacement of a value but this one's trickier.

 

 

(InputAttr,InputValue)=> 
let
	SubTable = Table.SelectRows(REPLACE_TABLE,each [ATTR] = InputAttr),
	DoReplacement = List.Generate(
		  ()=> [Counter=0, MyAttr=InputAttr, MyText=InputValue], 
		  each [Counter]<=List.Count(SubTable[OLD VAL]), 
		  each [Counter=[Counter]+1,
				MyText=Text.Replace(
						 [MyText], 
						 SubTable[OLD VAL]{[Counter]}, 
						 SubTable[NEW VAL]{[Counter]})], 
		  each [MyText]),
	GetLastValue = List.Last(DoReplacement)
in
	GetLastValue

 

 

Used this function to add a column; using the GetLastValue of the function as a Value for each row.

InputAttr is the Attribute/Field Name (after pivoting the table).

InputValue is the text that needs to be partially replaced.

List.Generate iterates through the text changes (in order of the RULES_TABLE).

 

Days of trial and error. not sure if I fully understand what I did there. Steps below:

REPLACE_TABLE

ATTROLD VALNEW VAL
DATA SHEET;|
DATA SHEET,|
BLOCK DIAGRAM;|
BLOCK DIAGRAM,|

 

DATA_TABLE

TAGDATA SHEETBLOCK DIAGRAM
BOB101AAA;BBB,CCCAAA;BBB,CCC
BOB102AAA;BBB,CCCAAA;BBB,CCC
BOB103AAA;BBB,CCCAAA;BBB,CCC

 

DATA_TABLE(UNPIVOT)

TAGInputAttrInputValue
BOB101DATA SHEETAAA;BBB,CCC
BOB101BLOCK DIAGRAMAAA;BBB,CCC
BOB102DATA SHEETAAA;BBB,CCC
BOB102BLOCK DIAGRAMAAA;BBB,CCC
BOB103DATA SHEETAAA;BBB,CCC
BOB103BLOCK DIAGRAMAAA;BBB,CCC

 

DATA_TABLE(Add Column with Function Above)

TAGInputAttrInputValueChanged Text
BOB101DATA SHEETAAA;BBB,CCCAAA|BBB|CCC
BOB101BLOCK DIAGRAMAAA;BBB,CCCAAA|BBB|CCC
BOB102DATA SHEETAAA;BBB,CCCAAA|BBB|CCC
BOB102BLOCK DIAGRAMAAA;BBB,CCCAAA|BBB|CCC
BOB103DATA SHEETAAA;BBB,CCCAAA|BBB|CCC
BOB103BLOCK DIAGRAMAAA;BBB,CCCAAA|BBB|CCC

 

Straightforward from there; delete old column and re-pivot using unique TAG and InputAttr.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors