cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
rrafferty37 Frequent Visitor
Frequent Visitor

Count of Customers with greater than "n" sales on a rolling 12 month basis

Hello,

 

This is what my data table looks like (the pertitent part):

 

OrderLine #Customer CodeDateSale
0011A1/1/201825
0012A1/1/201850
0013A1/1/201825
0014A1/1/201875

 

This data is for a few thousand customers over the past 5+years.  

What I am looking to do is get a count of the customers over a certain amount of sales in the last rolling 12 month period (for example $5000 in the last 12 months). 

 

Additionally, I have a year/month graph that shows the rolling 12 months over time. I would like to have the customer data shown the same way so that I can have a bar/line combo graph showing both metrics.  

 

I am getting stuck on the count.

 

For the Rolling 12 Sales I have:

R12 M Sales =
CALCULATE (
    [Sales],
    DATESINPERIOD ( 'Date'[Date], 
EOMONTH ( MIN ( 'Date'[Date] ), 0 ), 
-12, MONTH 
)
)

And this works just fine.  

 

Thanks!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

Hi,

Create a Calendar Table and build a relationship from the Date column of your Data Table to the Date column of your Calendar Table.  In the Calendar Table, extract the Year and Month via these calculated column formulas

Year = Year(Calendar[Date])

Month = FORMAT(Calendar[Date],"mmmm")

To your visual, drag Year and Month from the Calendar Table.  Write this measure

Measure = COUNTROWS(FILTER(SUMMARIZE(CALCULATETABLE(VALUES(Data[Customer Code]),DATESBETWEEN(Calendar[Date],EDATE(MIN(Calendar[Date]),-11),MAX(Calendar[Date]))),[Customer Code],"ABCD",CALCULATE(SUM(Data[Sale]),DATESBETWEEN(Calendar[Date],EDATE(MIN(Calendar[Date]),-11),MAX(Calendar[Date])))),[ABCD]>5000))

This measure should give a count of customers every month who have in the 12 months ended that month given you business exceeding $5000.

Hope this helps.

 

8 REPLIES 8
d_gosbell New Contributor
New Contributor

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

Are you just after a count of the distinct customer codes?

 

Would something like the following work:

R12 M Customers =
CALCULATE (
    DISTINCTCOUNT( Table1[CustomerCode] ),
    DATESINPERIOD ( 'Date'[Date], 
    EOMONTH ( MIN ( 'Date'[Date] ), 0 ), 
    -12, MONTH 
    )
)
ChrisHaas Established Member
Established Member

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

@rrafferty37 , try this:

[# Customers above R12 Sales Threshold] =
// Change this value as needed
VAR Threshold = 5000
VAR Rolling12ByCustomer =
	ADDCOLUMNS(
		VALUES(FactTable[Customer Code])
		,"Rolling 12 Sales", [R12 M Sales]
	)
VAR CustomersOverThreshold = 
	FILTER(
		Rolling12ByCustomer
		,[Rolling 12 Sales] >= Threshold
	)
VAR Result =
	COUNTROWS(CustomersOverThreshold)
RETURN
Result

1) Build a temporary table that computes the Rolling 12 sales for each customer (the date in the current filter context will provide the proper rolling 12 months).

 

2) Filter that temporary table to only the those customers that have sales >= threshold (set as a variable so that you can adjust it, or call it from a measure)

 

3) Count the number of rows in the filtered table

Super User
Super User

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

Hi,

Create a Calendar Table and build a relationship from the Date column of your Data Table to the Date column of your Calendar Table.  In the Calendar Table, extract the Year and Month via these calculated column formulas

Year = Year(Calendar[Date])

Month = FORMAT(Calendar[Date],"mmmm")

To your visual, drag Year and Month from the Calendar Table.  Write this measure

Measure = COUNTROWS(FILTER(SUMMARIZE(CALCULATETABLE(VALUES(Data[Customer Code]),DATESBETWEEN(Calendar[Date],EDATE(MIN(Calendar[Date]),-11),MAX(Calendar[Date]))),[Customer Code],"ABCD",CALCULATE(SUM(Data[Sale]),DATESBETWEEN(Calendar[Date],EDATE(MIN(Calendar[Date]),-11),MAX(Calendar[Date])))),[ABCD]>5000))

This measure should give a count of customers every month who have in the 12 months ended that month given you business exceeding $5000.

Hope this helps.

 

ChrisHaas Established Member
Established Member

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

@Ashish_Mathur , while this will likely still work, it is not optimal.

 

SUMMARIZE() has very poor performance when additional columns are added.  It's much better to use

 

 

ADDCOLUMNS(
	SUMMARIZE(
		Table,
		Column1,
		etc.
	),
	"Column to add", <expression>
)

 

Or, use SUMMARIZECOLUMNS()

 

SQLBI has an extensive article on this topic:

https://www.sqlbi.com/articles/introducing-summarizecolumns/

 

rrafferty37 Frequent Visitor
Frequent Visitor

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

So this almost works.  For each particular year/month, if the customer had no sales in that given month they would not be counted (even though the R12 M Sales was over the threshold).  

R12.JPG

 

This was filtered on the most recent month completed (April 2019).  Still new to DAX, but it seems like at the month level is where the addcolumns expression starts, and if there are no sales for that month the customer is not added.  I am not sure how to remedy this, but I despite this not serving my exact needs I have learned a lot by going through this post line by line. Thank you!!

 

 

rrafferty37 Frequent Visitor
Frequent Visitor

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

This function worked exactly as I needed it to! @ChrisHaas I will definitely be reading the article you linked to optimize this function. Thank you both so much, I have learned a great deal and have added a very useful component to my dashboard. Cheers!

rrafferty37 Frequent Visitor
Frequent Visitor

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

Hello,

 

As things naturally progressed from using this solution, I have been trying to modify this to segment my customer base. 

 

COUNTROWS (
    FILTER (
        SUMMARIZE (
            CALCULATETABLE (
                VALUES ( Sales[CustomerCode] ),
                DATESBETWEEN (
                    'Date'[Date],
                    EDATE ( MIN ( 'Date'[Date] ), -11 ),
                    MAX ( 'Date'[Date] )
                )
            ),
            Sales[CustomerCode],
            "Metric", CALCULATE (
                SUM ( Sales[Sales] ),
                DATESBETWEEN (
                    'Date'[Date],
                    EDATE ( MIN ( 'Date'[Date] ), -11 ),
                    MAX ( 'Date'[Date] )
                )
            )
        ),
        [Metric] > 2000
    )
)

Using the above code (thank you!) I replicated it a number of times to count how many customers are in various segments ([Metric] >500/1000/200/400/8000/10000, etc.)

I want to be able to see the value of sales within each of these segments. 

 

I tried to SUM intially by summing on my sales column and passing the filter expression in the above code but the output was not what I was looking for. 

 

If I understand the above, a summary table consiting of customer codes within a rolling 12 month period is created.  For each of these customer codes the sales over the same time period (written again within the sum expresioon inside the summarize expression) is calculated under the column name "Metric".  The countrows expression simply counts any row where Metric is greater than whatever is specified. I want to just sum the Metric column using the same parameters and seem to be hitting a wall. 

 

Thanks!

Super User
Super User

Re: Count of Customers with greater than "n" sales on a rolling 12 month basis

Thank you for sharing that @ChrisHaas.