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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Problem calculating retaining customers

Could anybody please explain why it doesn't work when I'm trying to identify if a new customer is retained. The logic I'm having is that if a customer have never made any purchase before current purchase, and will make a purchase in next 30 days, then this new customer is retained and will be marked as a retaining customer.

The DAX function looks like this:

 

Retaining Customers =
COUNTROWS (
FILTER (
ADDCOLUMNS (
VALUES ( Orders[会员ID] ),
"PreviousSales", CALCULATE (
COUNTROWS ( Orders ),
FILTER (
ALL( 'Dates' ),
'Dates'[Date] < MIN ( 'Dates'[Date] )
)
),
"SalesIn30Days", CALCULATE(
COUNTROWS ( Orders ),
FILTER (
ALL( 'Dates' ),
'Dates'[Date] < ( MIN ('Dates'[Date]) + 30) && 'Dates'[Date] > MIN('Dates'[Date]) )
)
),
[PreviousSales] = 0 && [SalesIn30Days] > 0
)
)
 
The previousSales part works fine - it correctly identifies all new customers, but the second part gives the wrong answer. It always returns all customers.
Can anybody help?
 
 
UPDATED:
The sample table looks like this
批注 2020-05-12 111842.png

 

And Here is the sample data and calculation I have

Sample Data and Calculation 

 

I'm hoping to have retaining customers = 2 for January because customer 2 and 3 are new customers and make another purchase within 30 days of their first purchase. Customer 1 is not a new customer for January so I won't be counted and customer 4 doesn't make any purchase after first purchase, so it won't be counted as well.

But my calculation gives me retaining customers = 3 and it seems to be problem with customer 4

9 REPLIES 9
amitchandak
Super User
Super User

@Anonymous , You can two new columns like this that can help you to get your calculations, these are in the order table

 

First purchase = minx(filter(Order,[CustomerID] = earlier([CustomerID])),[Order Date])
last purchase = maxx(filter(Order,[CustomerID] = earlier([CustomerID]) && [Order Date] < earlier([Order Date])),[Order Date])

Anonymous
Not applicable

@amitchandak Thank you for replying!

 

The dataset is really large so using ealier function is a bit slow, that's why I'm trying to use measure to calculate this number instead of adding calculated columns.

Another reason I want to use a measure is that I need to drill down through different levels, so it's impossible to do this by adding calculated columns.

@Anonymous , Try like this.  But use dimensions now

 

First purchase = minx(filter(all(Customer),Customer[CustomerID] = max(Customer[CustomerID])),[Order Date])

last purchase = calculate(maxx(filter(all(Customer),[CustomerID] = max(Customer[CustomerID])),[Order Date]),,filter( all(Date),Date[Date] < Max(Date[Date]))

Anonymous
Not applicable

@amitchandak Thank you so much!

 

The thing is that I only care about first and second purchase instead of last purchase.

If a customer is new (make first purchase) and then this same customer make another purchase within 30 days of his first purchase, this customer will be considered as a retaining customer. This customer might continues making further purchases but those won't be considered when calculating number of retaining customers.

 

Actually I already have calculated columns added, and using calculated columns give me the correct answer. But like what I've mentioned ealier, I want to use measures so I can drill down through the data when doing dashboard and report.

@Anonymous here are the measures which can get you first purchase date and the very next purchase date after first purchase and you can take it from there

 

First Purchase = CALCULATE ( MIN ( Orders[Date] ), ALLEXCEPT ( Orders, Orders[customer ID] ) )

Next Purchase = 
VAR __firsPurchase = [First Purchase] 
RETURN CALCULATE ( MIN ( Orders[Date] ), ALLEXCEPT ( ORders, Orders[customer ID] ), Dates[Date] > __firsPurchase )

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Thank you @amitchandak

 

Now I have

 

 

Retaining Customers = 
COUNTROWS(
    FILTER(
        VALUES(Orders[会员ID]),
        CALCULATE(
            COUNTROWS(Orders),
            FILTER(
                ALLEXCEPT(Orders, Orders[会员ID]),
                DATEDIFF([First Order Date], [Second Order Date],DAY) <= [Churn Time Value]
            )
        )
    )
) 

 

 

 

This gives number of retaining customers. But if a customer keep making purchases in different months, this customer will be counted as retaining customer for every month he places orders.

How am I suppose to change my function so a customer will only be counted once in his first purchase month?

Anonymous
Not applicable

Hi @v-diye-msft and @parry2k 

Here is the sample data and calculation I have

 

Sample Data and Calculation 

 

I'm hoping to have retaining customers = 2 for January because customer 2 and 3 are new customers and make another purchase within 30 days of their first purchase. Customer 1 is not a new customer for January so I won't be counted and customer 4 doesn't make any purchase after first purchase, so it won't be counted as well.

But my calculation gives me retaining customers = 3 and it seems to be problem with customer 4

v-diye-msft
Community Support
Community Support

Hi @Anonymous 

 

you might consider creating pbix file that will contain some sample data (remove the confidential info), upload the pbix to onedrive for business and share the link to the file. Please do not forget to describe the expected results based on this sample data.

 

 

Community Support Team _ Dina Ye
If this post helps, then please consider Accept it as the solution to help the other members find it more
quickly.
parry2k
Super User
Super User

@Anonymous too many calculations going on, it will be easier if you put these calculations in separate measures and identify what is not working and what should be the expected result. It will help to debug and then if you want, you can combine all that in one, also share sample data, relationship and expected output.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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