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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
leilei787
Helper II
Helper II

An odd count question

Hi All:

Happy Friday!

 

i have an odd question. here is my table below. i want to create a column that count how many times a customer show up

 

For example, customer A purchased 18 units on Jan 1st 2021, so that would be 1, and then customer A purchased again on 5/6/2021, so that count as 2 and Customer A showed up again in Sep, so that's 3. ( The result column is what i am looking for).....anyone could help me? thank you!!

 

Customer IDOrder DatePurchase QtyResult
A1/1/2021181
B1/15/202151
C3/4/2021121
D4/5/2021221
A5/6/2021162
B3/2/202152
C5/7/2021122
A9/4/202193
D8/1/202152
B10/8/202173
1 ACCEPTED SOLUTION

Hi @leilei787 
Actually now it is much easier. Check out the updated file https://www.dropbox.com/t/gnQXLqmohiDQaOqd
You just need two measures

 

First Purchase Date = CALCULATE ( MIN (Data[Order Date] ), Data[Amount] > 0 )
Last Purchase Date = CALCULATE ( MAX (Data[Order Date] ), Data[Amount] > 0 )

 

 The CALCULATE is only required to restrict the calculation on real sales transactions otherwise a simple MAX/MIN would be enough and the filter context will take care of the rest. You just need to place the Customer at the rows of the matrix visual and the Products ( either two or more) on the columns. Then place the two measures at the values, and that's it.
Untitled.png

View solution in original post

8 REPLIES 8
tamerj1
Super User
Super User

Hi @leilei787 
Here is the sample file with the solution https://www.dropbox.com/t/fvtEBetKhYgSu2a3
The calculated column formula is

 

 

Result = 
"Order " &
RANKX ( 
    FILTER (
        Data,
        Data[Customer ID] = EARLIER ( Data[Customer ID] )
    ),
    Data[Order Date],,
    ASC,
    Dense
)

 

1.png

Hi Tamerj1

 

Appreciate for your solution. two additional question: how to setup a condition in your formula that rank the order only if amount >0....so if customer A placed a Demo order, because the value =$0, we do not count as an order....it has to be a real order with amount >$0

 

Another question is...i want to create another column that identify first order and last order.....so in the example you provide, the 1st order would be first order.....the 3rd order would be the last order....but a customer would place many orders as time go on....it is easy to capture the first, but how to identify the last one? 

 

thank you!!

 

@leilei787 

Regarding your first question:

Result = 
IF (
    Data[Amount] > 0,
    RANKX ( 
        FILTER (
            Data,
            Data[Customer ID] = EARLIER ( Data[Customer ID] )
        ),
        Data[Order Date],,
        ASC,
        Dense
    )
)

Regarding yiu 2nd question. Please clarify. Do you want to flag the last sale and blank out othes?

 

Last Sale = 
VAR LastDate =
IF (
    Data[Amount] > 0,
    MAXX ( 
        FILTER (
            Data,
            Data[Customer ID] = EARLIER ( Data[Customer ID] )
        ),
        Data[Order Date]
    )
)
RETURN
IF ( Data[Order Date] = LastDate, TRUE )

 

Hi Tamerj1

 

again, thank you for your help! so just to clear what i am looking for. here is the story, we have product A and B. (A is the system, B is an accessory). Customers have to purchase product A first, then they can purchase B.....we are trying to see first and Last purchase date for A and B in certain period, then we can calculate the attach rate % for that period. 

 

this is the end result: (a summary table in Power BI to show each customer, what is first purchase date for A and B, what is the last purchase date for A and B.

 

Customer IDfirst purchase date(Product A)last purchase date(Product A)first purchase date (Product B)last purchase date (Product B)
1001/1/20216/23/20213/6/20218/17/2021
2002/4/20218/6/20212/25/20214/10/2021

 

i think in order to show this, we need calculate column. here is the new sample data

The last 3 columns ( Result, first order date, last order date are what i am hoping to see)

 

Customer IDOrder DateProductPurchase QtyResultFirst Order DateLast Order Date
1001/1/2021A18First Time Order of A1/1/2021 
1001/1/2021C10   
2002/4/2021A12First Time Order of A2/4/2021 
2002/25/2021B22First Time Order of B2/25/2021 
1003/6/2021B16First time order of B3/6/2021 
1004/7/2021A12   
2004/10/2021B9Last Time Order of B 4/10/2021
1005/8/2021A7   
1005/17/2021B2   
1006/23/2021A366Last Time Order of A 6/23/2021
1006/19/2021B89   
1007/25/2021B9   
2008/6/2021A34Last Time Order of A 8/6/2021
1008/17/2021B89Last time order of B 8/17/2021

 

i think the formula you provide works partially...we can blank 2nd, or 3rd order...only identify 1st and 2nd....please let me know if you could modify last formula. i do appreciate your time!

Hi @leilei787 
Actually now it is much easier. Check out the updated file https://www.dropbox.com/t/gnQXLqmohiDQaOqd
You just need two measures

 

First Purchase Date = CALCULATE ( MIN (Data[Order Date] ), Data[Amount] > 0 )
Last Purchase Date = CALCULATE ( MAX (Data[Order Date] ), Data[Amount] > 0 )

 

 The CALCULATE is only required to restrict the calculation on real sales transactions otherwise a simple MAX/MIN would be enough and the filter context will take care of the rest. You just need to place the Customer at the rows of the matrix visual and the Products ( either two or more) on the columns. Then place the two measures at the values, and that's it.
Untitled.png

Greg_Deckler
Community Champion
Community Champion

@leilei787 Try:

QtyResult Column = 
  VAR __Customer = [Customer ID]
  VAR __Date = [Order Date]
RETURN
  COUNTROWS(FILTER('Table',[Customer ID] = __Customer && [Order Date] <= __Date))


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

thanks Greg! The formula works! However, i need to revise my question after deeper look at the project. so here is what i am looking for ( see below table).

 

i have a list of customers with order date ( some order dates are the same because customer order different product). i am trying to identify a customer purchased a product the 1st time or 2nd time.

 

For example, Customer A ordered both product X and Z the 1st time on 1/1/2021, and then Customer A purchased product X and Z again ( 2nd order) on 5/6/2021, and 3rd order on product X in Sep

 

Order # can be number 1,2,3 instead of 1st, 2nd order

 

Thank you again!

 

Customer IDOrder DateProductPurchase QtyOrder #
A1/1/2021x181st order
A1/1/2021z51st order
B1/15/2021x51st order
C3/4/2021x121st order
C3/4/2021y421st order
D4/5/2021X221st order
A5/6/2021x162nd order
A5/6/2021Z72nd order
B3/2/2021X52nd order
C5/7/2021Y122nd order
A9/4/2021X93rd order
D8/1/2021X52nd order
B10/8/2021X73rd order

 

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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