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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Replace [textstring (the)] with [The textstring]

Hello,

 

I have a data source with one column that lists product names, some of which contain elements that have the "The" formatted at the end or in the middle of the string, and I need them at the beginning.

 

For example:

 

Beatles, the: Complete Collection --> The Beatles: Complete Collection

Allman Brothers Band (the): 2CDs --> The Allman Brothers Band: 2CDs

 

Not all of the elements have "The", and it can be formatted in five different ways:

 

  • , the
  • , The
  • , (the)
  •  (The)
  •  (the)

...so basically I need to find any of the above five elements, replace with "" AND insert "The " at the beginning of the string in one of the Power Query steps. Can this be done?

 

Thanks

1 REPLY 1
ImkeF
Community Champion
Community Champion

Sounds doable, please check this code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HcqxCoAgFAXQX3k4FTg1uqX9QaM4SF1SeGro+3+StjMc75VFFMbQJAmGXCsvQzDBjEtyqypor3bmEivZ3mbrg2ysNy3Tq6HNHeNPZyuQlOtD4AEVwgc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    ListOfKeywords = {", the", ", The", ", (the)", "(The)", "(the)"},
    PositionKeyword = Table.AddColumn(Source, "PositionKeyword", each List.PositionOf(List.Transform(ListOfKeywords, (x) => Text.Contains([Column1], x)), true)),
    Result = Table.AddColumn(PositionKeyword, "Result", each if [PositionKeyword] <> -1 then "The " & Text.Replace([Column1], ListOfKeywords{[PositionKeyword]}, "") else [Column1])
in
    Result

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors