Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
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.
Solved! Go to Solution.
Hi @vineshparekh,
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
Hi,
In one step with List.ReplaceMatchingItems
Text.Combine(
List.ReplaceMatchingItems(
Text.ToList([Original Data]),
List.Transform(Text.ToList(":;,!]){[|- "), each {_,""}))
)
)
Stéphane
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.
Hi @vineshparekh, done.
Specify chars and words to remove (I've already added all currencies):
Result:
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
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, 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
Required Data
Query I used: let in |
Hi @vineshparekh, done.
Specify chars and words to remove (I've already added all currencies):
Result:
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
Wow! This is some skills.
Works perfectly. Thanks much!
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.
Hi,
In one step with List.ReplaceMatchingItems
Text.Combine(
List.ReplaceMatchingItems(
Text.ToList([Original Data]),
List.Transform(Text.ToList(":;,!]){[|- "), each {_,""}))
)
)
Stéphane
Hi @vineshparekh,
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