Forum Discussion
Combing data from rows into columns
- 5 years ago
Hello scott_Abaana
in this case you can add a new column to unite both name-column in a record. Then use Table.Pivot that handles with List.Accumulate the different aggregated records.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJLzE0F0ZGpxSCeo6+BgaFSrA5C0ghE58PkjFDkjFE1GoMljaCSJigaTVDkTFHkTFHkzFANNVOKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [KEY = _t, Name = _t, Valid = _t, NameCode = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"KEY", Int64.Type}, {"Name", type text}, {"Valid", type text}}), #"Added Custom1" = Table.AddColumn(#"Changed Type", "Names", each [Name = [Name], NameCode=[NameCode]]), #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Name", "NameCode"}), #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Valid]), "Valid", "Names", each List.Accumulate(_, [], (o,r)=> if Record.FieldCount(o)>0 then [Name= o[Name]& ", " & r[Name], NameCode = o[NameCode]& ", " & r[NameCode]] else r )), #"Expanded Yes" = Table.ExpandRecordColumn(#"Pivoted Column", "Yes", {"Name", "NameCode"}, {"Yes.Name", "Yes.NameCode"}), #"Expanded No" = Table.ExpandRecordColumn(#"Expanded Yes", "No", {"Name", "NameCode"}, {"No.Name", "No.NameCode"}) in #"Expanded No"Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
OK so the slight twist when I went to apply it was that we lost and Unique code that is used to look up the Names
They also need to be put into the YES/NO Column.
I do think I could do it by running two queries of the same table and then merging them, but there is bound to be a cleaner way.
Hello scott_Abaana
in this case you can add a new column to unite both name-column in a record. Then use Table.Pivot that handles with List.Accumulate the different aggregated records.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJLzE0F0ZGpxSCeo6+BgaFSrA5C0ghE58PkjFDkjFE1GoMljaCSJigaTVDkTFHkTFHkzFANNVOKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [KEY = _t, Name = _t, Valid = _t, NameCode = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"KEY", Int64.Type}, {"Name", type text}, {"Valid", type text}}),
#"Added Custom1" = Table.AddColumn(#"Changed Type", "Names", each [Name = [Name], NameCode=[NameCode]]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Name", "NameCode"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Valid]), "Valid", "Names", each List.Accumulate(_, [], (o,r)=> if Record.FieldCount(o)>0 then [Name= o[Name]& ", " & r[Name], NameCode = o[NameCode]& ", " & r[NameCode]] else r )),
#"Expanded Yes" = Table.ExpandRecordColumn(#"Pivoted Column", "Yes", {"Name", "NameCode"}, {"Yes.Name", "Yes.NameCode"}),
#"Expanded No" = Table.ExpandRecordColumn(#"Expanded Yes", "No", {"Name", "NameCode"}, {"No.Name", "No.NameCode"})
in
#"Expanded No"
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
- scott_Abaana5 years agoHelper I
I think this works
But to make it work with this source code
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],I had to change the expanded date code: Yes to YES and No to NO, and make sure the data was consistent as I had both.
But its working on the TEST Data
- Jimmy8015 years agoCommunity Champion
Hello scott_Abaana
it has to be consistent. If you would have Yes and YES you would have to transform it before that is all the time the same. Power Query is case sensitive 🙂
So do you have still some question?
BR
Jimmy