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
Grygger
Frequent Visitor

Filter DistinctCount calculation to same Customer IDs found in other calculation, same cohort

I'm trying to identify the percentage of customers who purchased something in the last 1 month (based on the x axis) and compare vs. the previous 90 day period and filter down the two calculations to be of the same cohort.


What I'm struggling to do is filter down my previous 90 day query to contain ONLY the same CustomerIDs that were found purchasing in the last 30 days.

 

i.e. if we take a date, say July 1st, _Last1M_Customers will take a distinctcount of CustomerIDs between June 1st - July 1st. The _Last3M_Customers will look at March 1st - May 31st to get the previous 90 day period.

 

 

VAR _Last1M_Customers = CALCULATE(DISTINCTCOUNT(sales[CustomerID]), DATESINPERIOD(sales[Sale_Date], MIN(sales[Sale_Date]), -30, DAY))

VAR _Last3M_Customers = CALCULATE(DISTINCTCOUNT(sales[CustomerID]), DATESINPERIOD(sales[Sale_Date], DATEADD(FIRSTDATE(sales[Sale_Date]), -31, DAY), -91, DAY))

RETURN _Last1M_Customers/_Last3M_Customers

 

 

I'm more comfortable with SQL and what I'm essentially trying to do is add logic to the _Last3M_Customers that is something akin to

 

WHERE CustomerID IN (SELECT CustomerID FROM _Last1M_Customers)

 




4 REPLIES 4
Anonymous
Not applicable

@Grygger Maybe this helps:

VAR _Last1M_Customers = CALCULATE(DISTINCTCOUNT(sales[CustomerID]), DATESINPERIOD(sales[Sale_Date], MIN(sales[Sale_Date]), -30, DAY))

VAR _Last1M_Customers_List=SUMMARIZE(FILTER(sales,DATESINPERIOD(sales[Sale_Date], MIN(sales[Sale_Date]), -30, DAY)),sales[CustomerID])

VAR _Last3M_Customers = COUNTROWS(SUMMARIZE(FILTER(sales,DATESINPERIOD(sales[Sale_Date], DATEADD(FIRSTDATE(sales[Sale_Date]), -31, DAY), -91, DAY)&&_Last1M_Customers_List),sales[CustomerID]))

RETURN _Last1M_Customers/_Last3M_Customer

 

This definitely gets one step closer! My only challenge is that there can be duplicate customer IDs for multiple purchases, so that's why I've kept the DistinctCount() function throughout, I could be wrong but would this inflate the count with duplicate customer IDs?

Anonymous
Not applicable

DistinctCount counts unique cutomer ID, without duplicates. If you need count  unique [customer ID]&[purchase ID] then modify DAX a little bit:

VAR _Last1M_Customers = COUNTROWS(SUMMARIZE(FILTER(sales,DATESINPERIOD(sales[Sale_Date], MIN(sales[Sale_Date]), -30, DAY)),sales[CustomerID],sales[PurchaseID]))

VAR _Last1M_Customers_List=SUMMARIZE(FILTER(sales,DATESINPERIOD(sales[Sale_Date], MIN(sales[Sale_Date]), -30, DAY)),sales[CustomerID],sales[PurchaseID])

VAR _Last3M_Customers = COUNTROWS(SUMMARIZE(FILTER(sales,DATESINPERIOD(sales[Sale_Date], DATEADD(FIRSTDATE(sales[Sale_Date]), -31, DAY), -91, DAY)&&_Last1M_Customers_List),sales[CustomerID],sales[PurchaseID]))

RETURN _Last1M_Customers/_Last3M_Customer

No luck unfortunately, comes back with an error along the lines of expected a single value but received multiple values.

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.