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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
vineshparekh
Helper I
Helper I

Replace multiple values containing specific letters in Power Query

Hello,

 

I am stuck in Power Query where I want to replace some characters within the column and keep rest of the characters. 

Is there a way to remove these extra characters with one step? 

 

For example,

Original Data           Required Data

: XYZ                        XYZ

XYZ :]                       XYZ

XYZ --                      XYZ

 

Thank you.

4 ACCEPTED SOLUTIONS
dufoq3
Super User
Super User

Hi @vineshparekh,

dufoq3_0-1705673164215.png

 

  • edit 2nd step YourSource = Source (refer to your date after equals sign)
  • in 3rd step CharsToBeReplaced you can add more characters which should be deleted (just put them in quotes one by one and separate by comma)
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WslKIiIxSitWJVgLSClaxcKauroJSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Original Data" = _t]),
    YourSource = Source,
    CharsToBeReplaced = {":", "]", "-"},
    StepBack = YourSource,
    Ad_CleanedData = Table.AddColumn(StepBack, "Cleaned Data", each
        Text.Trim(
            List.Accumulate(
                List.Buffer(CharsToBeReplaced),
                [Original Data],
                (s,c)=> Text.Replace(Text.From(s), c, "")
            )), type text)
in
    Ad_CleanedData

 


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

slorin
Super User
Super User

Hi,

In one step  with List.ReplaceMatchingItems

 

Text.Combine(
List.ReplaceMatchingItems(
Text.ToList([Original Data]),
List.Transform(Text.ToList(":;,!]){[|- "), each {_,""}))
)
)

 Stéphane

View solution in original post

spinfuzer
Super User
Super User

If the characters are in the middle of your text, did you want to keep it or remove them too.  If you want to remove them regardless of where they are, the other solutions will work.

 

If you need to keep the characters in the middle the below will work.

Add custom column

 

 

Text.Trim([Original Data], Text.ToList(" :;[]-")) 

 

 

@dufoq3 Thanks did not realize you could put a list in Text.Trim.

 

View solution in original post

Hi @vineshparekh, done.

 

Specify chars and words to remove (I've already added all currencies):

dufoq3_3-1709839822590.png

 

Result:

dufoq3_0-1709839701594.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wso1R0jXQMzBUcHZ0iVFSitWJVjID8g0UQoNdwDyQAiMjYz1LsBBUSbSCqameqQGYHREZpWAVC9NpagAySQGkLhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    CharsToRemove = " ,[]:/|\*-+=_()';.'""{}!?`~@#$%^&*",
    WordsToRemove = List.Buffer({"AED", "AFN", "ALL", "AMD", "AOA", "ARS", "AUD", "AUD", "AUD", "AUD", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CHF", "CLP", "CNY", "COP", "CRC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "FJD", "GBP", "GEL", "GHS", "GMD", "GNF", "GTQ", "GYD", "HNL", "HTG", "HUF", "IDR", "ILS", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SLL", "SOS", "SRD", "SSP", "STD", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XAF", "XAF", "XAF", "XAF", "XAF", "XCD", "XCD", "XCD", "XCD", "XCD", "XCD", "XOF", "XOF", "XOF", "XOF", "XOF", "XOF", "XOF", "XOF", "YER", "ZAR", "ZMW"}),
    StepBack = Source,
    Ad_Cleaned = Table.AddColumn(StepBack, "Cleaned", each 
     [ a = Text.Trim([Column1], Text.ToList(CharsToRemove)), //remove Chars
       b = List.Select(Text.Split(a, " "), each _ <> ""), //split to list and remove spaces
       c = List.RemoveMatchingItems(b, WordsToRemove),
       d = Text.Combine(c, "")
     ][d], 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

10 REPLIES 10
vineshparekh
Helper I
Helper I

Hi All,

 

Thank you for these responses and this works perfectly for removing the characters.

I have a question when I work on the report, I have few words along with the characters that I want to remove. Could you please help? Please note that there are spaces too.

 

Original Data           Required Data

: XYZ  "                      XYZ

XYZ :]                        XYZ

XYZ --    "                  XYZ

XYZ CAD                   XYZ

=" XYZ                      XYZ

 

Thanks again,

Vinesh.

Hi, could you share sample of real data please? We can create such query based on this new sample, but it won't probably work with real data.


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, please see the attached and unfortunately now the query is not working for the characters as well. I want to get a column removing all characters, spaces, specific words like CAD or USD to get the numbers or XYZ.

Total
="-0.01 CAD"
60.00 USD
="-223.90 USD"
[ 55.50
XYZ :]
60.50 CAD "

 

Required Data

0.01
60
223.9
55.5
XYZ
60.5

 

Query I used:

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Total", type text}}),
RemoveChar = Text.Combine(
List.ReplaceMatchingItems(
Text.ToList([Total]),
List.Transform(Text.ToList(":;,!]){[|- "), each {_,""}))
)

in
RemoveChar

Hi @vineshparekh, done.

 

Specify chars and words to remove (I've already added all currencies):

dufoq3_3-1709839822590.png

 

Result:

dufoq3_0-1709839701594.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wso1R0jXQMzBUcHZ0iVFSitWJVjID8g0UQoNdwDyQAiMjYz1LsBBUSbSCqameqQGYHREZpWAVC9NpagAySQGkLhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    CharsToRemove = " ,[]:/|\*-+=_()';.'""{}!?`~@#$%^&*",
    WordsToRemove = List.Buffer({"AED", "AFN", "ALL", "AMD", "AOA", "ARS", "AUD", "AUD", "AUD", "AUD", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CHF", "CLP", "CNY", "COP", "CRC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "FJD", "GBP", "GEL", "GHS", "GMD", "GNF", "GTQ", "GYD", "HNL", "HTG", "HUF", "IDR", "ILS", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SLL", "SOS", "SRD", "SSP", "STD", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XAF", "XAF", "XAF", "XAF", "XAF", "XCD", "XCD", "XCD", "XCD", "XCD", "XCD", "XOF", "XOF", "XOF", "XOF", "XOF", "XOF", "XOF", "XOF", "YER", "ZAR", "ZMW"}),
    StepBack = Source,
    Ad_Cleaned = Table.AddColumn(StepBack, "Cleaned", each 
     [ a = Text.Trim([Column1], Text.ToList(CharsToRemove)), //remove Chars
       b = List.Select(Text.Split(a, " "), each _ <> ""), //split to list and remove spaces
       c = List.RemoveMatchingItems(b, WordsToRemove),
       d = Text.Combine(c, "")
     ][d], 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.

Wow! This is some skills.

Works perfectly. Thanks much! 

You're welcome 😉


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

spinfuzer
Super User
Super User

If the characters are in the middle of your text, did you want to keep it or remove them too.  If you want to remove them regardless of where they are, the other solutions will work.

 

If you need to keep the characters in the middle the below will work.

Add custom column

 

 

Text.Trim([Original Data], Text.ToList(" :;[]-")) 

 

 

@dufoq3 Thanks did not realize you could put a list in Text.Trim.

 

This could be done more easily I would say:

dufoq3_1-1705693020050.png

Add this as custom column:

= Text.Trim( [Original Data], { " ", ":", "]", "-" } )

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

slorin
Super User
Super User

Hi,

In one step  with List.ReplaceMatchingItems

 

Text.Combine(
List.ReplaceMatchingItems(
Text.ToList([Original Data]),
List.Transform(Text.ToList(":;,!]){[|- "), each {_,""}))
)
)

 Stéphane

dufoq3
Super User
Super User

Hi @vineshparekh,

dufoq3_0-1705673164215.png

 

  • edit 2nd step YourSource = Source (refer to your date after equals sign)
  • in 3rd step CharsToBeReplaced you can add more characters which should be deleted (just put them in quotes one by one and separate by comma)
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WslKIiIxSitWJVgLSClaxcKauroJSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Original Data" = _t]),
    YourSource = Source,
    CharsToBeReplaced = {":", "]", "-"},
    StepBack = YourSource,
    Ad_CleanedData = Table.AddColumn(StepBack, "Cleaned Data", each
        Text.Trim(
            List.Accumulate(
                List.Buffer(CharsToBeReplaced),
                [Original Data],
                (s,c)=> Text.Replace(Text.From(s), c, "")
            )), type text)
in
    Ad_CleanedData

 


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

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors