Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Incrementing index on multiple categories - Power Query M

Hi all, I have two fields in my data and want to be able to assign incremental values when the status for each ID is "Completed" using Power Query not DAX. Please see below Problem ID Status A In ...
  • Anonymous's avatar
    Anonymous
    5 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"
  • Jimmy801's avatar
    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