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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
aqeel_shaikh
Helper I
Helper I

Between two numeric there is a special character which need to be replace with hyphen

@dufoq3 
Between two numeric there is a special character which need to be replace with hyphen

Eg: 111/2222***333$444

solution: 111-2222-333-444

 

Please help with a query solution.


1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @aqeel_shaikh

 

Result

dufoq3_0-1714471258960.png

 

Define CharsToReplace

dufoq3_1-1714471303830.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ01DcCAi0tLWNjYxUTExOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    CharsToReplace = "/*$",
    Ad_Cleaned = Table.AddColumn(Source, "Cleaned", each 
        [ a = Text.ToList([Column1]),
          b = Text.ToList(CharsToReplace),
          c = List.Zip({ b, List.Repeat({"-"}, List.Count(b)) }),
          d = Text.Combine(List.ReplaceMatchingItems(a, c)),
          e = Text.Combine(List.RemoveItems(Text.Split(d, "-"), {""}), "-")
        ][e], type text)
in
    Ad_Cleaned

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

6 REPLIES 6
dufoq3
Super User
Super User

Hi @aqeel_shaikh

 

Result

dufoq3_0-1714471258960.png

 

Define CharsToReplace

dufoq3_1-1714471303830.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ01DcCAi0tLWNjYxUTExOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    CharsToReplace = "/*$",
    Ad_Cleaned = Table.AddColumn(Source, "Cleaned", each 
        [ a = Text.ToList([Column1]),
          b = Text.ToList(CharsToReplace),
          c = List.Zip({ b, List.Repeat({"-"}, List.Count(b)) }),
          d = Text.Combine(List.ReplaceMatchingItems(a, c)),
          e = Text.Combine(List.RemoveItems(Text.Split(d, "-"), {""}), "-")
        ][e], type text)
in
    Ad_Cleaned

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

is this the correct query

let
Source = Excel.Workbook(File.Contents("C:\Users\s441801\OneDrive - Emirates Group\General - AQEEL\Power query\POWER Q TEST SAMPLE.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
Ad_Cleaned = Table.AddColumn(Sheet1_Sheet, "Cleaned", each
[ lst = {"0".."9"},
a1 = Splitter.SplitTextByCharacterTransition(each true, (x)=> not List.Contains(lst, x))([Column1]),
a2 = Text.Combine(List.RemoveItems(a1, {"-"})),
b1 = Splitter.SplitTextByCharacterTransition((x)=> not List.Contains(lst, x), each true)(a2),
b2 = Text.Combine(List.RemoveItems(b1, {"-"}))
[b2],
CharsToReplace = "!@#%^&/\*$_",
a = Text.ToList([Column1]),
b = Text.ToList(CharsToReplace),
c = List.Zip({ b, List.Repeat({"-"}, List.Count(b)) }),
d = Text.Combine(List.ReplaceMatchingItems(a, c)),
e = Text.Combine(List.RemoveItems(Text.Split(d, "-"), {""}), "-")
][e], type text)
in
Ad_Cleaned

You see that it is not, but you are mixing 2 separate topics and creating a mess.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi, I did try combining both and it worked, below is the final out put.

let
Source = Excel.Workbook(File.Contents("C:\Users\s441801\OneDrive - Emirates Group\General - AQEEL\Power query\POWER Q TEST SAMPLE.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
Ad_Cleaned = Table.AddColumn(Sheet1_Sheet, "Cleaned", each
[ lst = {"0".."9"},
a1 = Splitter.SplitTextByCharacterTransition(each true, (x)=> not List.Contains(lst, x))([Column1]),
a2 = Text.Combine(List.RemoveItems(a1, {"-"})),
b1 = Splitter.SplitTextByCharacterTransition((x)=> not List.Contains(lst, x), each true)(a2),
b2 = Text.Combine(List.RemoveItems(b1, {"-"}))
[b2],
CharsToReplace = "!@#%^&/\*$_",
a = Text.ToList([Column1]),
b = Text.ToList(CharsToReplace),
c = List.Zip({ b, List.Repeat({"-"}, List.Count(b)) }),
d = Text.Combine(List.ReplaceMatchingItems(a, c)),
e = Text.Combine(List.RemoveItems(Text.Split(d, "-"), {""}), "-")
][e], type text)
in
Ad_Cleaned

wdx223_Daniel
Super User
Super User

=Text.Combine(List.TransformMany(Splitter.SplitTextByCharacterTransition(each not List.Contains({"0".."9"},_),{"0".."9"})(111/2222***333$444"),each Splitter.SplitTextByCharacterTransition({"0".."9"},each not List.Contains({"0".."9"},_))(_),(x,y)=>y),"-")

Eg: 111/2222***333$444

is a sample, i have many number with different special character... how do i write query for all in one go.. Appreciate your help?

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors