The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
I'd like to ask for some input on something.
I recently had an encounter where I was asked to create DAX measures for the following:
I came up with the following measures:
Customers Returning Within 90 Days of First Purchase:=COUNTROWS(
FILTER(
Customers,
CALCULATE(
DISTINCTCOUNT(Sales[SalesOrderNumber]),
FILTER(
ALL('Calendar'),
Calendar[Date] < MAX('Calendar'[Date])
)
) > 1 &&
DATEDIFF(
Customers[DateFirstPurchase],
CALCULATE(
MIN(Sales[OrderDate]),
Sales[CustomerKey] = EARLIER(Customers[CustomerKey]),
Sales[OrderDate] > EARLIER(Customers[DateFirstPurchase])
),
DAY
) <= 90
)
)
Customers Returning Within Following Three Months:=COUNTROWS(
FILTER(
Customers,
CALCULATE(
DISTINCTCOUNT(Sales[SalesOrderNumber]),
FILTER(
ALL(Calendar),
Calendar[Date] >= EOMONTH(EARLIER(Customers[DateFirstPurchase]),0) + 1 &&
Calendar[Date] <= EOMONTH(EARLIER(Customers[DateFirstPurchase]) ,3)
)
)
)
)
Further on in the discussion, I was asked to achieve the exact same result for the measure "Customers Returning Within 90 Days of First Purchase" without filtering the Sales table at all. I will completely admit that I am stumped on how to accomplish this. Could anyone please provide some guidance on how to this can be done?
Solved! Go to Solution.
So I am not one to give up on things so last weekend I sat down and figured this out:
Customers Returning Within 90 Days of First Purchase Redux:=CALCULATE(
[Customer Count],
FILTER(
Customers,
CALCULATE(
DISTINCTCOUNT(Sales[SalesOrderNumber]),
FILTER(
ALL(Calendar),
Calendar[Date] > EARLIER(Customers[DateFirstPurchase]) &&
Calendar[Date] <= EARLIER(Customers[DateFirstPurchase]) + 90
)
)
)
)
Customers Returning Within Following Three Months Redux:=CALCULATE(
[Customer Count],
FILTER(
Customers,
CALCULATE(
DISTINCTCOUNT(Sales[SalesOrderNumber]),
FILTER(
ALL(Calendar),
Calendar[Date] >= EOMONTH(EARLIER(Customers[DateFirstPurchase]),0) + 1 &&
Calendar[Date] <= EOMONTH(EARLIER(Customers[DateFirstPurchase]) ,3)
)
)
)
)
My original solution for Customers Returning Within 90 Days of First Purchase clearly demonstrates that while there are lots of ways to solve something but not all of them are good. This solution is simple and straightforward.
Just check a rank that can be created on the date within the customer. That will give you the order and based on that you can calculate your next or previous date.
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-3-of-3-the-finale
Hi -
Thanks for replying. I'm not getting it. The biggest thing I'm not understanding is how would you limit sales transactions to only those of a specified customer without filtering?
Secondarily and far less important, if I rank sales transactions based on date, the first one would be ranked 1, the second one would be 2, etc. Whether the second one happened within 90 days won't be reflected in a ranking...
I'm confused! 🙂
So I am not one to give up on things so last weekend I sat down and figured this out:
Customers Returning Within 90 Days of First Purchase Redux:=CALCULATE(
[Customer Count],
FILTER(
Customers,
CALCULATE(
DISTINCTCOUNT(Sales[SalesOrderNumber]),
FILTER(
ALL(Calendar),
Calendar[Date] > EARLIER(Customers[DateFirstPurchase]) &&
Calendar[Date] <= EARLIER(Customers[DateFirstPurchase]) + 90
)
)
)
)
Customers Returning Within Following Three Months Redux:=CALCULATE(
[Customer Count],
FILTER(
Customers,
CALCULATE(
DISTINCTCOUNT(Sales[SalesOrderNumber]),
FILTER(
ALL(Calendar),
Calendar[Date] >= EOMONTH(EARLIER(Customers[DateFirstPurchase]),0) + 1 &&
Calendar[Date] <= EOMONTH(EARLIER(Customers[DateFirstPurchase]) ,3)
)
)
)
)
My original solution for Customers Returning Within 90 Days of First Purchase clearly demonstrates that while there are lots of ways to solve something but not all of them are good. This solution is simple and straightforward.
Did this solution work for you?
@thatnerdguy I would approach this in a completely different way now than I did four years ago.
Good luck with the interview! 😉