Forum Discussion

WALEED's avatar
WALEED
Advocate II
8 years ago
Solved

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: TAG DATA SHEET BLOCK DIAGRAM BOB101 AAA;BBB,CCC AAA;BBB,C...
  • WALEED's avatar
    WALEED
    8 years ago

    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.