Forum Discussion

puru85's avatar
puru85
Icon for Helper II rankHelper II
2 years ago
Solved

Power Query to Create Custom "Visit_Number" Column

Hello Experts,

Below is my data:

DateCustomer_IDInvoice
1/1/2022A1IN-01
1/13/2022A2IN-02
1/13/2022A2IN-03
2/5/2022A1IN-04
7/8/2022A2IN-05
8/8/2022A3IN-06
8/9/2022A3IN-07
8/15/2022A4IN-08
8/15/2022A4IN-09
9/2/2022A2IN-10
9/2/2022A2IN-11
9/10/2022A4IN-12

 

My Expected result is:

Customer_IDVisit_DateInvoiceVisit_Number
A11/1/2022IN-011
A12/5/2022IN-042
A21/13/2022IN-021
A21/13/2022IN-031
A27/8/2022IN-052
A29/2/2022IN-103
A29/2/2022IN-113
A38/8/2022IN-061
A38/9/2022IN-072
A48/15/2022IN-081
A48/15/2022IN-091
A49/10/2022IN-122


Requirement details are:
1) To achive in Powerquery ( Backend)
2)  To create a custom column "Visit_Number"  
3) invoices on the same date for the same Customer_iD are considered a single visit, and each new date with an invoice increments the visit count.

4) To understand more clear on the requirement, Please look at the cells marked in red on My Expected result.

Please help me. 

Thank you 

  • Hey puru85 ,

     

    here, you will find a pbix that contains a solution that creates the below table: https://tommartens-my.sharepoint.com/:u:/g/personal/tom_minceddata_com/EYnAvS_rOA1Ctd-ZZu3YrUMBoBjOk4vGpecvPIw9-GYQzA?e=zfMfV3

     

    The table "RowIndexInGroupRankDense" looks like this:

    The magic is done by the function Table.AddRankColumn

    and a little trick.

    The trick is explained by the two queries

    1. 1-RowIndexInGroup-Grouping
    2. 2-RowIndexInGroup-Formatted

    This might be helpful because after "adjusting" the grouping function the grouping dialog can not be rendered any longer:

    The trick, start with grouping the rows by customer and tweaking the grouping. The next screenshot shows the grouping:

     

    The code snippet below shows the M-code after the grouping but formatted:

     

     

    #"Grouped Rows" = 
            Table.Group(#"Changed Type", {"Customer_ID"}, 
                {
                    {"AllRows", 
                        each _ 
                        , type table [Date=nullable date, Customer_ID=nullable text, Invoice=nullable text]
                    }
                }
            )

     

     

     

    Then the line

    each _

    has to be adapted with the aggregation function, e.g.:  Table.AddRankColumn

    and the row with table definition must be removed.

    Finally, expand the Table:

    Hopefully, this provides what you are looking for.

     

    Regards,

    Tom

2 Replies

  • Hey puru85 ,

     

    here, you will find a pbix that contains a solution that creates the below table: https://tommartens-my.sharepoint.com/:u:/g/personal/tom_minceddata_com/EYnAvS_rOA1Ctd-ZZu3YrUMBoBjOk4vGpecvPIw9-GYQzA?e=zfMfV3

     

    The table "RowIndexInGroupRankDense" looks like this:

    The magic is done by the function Table.AddRankColumn

    and a little trick.

    The trick is explained by the two queries

    1. 1-RowIndexInGroup-Grouping
    2. 2-RowIndexInGroup-Formatted

    This might be helpful because after "adjusting" the grouping function the grouping dialog can not be rendered any longer:

    The trick, start with grouping the rows by customer and tweaking the grouping. The next screenshot shows the grouping:

     

    The code snippet below shows the M-code after the grouping but formatted:

     

     

    #"Grouped Rows" = 
            Table.Group(#"Changed Type", {"Customer_ID"}, 
                {
                    {"AllRows", 
                        each _ 
                        , type table [Date=nullable date, Customer_ID=nullable text, Invoice=nullable text]
                    }
                }
            )

     

     

     

    Then the line

    each _

    has to be adapted with the aggregation function, e.g.:  Table.AddRankColumn

    and the row with table definition must be removed.

    Finally, expand the Table:

    Hopefully, this provides what you are looking for.

     

    Regards,

    Tom