Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!
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:
To:
Thanks in advance!
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"
Proud to be a 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?
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
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.
User | Count |
---|---|
140 | |
113 | |
104 | |
77 | |
64 |
User | Count |
---|---|
135 | |
122 | |
101 | |
71 | |
61 |