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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
puru85
Helper II
Helper II

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 

1 ACCEPTED SOLUTION
TomMartens
Super User
Super User

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-ZZu3YrUMBoBjOk...

 

The table "RowIndexInGroupRankDense" looks like this:

TomMartens_0-1715539039026.png

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:

image.png 

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:

image.png

Hopefully, this provides what you are looking for.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

2 REPLIES 2
puru85
Helper II
Helper II

Awesome @TomMartens 🙂

TomMartens
Super User
Super User

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-ZZu3YrUMBoBjOk...

 

The table "RowIndexInGroupRankDense" looks like this:

TomMartens_0-1715539039026.png

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:

image.png 

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:

image.png

Hopefully, this provides what you are looking for.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.