Forum Discussion
Transpose/Transform Multiple Rows into Single Row
- 7 years ago
you should be able to adjust this code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTJUitWJVkqCs5LhrBQ4KxXIMgKz0uCsdCDLGMzKALJMwKxMIMsUzMqCs7LhrBwIKxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [name = _t, id = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"id", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {{"names", each Table.Transpose(Table.FromList(List.Combine({[name]}))), type table}}), #"Expanded names" = Table.ExpandTableColumn(#"Grouped Rows", "names", {"Column1", "Column2", "Column3", "Column4"}, {"Column1", "Column2", "Column3", "Column4"}) in #"Expanded names"you should:
1) replace the red code with your table definition
2) add coma at the end of your table definition
3) replace blue step name with whatever is the the name of the last step in your table definition
hope that helps :)
- 7 years ago
if you replace last row in the sytnax with this
= Table.ExpandTableColumn(#"Grouped Rows", "names", List.Distinct(List.Combine(List.Transform(#"Grouped Rows"[names], each Table.ColumnNames(_)))), List.Distinct(List.Combine(List.Transform(#"Grouped Rows"[names], each Table.ColumnNames(_)))))
then it will always expand all the columns
- 7 years ago
try this
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tcw5DsAgEEPRu0xNCrKcBlGQQDbI/dtYM5Khe/qWHIIkceIlOmjy9N411IPKVIFm1Uld0KK6oVX1QJtJH80vlbqGvVKN+kzxBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [name = _t, id = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"id", Int64.Type}}), ExcludedItems = Table.SelectRows(#"Changed Type", each Text.EndsWith([name], "-1")), ExcludedItemsWithKey = Table.AddColumn(ExcludedItems, "Custom", each Text.Start([name], Text.Length([name]) - 2)), WithoutExcludedItems = Table.SelectRows(#"Changed Type", each not Text.EndsWith([name], "-1")), #"Merged Queries" = Table.NestedJoin(WithoutExcludedItems,{"name", "id"},ExcludedItemsWithKey,{"Custom", "id"},"WithoutExcludedItems",JoinKind.LeftOuter), #"Expanded WithoutExcludedItems" = Table.ExpandTableColumn(#"Merged Queries", "WithoutExcludedItems", {"name"}, {"name with -1"}) in #"Expanded WithoutExcludedItems"
Stachu thank your for help. I think I need to learn M code.
I have complicated the task a bit, and this solution does not fit. You can help transform the following table:
to table:
misspelled. I mean, Can you help transform the following table?
- Stachu7 years agoCommunity Champion
try this
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tcw5DsAgEEPRu0xNCrKcBlGQQDbI/dtYM5Khe/qWHIIkceIlOmjy9N411IPKVIFm1Uld0KK6oVX1QJtJH80vlbqGvVKN+kzxBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [name = _t, id = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"id", Int64.Type}}), ExcludedItems = Table.SelectRows(#"Changed Type", each Text.EndsWith([name], "-1")), ExcludedItemsWithKey = Table.AddColumn(ExcludedItems, "Custom", each Text.Start([name], Text.Length([name]) - 2)), WithoutExcludedItems = Table.SelectRows(#"Changed Type", each not Text.EndsWith([name], "-1")), #"Merged Queries" = Table.NestedJoin(WithoutExcludedItems,{"name", "id"},ExcludedItemsWithKey,{"Custom", "id"},"WithoutExcludedItems",JoinKind.LeftOuter), #"Expanded WithoutExcludedItems" = Table.ExpandTableColumn(#"Merged Queries", "WithoutExcludedItems", {"name"}, {"name with -1"}) in #"Expanded WithoutExcludedItems"- Anthony0077 years agoHelper I
Thanks for the help and time spent on me. All solutions work great.
tell me where it is better to take the literature for learning M code? I saw the list of formulas.
But I see there is a different methods for use, to examples, using "#" and not using- Stachu7 years agoCommunity Champion
you could try this
https://msdn.microsoft.com/en-us/query-bi/m/power-query-m-language-specificationbut it's quite technical
these videos are quite good as well
https://www.youtube.com/watch?v=ecfRTEoYadIhttps://www.youtube.com/watch?v=L0Y1KL7o3aQ
other than language specification I haven't read any books. just lots of blogs, videos etc.