Forum Discussion
Incrementing index on multiple categories - Power Query M
- Anonymous5 years ago
Hi Anonymous
I am not sure if I made it too complicated, but it got what you want:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUfLMUwgoyk8vSi0uVorVIU3MOT+3ICe1JDWFBjpdsIi5YhFzwyGGagOGqlgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Status = _t]), #"Added Index" = Table.AddIndexColumn(Source, "SortIndex", 0, 1, Int64.Type), GroupToDiff = Table.Group(#"Added Index", {"ID", "Status"}, {{"Table", each _, type table }}), AddTest = Table.AddColumn(GroupToDiff, "Custom", each if [Status] = "Completed" then Table.AddIndexColumn([Table],"test",1,1) else Table.AddColumn([Table], "test", each 0)), SelectCustom = Table.SelectColumns(AddTest,{"Custom"}), #"Expanded Custom" = Table.ExpandTableColumn(SelectCustom, "Custom", {"ID", "Status", "SortIndex", "test"}, {"ID", "Status", "SortIndex", "test"}), #"Sorted Rows" = Table.Sort(#"Expanded Custom",{{"SortIndex", Order.Ascending}}), #"Grouped Rows" = Table.Group(#"Sorted Rows", {"ID"}, {{"allrows", each _, type table }}), #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", (OT)=> Table.AddColumn(OT[allrows],"new", (x)=>[a=List.Min( List.Select( Table.SelectColumns( Table.SelectRows(OT[allrows],(IT)=> IT[SortIndex]>=x[SortIndex]),"test")[test], each _ <>0)), b=List.Max( List.Select( Table.SelectColumns( Table.SelectRows(OT[allrows],(IT)=> IT[SortIndex]<=x[SortIndex]),"test")[test], each _ <>0)), c=if a <>null then a else if b<>null then b+1 else 0][c])), #"Added Custom1" = Table.AddColumn(#"Added Custom", "final", each Table.Group([Custom],{"ID","new"},{{"t", each _,type table}})), #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"final"}), #"Expanded final" = Table.ExpandTableColumn(#"Removed Other Columns", "final", {"t"}, {"t"}), #"Added Custom2" = Table.AddColumn(#"Expanded final", "Custom", each Table.AddIndexColumn([t],"Index",1,1)), #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom2",{"Custom"}), #"Expanded Custom1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Custom", {"ID", "Status", "SortIndex", "Index"}, {"ID", "Status", "SortIndex", "Index"}) in #"Expanded Custom1" - 5 years ago
Hello Anonymous
as I can see the index has to restart whenever ID and status together are changing. So you need to apply Table.Group with GroupKind.Local. This grouped tables then get a Index column starting with 1 by applying a Table.TransformColumns. By the way I was assuming that ID and status are two different columns and therefore I splittet your data into two different columns.
Here the complete code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTwzFMIKMpPL0otLlaK1SFWxDk/tyAntSQ1hWg9Tmh6nDFUuGCIuGKIuKGZgslH0RELAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}}), #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "ID", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"ID.1", "ID.2"}), #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"ID.1", type text}, {"ID.2", type text}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"ID.1", "ID"}, {"ID.2", "Status"}}), #"Grouped Rows" = Table.Group(#"Renamed Columns", {"ID", "Status"}, {{"AllRows", each _, type table [ID=text, Status=text]}}, GroupKind.Local), AddIndexToTable = Table.TransformColumns ( #"Grouped Rows", { { "AllRows", (tbl)=> Table.AddIndexColumn(tbl,"Index", 1) } } ), #"Expanded AllRows" = Table.ExpandTableColumn(AddIndexToTable, "AllRows", {"Index"}, {"Index"}) in #"Expanded AllRows"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
See this code Anonymous
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTwzFMIKMpPL0otLlaK1SFWxDk/tyAntSQ1hWg9Tmh6nDFUuGCIuGKIuKGZgslH0RELAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID Status" = _t]),
#"Grouped Rows" = Table.Group(Source, {"ID Status"}, {{"All Rows", each Table.AddIndexColumn(_, "Index", 1, 1), type table [ID Status=nullable text, Index = Int64.Type]}}),
#"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"Index"}, {"Index"}),
#"Added Custom" = Table.AddColumn(#"Expanded All Rows", "New ID Status", each [ID Status] & " " & Text.From([Index]), Text.Type),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"New ID Status"})
in
#"Removed Other Columns"
It turns this:
into this:
I could have done this in fewer steps with more manual code but it would have been more difficult to follow. Here you can see in each step what I am doing. The key is the Group Rows step. I manually edited the code to add an index within each grouping. Then I expanded that column out, merged the columns and returned the above result.
Hey Anonymous - I'm just curious what I missed in your original post that my code isn't doing for you.
- Anonymous5 years agoNot applicable
Hi edhans
To my understanding, you are counting "In progress" and "Completed" seperately. Have a look at the sample data, my understanding is a loop, counting from In progress to Completed then from In progress to Completed for each ID. I know I made it too complicated, hope you have a better solution.