cancel
Showing results for 
Search instead for 
Did you mean: 
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
Super User
Super User

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

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!

@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
Power BI December 2021 Update_carousel 768x460.jpg

Check it Out!

Click here to read more about the December 2021 Updates!

User Group Leader Meeting January 768x460.png

Calling all User Group Leaders!

Don't miss the User Group Leader meetings on January, 24th & 25th, 2022.

Jan 2022 Dev Camp 768x460 copy.png

Power BI Dev Camp- January 27th, 2022

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors