Forum Discussion
Calculating Previous Sales Date
I have an interesting issue I need help figuring out.
I have a table that is a list of orders (columns like order date, name, product, price, etc).
I have been requested to create a measure that displays the number of new or returning customers from this data (new/returning being a customer who made a purchase that had not made a purchase in the last 3 years).
My thoughts for doing this would be to take the table I have and add a column that shows the previous date that customer made a purchase, if it was in the last 3 years. So for example, if customer A has an order date of 12/15/2023 and 5/27/2022, the order date of this table would be 12/15/2023, as it already is. There would be a new column (called last order date) that displays 5/27/2022. If the customer in the order data does not have a previous order date in the last 3 years, then the column will just display a null.
To continue the example, lets say customer B has an order date of 1/1/2023, and 1/1/2009. In this example order date would display 1/1/2023, and last order date would just display "null" (it was longer than 3 years ago).
With this I could just take a distinct count of customer numbers where the last order date is null, since that signifies they either have never ordered before, or it was more than 3 years ago.
Can someone guide me through how to create this custom column that will display the previous date a customer ordered, and null if its longer than 3 years from the current day?
Thanks!
I would suggest the following DAX measure:
New Customers - 3 Years =VAR N =CALCULATE(DISTINCTCOUNT(Orders[Customer ID]),FILTER (Orders,Orders[Order Date]= CALCULATE (MIN ( Orders[Order Date] ),ALLEXCEPT(Orders, Orders[Customer ID]))&&DATEDIFF(Orders[Order Date],TODAY(),YEAR)<3))VAR R = DISTINCTCOUNT(Orders[Customer ID])-NRETURNNSimply return 'R' for returning customers.
6 Replies
- kb_ccFrequent Visitor
I would suggest the following DAX measure:
New Customers - 3 Years =VAR N =CALCULATE(DISTINCTCOUNT(Orders[Customer ID]),FILTER (Orders,Orders[Order Date]= CALCULATE (MIN ( Orders[Order Date] ),ALLEXCEPT(Orders, Orders[Customer ID]))&&DATEDIFF(Orders[Order Date],TODAY(),YEAR)<3))VAR R = DISTINCTCOUNT(Orders[Customer ID])-NRETURNNSimply return 'R' for returning customers.- AppleManHelper III
I will try this out. Looking at this code, variable N should be returning the number of customers that are not returning customers, correct?
So variable R will just find the number of distinct customers and then remove the ones that are not return customers?- kb_ccFrequent Visitor
That's correct! Variable 'N' returns the number of customers who made their first purchase within the past 3 years.
Variable 'R' returns any customers that do not meet this criteria.