Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
ahutchins
Frequent Visitor

Increment index column based on another specific value

I have a dataset that includes an IncidentNumber, Responder, and ActionCode. Each responder is supposed to go through four ActionCodes, which I'll call Action 1-4, but some actions are skipped. Each Responder could repeat Actions 1-4 in a given IncidentNumber. See sample below.

 

What I would like to do is to add a column that I'll call InstanceNum that increments whenever Action 1 comes up. So far, I have successfully grouped by Incident and Responder using the All Rows operation. I can add an index column, but I can't figure out how to increment the number when Action 1 comes up.

 

Any help is appreciated.

 

Sample

Incident  Responder  Action  

InstanceNum

(what I want to add)

1A21
1A31
1A41
1A12
1A22
1A32
1B11
1B21
1B31

 

 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiOlWB0EzxiFZ4LCM0Th4dOHS84JxRQnFJVOOFQaoZhphGKmEYrLkFTGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Incident = _t, Responder = _t, Action = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Incident", "Responder"}, {{"Temp", each _, type table [Incident=number, Responder=text, Action=number]}}),
    //Function Start
    fxProcessNext=(Tbl)=>
        let
            #"Added Index" = Table.AddIndexColumn(Tbl, "Index", 0, 1, Int64.Type),
            #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try if Tbl[Action]{[Index]-1}>[Action] then "Y" else "N" otherwise "Y"),
            #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each List.Count(List.Select(List.FirstN(#"Added Custom"[Custom],[Index]+1), each _ = "Y"))),
            #"Renamed Columns" = Table.RenameColumns(#"Added Custom1",{{"Custom.1", "InstanceNum"}})
        in
            #"Renamed Columns",
    //Function End
    #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "fxProcessNext", each fxProcessNext([Temp])),
    #"Expanded fxProcessNext" = Table.ExpandTableColumn(#"Invoked Custom Function", "fxProcessNext", {"Action", "InstanceNum"}, {"Action", "InstanceNum"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded fxProcessNext",{"Temp"})
in
    #"Removed Columns"

 

View solution in original post

4 REPLIES 4
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use this Excel file having the Query and output

https://1drv.ms/x/s!Akd5y6ruJhvhuTOMPxSd1vSL2xyh?e=pSZpcs 

Thank you so much! As it turns out, my version of Office (16) is too old. Fortunately, I'm a beta tester for 365 and it works great on that machine.

Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiOlWB0EzxiFZ4LCM0Th4dOHS84JxRQnFJVOOFQaoZhphGKmEYrLkFTGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Incident = _t, Responder = _t, Action = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Incident", "Responder"}, {{"Temp", each _, type table [Incident=number, Responder=text, Action=number]}}),
    //Function Start
    fxProcessNext=(Tbl)=>
        let
            #"Added Index" = Table.AddIndexColumn(Tbl, "Index", 0, 1, Int64.Type),
            #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try if Tbl[Action]{[Index]-1}>[Action] then "Y" else "N" otherwise "Y"),
            #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each List.Count(List.Select(List.FirstN(#"Added Custom"[Custom],[Index]+1), each _ = "Y"))),
            #"Renamed Columns" = Table.RenameColumns(#"Added Custom1",{{"Custom.1", "InstanceNum"}})
        in
            #"Renamed Columns",
    //Function End
    #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "fxProcessNext", each fxProcessNext([Temp])),
    #"Expanded fxProcessNext" = Table.ExpandTableColumn(#"Invoked Custom Function", "fxProcessNext", {"Action", "InstanceNum"}, {"Action", "InstanceNum"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded fxProcessNext",{"Temp"})
in
    #"Removed Columns"

 

Thank you for your help. When I try the blank query I get this error:

 

"Expression.Error: 5 arguments were passed to function which expects between 2 and 4.

Details:

  Pattern=

  Arguments=List"

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.