Forum Discussion
han_rj
1 year agoHelper IV
Transpose Data in Power Query Problem
Hi , I need help achieve this expected output where the resultant is a transposed data. Row scrore is transposed into a column but instead of manually renaming as Country1_Score is there an autom...
- 1 year ago
Hi han_rj, check this:
Output
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8swrKC1R0lFCoFidaCjTOb80r6SoUsEQVSY4tbg4Mz/PEME0QjCNEUwTsIbg5PyiVKCgoQGQMAURRmAWWNI3NSUxByQExMZgUaBCJBfgdJIRNZxkCrXZDKt7DKBuAronFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t]), // You can probably delete this step. ReplacedBlankWithNull = Table.TransformColumns(Source,{}, each if _ = "" then null else _), FilteredRows = Table.SelectRows(ReplacedBlankWithNull, each not List.IsEmpty(List.RemoveNulls(Record.ToList(_)))), FnCheckCountry = each List.Contains(Record.ToList(_), "Country", (x,y)=> Text.StartsWith(x ?? "", y)), RemovedTopRows = Table.Skip(FilteredRows, each not FnCheckCountry(_)), Ad_GroupHelper = Table.AddColumn(RemovedTopRows, "GroupHelper", each FnCheckCountry(_), type logical), GroupedRows = Table.Group(Ad_GroupHelper, "GroupHelper", {{"T", each [ a = Table.RemoveColumns(_, {"GroupHelper"}), b = Table.PromoteHeaders(Table.FromRows(Table.ToColumns(Table.Skip(a)))), c = List.Transform(Table.ColumnNames(b), (x)=> Text.Combine({List.RemoveNulls(Record.ToList(a{0})){0}, x}, "_")), d = Table.RenameColumns(b, List.Zip({ Table.ColumnNames(b), c })) ][d], type table}}, 0, (x,y)=> Byte.From(y = true) ), T = GroupedRows[T], Combined = Table.FromColumns(List.Combine(List.Transform(T, Table.ToColumns)), List.Combine(List.Transform(T, Table.ColumnNames)) ), RenamedColumns = Table.RenameColumns(Combined,{{Table.ColumnNames(Combined){0}, "Type"}}), RemovedOtherColumns = Table.SelectColumns(RenamedColumns, List.Select(Table.ColumnNames(RenamedColumns), (x)=> not Text.EndsWith(x, "Column1"))) in RemovedOtherColumns
dufoq3
1 year agoCommunity Champion
Hi han_rj, check this:
Output
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8swrKC1R0lFCoFidaCjTOb80r6SoUsEQVSY4tbg4Mz/PEME0QjCNEUwTsIbg5PyiVKCgoQGQMAURRmAWWNI3NSUxByQExMZgUaBCJBfgdJIRNZxkCrXZDKt7DKBuAronFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t]),
// You can probably delete this step.
ReplacedBlankWithNull = Table.TransformColumns(Source,{}, each if _ = "" then null else _),
FilteredRows = Table.SelectRows(ReplacedBlankWithNull, each not List.IsEmpty(List.RemoveNulls(Record.ToList(_)))),
FnCheckCountry = each List.Contains(Record.ToList(_), "Country", (x,y)=> Text.StartsWith(x ?? "", y)),
RemovedTopRows = Table.Skip(FilteredRows, each not FnCheckCountry(_)),
Ad_GroupHelper = Table.AddColumn(RemovedTopRows, "GroupHelper", each FnCheckCountry(_), type logical),
GroupedRows = Table.Group(Ad_GroupHelper, "GroupHelper", {{"T", each
[ a = Table.RemoveColumns(_, {"GroupHelper"}),
b = Table.PromoteHeaders(Table.FromRows(Table.ToColumns(Table.Skip(a)))),
c = List.Transform(Table.ColumnNames(b), (x)=> Text.Combine({List.RemoveNulls(Record.ToList(a{0})){0}, x}, "_")),
d = Table.RenameColumns(b, List.Zip({ Table.ColumnNames(b), c }))
][d], type table}}, 0,
(x,y)=> Byte.From(y = true) ),
T = GroupedRows[T],
Combined = Table.FromColumns(List.Combine(List.Transform(T, Table.ToColumns)), List.Combine(List.Transform(T, Table.ColumnNames)) ),
RenamedColumns = Table.RenameColumns(Combined,{{Table.ColumnNames(Combined){0}, "Type"}}),
RemovedOtherColumns = Table.SelectColumns(RenamedColumns, List.Select(Table.ColumnNames(RenamedColumns), (x)=> not Text.EndsWith(x, "Column1")))
in
RemovedOtherColumns