Forum Discussion
Transform data like two row with main title and subtitle ?
- 5 years ago
Hello utgqw
here another approach. It uses List.Generate to create a new column that identifies the main groups. In the Group-function the first row is deleted and a new column with the main-cat is added
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcgQCJR0lQwMDpVidaKXE4pQ0INcYGw/CMYFwnIAAyDOC6itOBEvCjClPLQLyTJH1gTixAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cat = _t, Column1 = _t]), ChangedType = Table.TransformColumnTypes(Source,{{"Cat", type text}, {"Column1", Int64.Type}}), GenerateGroupColumns = List.Generate ( ()=> [RunningTotal = ChangedType[Column1]{0}, Counter=0, Group=1], each [Counter]<= Table.RowCount(ChangedType)-1, (rec)=> [ RunningTotal = if rec[RunningTotal] - ChangedType[Column1]{rec[Counter]+1}< 0 then ChangedType[Column1]{rec[Counter]+1} else rec[RunningTotal] - ChangedType[Column1]{rec[Counter]+1}, Counter = rec[Counter]+1, Group = if rec[RunningTotal] - ChangedType[Column1]{rec[Counter]+1}< 0 then rec[Group]+1 else rec[Group] ], each [Group] ), CreateTable = Table.FromColumns(Table.ToColumns(ChangedType)&{GenerateGroupColumns}, Table.ColumnNames(ChangedType)&{"Group"}), GroupedRows = Table.Group ( CreateTable, {"Group"}, { { "AllRows", (tbl)=> Table.AddColumn(Table.Skip(tbl,1), "Main", each tbl[Cat]{0}) } } ), RemovedOtherColumns = Table.SelectColumns(GroupedRows,{"AllRows"}), ExpandedAllRows = Table.ExpandTableColumn(RemovedOtherColumns, "AllRows", {"Cat", "Column1", "Group", "Main"}, {"Cat", "Column1", "Group", "Main"}) in ExpandedAllRowsCopy 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
Really nice but it hard for me to understand and apply for work ... thankyou
Hello utgqw
I totally agree. The thing is... easy transformation can be made by using the gui. More complex ones you have to write your code on you own and therefore also not easy to apply. What you can try with my code is to replace my datasource steps with your query. Connect them by changing the step-names in my code and maybe also changing column names in my code.
Hope this helps a little bit
Jimmy