Forum Discussion

ahutchins's avatar
ahutchins
Frequent Visitor
4 years ago
Solved

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 ...
  • Vijay_A_Verma's avatar
    4 years ago

    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"