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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
rubanjbe
Regular Visitor

Create visit number for each customer

Hi,

I would really appriciate anyone who could help me figure out a power query/dax to generate visit number for each customer.  I have all the dates a customer visited in ascending order. I now need to create the visit_number column as shown

 

 

visit number.JPG

 

 

Thanks,

Ruban

 
2 ACCEPTED SOLUTIONS
artemus
Employee
Employee

You can use a query to do this like (replace Table with your table):

 

let
Source = Table,
#"Grouped Rows" = Table.Group(Source, {"Customer_Id"}, {{"Rows", each _, type table [Date=date, CustomerId=number]}}),
Custom1 = Table.TransformColumns(#"Grouped Rows", {"Rows", each Table.AddIndexColumn(_, "Visit_Number", 1)}),
#"Expanded Rows" = Table.ExpandTableColumn(Custom1, "Rows", {"Date", "Visit_Number"}, {"Date", "Visit_Number"})
in
#"Expanded Rows"

P.S. this assumes the table is sorted by date. If not you will need to sort it that way first

 

View solution in original post

edhans
Super User
Super User

The following measure will do this in DAX if you want to do it that way. Just depends on where you need it. On a very large data set, the Power Query method will cause a longer refresh, but depending on your model, a DAX measure may be slower to the end user. Anything under a few hundred thousand records though I doubt anyone would know the difference.

 

Unlike the PQ method though, this doesn't require anything to be sorted first.

 

 

 

 

Visit Count = 
VAR ThisCustomer = MAX('Table'[Customer ID])
VAR ThisDate = MAX('Table'[Date])
RETURN
CALCULATE(
    COUNTROWS('Table'),
    FILTER(
        ALL('Table'),
        'Table'[Customer ID] = ThisCustomer && 'Table'[Date] <= ThisDate
    )
)

 

 

 

EDIT: This is slightly cleaner.

 

Visit Count = 
VAR ThisDate = MAX('Table'[Date])
RETURN
CALCULATE(
    COUNTROWS('Table'),
    FILTER(
        ALLEXCEPT('Table','Table'[Customer ID]),
        'Table'[Date] <= ThisDate
    )
)

 

 

2020-01-14 08_15_26-Untitled - Power BI Desktop.png



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

The following measure will do this in DAX if you want to do it that way. Just depends on where you need it. On a very large data set, the Power Query method will cause a longer refresh, but depending on your model, a DAX measure may be slower to the end user. Anything under a few hundred thousand records though I doubt anyone would know the difference.

 

Unlike the PQ method though, this doesn't require anything to be sorted first.

 

 

 

 

Visit Count = 
VAR ThisCustomer = MAX('Table'[Customer ID])
VAR ThisDate = MAX('Table'[Date])
RETURN
CALCULATE(
    COUNTROWS('Table'),
    FILTER(
        ALL('Table'),
        'Table'[Customer ID] = ThisCustomer && 'Table'[Date] <= ThisDate
    )
)

 

 

 

EDIT: This is slightly cleaner.

 

Visit Count = 
VAR ThisDate = MAX('Table'[Date])
RETURN
CALCULATE(
    COUNTROWS('Table'),
    FILTER(
        ALLEXCEPT('Table','Table'[Customer ID]),
        'Table'[Date] <= ThisDate
    )
)

 

 

2020-01-14 08_15_26-Untitled - Power BI Desktop.png



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Hoping to jump in on this. I have a similiar situation where I want to count the number of times a client has visited the office in total.

 

I used this to get the # of visits column:

Visit Count = 
VAR ThisDate = MAX('Table'[Date])
RETURN
CALCULATE(
    COUNTROWS('Table'),
    FILTER(
        ALLEXCEPT('Table','Table'[Customer ID]),
        'Table'[Date] <= ThisDate

 Then used this measure :

3 visits = calculate(

    COUNT('tabe'[customer id]),

    'table'[Visit Count] = 3

)/3

The result gets me the number i'm looking for, but its a bandaid solution because I can't use >= 4. Any thoughts? We're looking to graph the number of clinets who visited once,twice, three times, etc.

 

Thank you!

artemus
Employee
Employee

You can use a query to do this like (replace Table with your table):

 

let
Source = Table,
#"Grouped Rows" = Table.Group(Source, {"Customer_Id"}, {{"Rows", each _, type table [Date=date, CustomerId=number]}}),
Custom1 = Table.TransformColumns(#"Grouped Rows", {"Rows", each Table.AddIndexColumn(_, "Visit_Number", 1)}),
#"Expanded Rows" = Table.ExpandTableColumn(Custom1, "Rows", {"Date", "Visit_Number"}, {"Date", "Visit_Number"})
in
#"Expanded Rows"

P.S. this assumes the table is sorted by date. If not you will need to sort it that way first

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors