Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
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.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
110 | |
94 | |
81 | |
66 | |
58 |
User | Count |
---|---|
150 | |
119 | |
104 | |
87 | |
67 |