indexingduplicatespowerquery

Add an Index for Duplicate Values using Power Query


I have a dataset that has duplicate Invoice Number values.

I need help generating an index column ("Occurrences") to count the number of occurrences of the "InvoiceNo".

Any guidance would be greatly appreciated.

InvoiceNo Occurrence
100011 1
100012 1
100013 1
100011 2
100011 3
100012 2
100014 1

I have tried using code suggested from a similar thread, but it is generating an error. The error says "We cannot convert the value 100011 to type list". However, I'm not sure that the syntax of the code is correct anyway. Appreciate any guidance that you can give. TIA.

let
    initialTable = Table.FromColumns(InvoiceNo, type table [Occurrence = text]),
    grouped = Table.Group(initialTable, "InvoiceNo", {{"toCombine", each Table.AddIndexColumn(_, "Occurrences", 1, 1), type table}}),
    combined = Table.Combine(grouped[toCombine])
in
    combined

Solution

  • As an alternative. Lets assume your table with invoice numbers is called "Table1", then this code should work.

    let
        Source =Excel.CurrentWorkbook(){[Name="Table1"]}[Content],  
        AddIndex =Table.AddIndexColumn(Source,"Index",0,1,Int64.Type),
        AddOccurrance =Table.AddColumn(
            
            AddIndex,
            "Occurrance",
            each Table.RowCount(
                let
                    Inv =[InvoiceNo],       
                    Idx =[Index]            
                in
                    Table.SelectRows(AddIndex,  
                        each [InvoiceNo] = Inv and [Index] <= Idx
                                                 
                                                 
                        )
            )
        ),
    
        RemoveCol =Table.RemoveColumns(AddOccurrance,{"Index"})                     
    in
        RemoveCol