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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
CJDK
Helper II
Helper II

Split specific text strings into separate column

I have a column with a mix of 50 country names and 3 region names. I want to split the column so that the countries and regions are in separate columns, so that I can build a hierarchy. In other words, how do I split a column using specific text strings?

 

From:

CJDK_2-1601029017732.png

 

To:

CJDK_1-1601028823343.png

Thanks in advance!

4 REPLIES 4
FarhanAhmed
Community Champion
Community Champion

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs4vzSspqlQwVIrVQfCMwLyg1PTM/DwoByZljMIzQVYINCMWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Column1", "Column1 - Copy"),

// Code to replace values
    #"Replaced Value" = Table.ReplaceValue( #"Duplicated Column"  ,each [Column1],each if Text.Contains([Column1], "Region") then "" else [Column1],Replacer.ReplaceValue,{"Column1"}),
    #"Renamed Columns1" = Table.RenameColumns(#"Replaced Value",{{"Column1", "Country"}}),
    #"Added Conditional Column" = Table.AddColumn(#"Renamed Columns1", "Country_", each if [Country] = [#"Column1 - Copy"] then "" else [#"Column1 - Copy"]),
    #"Renamed Columns" = Table.RenameColumns(#"Added Conditional Column",{{"Country_", "Region"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Column1 - Copy"})
in
    #"Removed Columns"






Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!

Proud to be a Super User!




amitchandak
Super User
Super User

@CJDK , Can try a new column like

 

region = if( Search("Region",[country],,0) >0 , [country] , blank())

or

region = if( [country] in {"Bal Region","Ca Region"," .. Region"} , [country] , blank()) //give correct region name

@amitchandak Wow! Fast! Thank you! 👍 I am getting an error Token RightParen expected. Do you have a suggestion about that?

CJDK_0-1601031460770.png

 

I am having technical problems with Power BI Desktop, so this might be why the column creation is not working.

Hi @CJDK,

 

You may try this.

each if [Country] = "Balkans Region" then "Balkans Region" else if [Country] = "Caucasus Region" then "Caucasus Region" else if [Country] = "Central Asian Region" then "Central Asian Region" else null

 3.png

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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