PowerBI replace text query

JackkG

New Member
Joined
Dec 10, 2014
Messages
42
Hi Team,

I'm stuck with how to replace text in PowerBI. I got one column which contains multiple names with special chars and would like to get rid of all those and just keep it as names separated with ";". Is there an easy way to achieve this?

example

cell A2
Bage Khiu <57697099.b2wg>;Jonas Mill <59603752.dt98>;Raj Dev <60877919.q2y5>

cell A3
Mac Dillon <57697055.b2wg>;Ben Chad <59644752.dt98>;Matt Pal <60877339.q2y5>


Thank you!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Book3
ABC
1TextMerged
2Bage Khiu <57697099.b2wg>;Jonas Mill <59603752.dt98>;Raj Dev <60877919.q2y5>Bage Khiu;Jonas Mill;Raj Dev
3Mac Dillon <57697055.b2wg>;Ben Chad <59644752.dt98>;Matt Pal <60877339.q2y5>Mac Dillon;Ben Chad;Matt Pal
Sheet3


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Split = Table.SplitColumn(Source, "Text", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Text.1", "Text.2", "Text.3"}),
    Extract = Table.TransformColumns(Split, {{"Text.1", each Text.BeforeDelimiter(_, " <"), type text}, {"Text.2", each Text.BeforeDelimiter(_, " <"), type text}, {"Text.3", each Text.BeforeDelimiter(_, " <"), type text}}),
    Merge = Table.CombineColumns(Extract,{"Text.1", "Text.2", "Text.3"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged")
in
    Merge
 
Upvote 0
Solution
Here's a probably better way of doing it. Shorter anyway.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Transform = Table.TransformColumns(Source,{{"Text", each Text.Combine(List.Transform(Text.Split(_,";"), each Text.BeforeDelimiter(_, " <")),";")}})
in
    Transform
 
Upvote 0
Here's a probably better way of doing it. Shorter anyway.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Transform = Table.TransformColumns(Source,{{"Text", each Text.Combine(List.Transform(Text.Split(_,";"), each Text.BeforeDelimiter(_, " <")),";")}})
in
    Transform

Kool. You're genius man!! Codestar! :)
 
Upvote 0
A less efficient approach for this particular application uses a generalized custom function I called Table_ColumnRemoveDelimitedText

Power Query:
(tbl as table, col as text, delimiters as list) as table =>
    Table.TransformColumns(tbl,{{col, each let lst = {1..Text.Length(_) - Text.Length(Text.Replace(_,delimiters{0},""))} in 
        List.Accumulate(lst,_, (s,c)=> Text.Replace(Text.Replace(s, Text.BetweenDelimiters(s,delimiters{0},delimiters{1}),""),delimiters{0} & delimiters{1},""))}})

With this function loaded and correctly named the query is simply:

Power Query:
Table_ColumnRemoveDelimitedText(Excel.CurrentWorkbook(){[Name="Table1"]}[Content],"Text",{"<",">"})

Book2
ABCD
1TextText
2abc<12345>;def<789>;efgh<345678>abc;def;efgh
3abcdeabcde
4abc<comment1>, And def<comment2>abc, And def
5abc; def<comments 3;4>abc; def
6
Sheet1


or you could put the function in the same query as done here

Power Query:
let 
    myFunction = (tbl as table, col as text, delimiters as list) as table =>
        Table.TransformColumns(tbl,{{col, each let lst = {1..Text.Length(_) - Text.Length(Text.Replace(_,delimiters{0},""))} in 
        List.Accumulate(lst,_, (s,c)=> Text.Replace(Text.Replace(s, Text.BetweenDelimiters(s,delimiters{0},delimiters{1}),""),delimiters{0} & delimiters{1},""))}}),
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Result = myFunction(Source,"Text",{"<",">"})
in 
    Result

Note that reversed, unmatched or nested delimiters may cause unwanted or incomplete transformations.
 
Upvote 0
A less efficient approach for this particular application uses a generalized custom function I called Table_ColumnRemoveDelimitedText

Power Query:
(tbl as table, col as text, delimiters as list) as table =>
    Table.TransformColumns(tbl,{{col, each let lst = {1..Text.Length(_) - Text.Length(Text.Replace(_,delimiters{0},""))} in
        List.Accumulate(lst,_, (s,c)=> Text.Replace(Text.Replace(s, Text.BetweenDelimiters(s,delimiters{0},delimiters{1}),""),delimiters{0} & delimiters{1},""))}})

With this function loaded and correctly named the query is simply:

Power Query:
Table_ColumnRemoveDelimitedText(Excel.CurrentWorkbook(){[Name="Table1"]}[Content],"Text",{"<",">"})

Book2
ABCD
1TextText
2abc<12345>;def<789>;efgh<345678>abc;def;efgh
3abcdeabcde
4abc<comment1>, And def<comment2>abc, And def
5abc; def<comments 3;4>abc; def
6
Sheet1


or you could put the function in the same query as done here

Power Query:
let
    myFunction = (tbl as table, col as text, delimiters as list) as table =>
        Table.TransformColumns(tbl,{{col, each let lst = {1..Text.Length(_) - Text.Length(Text.Replace(_,delimiters{0},""))} in
        List.Accumulate(lst,_, (s,c)=> Text.Replace(Text.Replace(s, Text.BetweenDelimiters(s,delimiters{0},delimiters{1}),""),delimiters{0} & delimiters{1},""))}}),
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Result = myFunction(Source,"Text",{"<",">"})
in
    Result

Note that reversed, unmatched or nested delimiters may cause unwanted or incomplete transformations.
Thank you, JGordon11. Appreciate your help on this one. I can see there are different ways to achieve one thing. :)

 
Upvote 0

Forum statistics

Threads
1,215,145
Messages
6,123,291
Members
449,094
Latest member
GoToLeep

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top