Forum Discussion
rafterse
1 year agoHelper I
Power query, move rows to columns based on there value
The data
- 1 year ago
Hi rafterse
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.
mromain
1 year agoRegular Visitor
Hello rafterse,
Here is a possible solution:
let
Source = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WciwoyElV0lHyzU/KhDAqgEApVgchlZOYl5KTmQdi+sAl/fKzMxOx6wtOzC0uzUsnJInVWHzSsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Company = _t, #"Phone type" = _t, number = _t]), {{"Company", Any.Type}, {"Phone type", Any.Type}, {"number", Any.Type}}),
DataLandlines =
let
FilterLandLines = Table.SelectRows(Source, each ([Phone type] = "landline")),
GroupByCompany = Table.Group(FilterLandLines, {"Company"}, {{"DataLandlines", each _[[Phone type], [number]], type table [Phone type=text, number=text]}}),
fnTransformData = (t as table) as table =>
let
AddColumnIndex = Table.AddIndexColumn(t, "Index", 1, 1, Int64.Type),
AddColumnName = Table.AddColumn(AddColumnIndex, "Name", each Text.Proper([Phone type]) & Text.From([Index])),
SelectColumns = Table.SelectColumns(AddColumnName,{"Name", "number"})
in
SelectColumns,
TransformDatalandLines = Table.TransformColumns(GroupByCompany, {{"DataLandlines", fnTransformData, type table}}),
DevelopDataLandlines = Table.ExpandTableColumn(TransformDatalandLines, "DataLandlines", {"Name", "number"}, {"Name", "number"}),
PivotColumnLandlines = Table.Pivot(DevelopDataLandlines, List.Distinct(DevelopDataLandlines[Name]), "Name", "number")
in
PivotColumnLandlines,
MergeDataLandlines = Table.NestedJoin(Source, {"Company"}, DataLandlines, {"Company"}, "DataLandlines", JoinKind.LeftOuter),
ExpandDataLandlines = let cols = List.Skip(Table.ColumnNames(DataLandlines)) in Table.ExpandTableColumn(MergeDataLandlines, "DataLandlines", cols, cols),
ReplaceNullLandline1 = Table.ReplaceValue(ExpandDataLandlines,null,"No Landline",Replacer.ReplaceValue,{"Landline1"})
in
ReplaceNullLandline1