Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
Hi all,
I am trying to acheive a logic in Power Query.
Lets consider the below sample data. Series is the source data, and i want to convert the source data as mentioned in the second column, which is expected output.
Series column can have values , seperated by | and it can come in any order as shown here, but in the output it should be ordered as needed.
Any help will be highly appriciated.
| Series | ExpectedOutput |
| A|D|B | A|B|D |
| C|A|E|D | A|C|D|E |
| D|B | B|D |
| E|D|A | A|D|E |
| D | D |
| D|E|B|A|C | A|B|C|D|E |
Thanks
Raj
Solved! Go to Solution.
I thought this, a small variation of what we had before, would work but it doesn't. I haven't had the time to fix it:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PczBDQAgCAPAVQxvl0BhCuImHd4q6A/K0QhRGEaTzmHAZPWQCYXDMpy8+43LPUUBTfPFWa2ws4/vr7p61gY=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Series " = _t, ExpectedOutput = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Series ", type text}, {"ExpectedOutput", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"ExpectedOutput"}),
#"Trimmed Text" = Table.TransformColumns(#"Removed Columns",{{"Series ", Text.Trim, type text}}),
#"Added Custom" = Table.AddColumn(#"Trimmed Text", "Custom", each Text.Combine(List.Sort(Text.Split([#"Series "],"|"), List.PositionOf({"C","E","D","B","A","K","F"}, [#"Series "])),"|")),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Series "})
in
#"Removed Columns1"
So try this other approach instead, less elegant but effective:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PczBDQAgCAPAVQxvl0BhCuImHd4q6A/K0QhRGEaTzmHAZPWQCYXDMpy8+43LPUUBTfPFWa2ws4/vr7p61gY=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Series " = _t, ExpectedOutput = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Series ", type text}, {"ExpectedOutput", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"ExpectedOutput"}),
#"Trimmed Text" = Table.TransformColumns(#"Removed Columns",{{"Series ", Text.Trim, type text}}),
#"Added Custom" = Table.AddColumn(#"Trimmed Text", "Custom", each Table.FromList(Text.Split([#"Series "],"|"))),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Series "}),
#"Added Custom3" = Table.AddColumn(#"Removed Columns1", "Custom.2", each Table.AddColumn([Custom], "Aux", each List.PositionOf({"C","E","D","B","A","K","F"}, [Column1]))),
#"Added Custom4" = Table.AddColumn(#"Added Custom3", "Custom.3", each Table.Sort([Custom.2], each [Aux])),
#"Added Custom6" = Table.AddColumn(#"Added Custom4", "Custom.4", each Text.Combine([Custom.3][Column1],"|")),
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom6",{"Custom", "Custom.2", "Custom.3"})
in
#"Removed Columns2"
I thought this, a small variation of what we had before, would work but it doesn't. I haven't had the time to fix it:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PczBDQAgCAPAVQxvl0BhCuImHd4q6A/K0QhRGEaTzmHAZPWQCYXDMpy8+43LPUUBTfPFWa2ws4/vr7p61gY=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Series " = _t, ExpectedOutput = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Series ", type text}, {"ExpectedOutput", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"ExpectedOutput"}),
#"Trimmed Text" = Table.TransformColumns(#"Removed Columns",{{"Series ", Text.Trim, type text}}),
#"Added Custom" = Table.AddColumn(#"Trimmed Text", "Custom", each Text.Combine(List.Sort(Text.Split([#"Series "],"|"), List.PositionOf({"C","E","D","B","A","K","F"}, [#"Series "])),"|")),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Series "})
in
#"Removed Columns1"
So try this other approach instead, less elegant but effective:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PczBDQAgCAPAVQxvl0BhCuImHd4q6A/K0QhRGEaTzmHAZPWQCYXDMpy8+43LPUUBTfPFWa2ws4/vr7p61gY=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Series " = _t, ExpectedOutput = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Series ", type text}, {"ExpectedOutput", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"ExpectedOutput"}),
#"Trimmed Text" = Table.TransformColumns(#"Removed Columns",{{"Series ", Text.Trim, type text}}),
#"Added Custom" = Table.AddColumn(#"Trimmed Text", "Custom", each Table.FromList(Text.Split([#"Series "],"|"))),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Series "}),
#"Added Custom3" = Table.AddColumn(#"Removed Columns1", "Custom.2", each Table.AddColumn([Custom], "Aux", each List.PositionOf({"C","E","D","B","A","K","F"}, [Column1]))),
#"Added Custom4" = Table.AddColumn(#"Added Custom3", "Custom.3", each Table.Sort([Custom.2], each [Aux])),
#"Added Custom6" = Table.AddColumn(#"Added Custom4", "Custom.4", each Text.Combine([Custom.3][Column1],"|")),
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom6",{"Custom", "Custom.2", "Custom.3"})
in
#"Removed Columns2"
Hi @Anonymous
Try this based on your sample data. The important part is the one highlighted in red:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PczBDQAgCAPAVQxvl0BhCuImHd4q6A/K0QhRGEaTzmHAZPWQCYXDMpy8+43LPUUBTfPFWa2ws4/vr7p61gY=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Series " = _t, ExpectedOutput = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Series ", type text}, {"ExpectedOutput", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"ExpectedOutput"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns", "Custom", each Text.Combine(List.Sort(Text.Split([#"Series "],"|"),Order.Ascending),"|")),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Series "})
in
#"Removed Columns1"
@AlB - Thanks for the excellent code. This perfectly works.can i add one more qn? In the real case, the order is not simple ascending, can we sort this based on a custom list or order? For Ex: Instead of A|B|C|D|E|F|K, the order should be C|E|D|B|A|K|F, how to do that?
Many thanks for your help.
Thanks
Raj
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 48 | |
| 45 | |
| 41 | |
| 20 | |
| 17 |
| User | Count |
|---|---|
| 69 | |
| 64 | |
| 32 | |
| 31 | |
| 27 |