Forum Discussion
TopN Customers Rolling 12 months
- 8 years ago
This is an interesting problem so I gave it a try. Let me know if it doesn't work for you as I haven't tested these as I don't really have a good sample data set to work off of. You'll have to translate the column names I used for your model.
Here is what I came up with for 4 measures assuming you have YearMonth on rows like you have in your result screenshots.
Total Sales = SUM ( FactSales[SalesAmount] )
Then for total sales last 12 Mo
Total Sales Last 12MO = VAR _last12MO = DATESBETWEEN ( DimDate[Datekey], NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( DimDate[Datekey] ) ) ), LASTDATE ( DimDate[Datekey] ) ) RETURN CALCULATE ( [Total Sales], _last12MO )then for rolling top 20 sales last 12 months:
Total Sales Last 12 MO Rolling Top20 = VAR _last12MO = DATESBETWEEN ( DimDate[Datekey], NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( DimDate[Datekey] ) ) ), LASTDATE ( DimDate[Datekey] ) ) VAR _Top20Last12MO = CALCULATE ( SUMX ( VALUES ( DimDate[YearMonth] ), CALCULATE ( [Total Sales], TOPN ( 20, VALUES ( DimCustomer[CustomerKey] ), [Total Sales] ) ) ), _last12MO ) RETURN _Top20Last12MOand finally the percentage is easy:
Percentage of Sales = DIVIDE ( [Total Sales Last 12 MO Rolling Top20], [Total Sales Last 12MO] )
Let me know if this worked.
- 8 years ago
mattbrice - Hey - I think this is right. :) I've modified a few things - but you gave me the core idea. THANK YOU!
I'm getting ready to meet with our Sales Manager to verify the numbers but from what I've checked so far it seems accurate.THANKS SO MUCH! I'll post the final after I confirm.
- 8 years ago
Full answer with mattbrice base formulas from a test dataset - I'm also including the measures for the Count of Customers. I redid the customers count - using the base that matt created. So shout-out to you, mattbrice! :) THANKS!
Count of Customers = CALCULATE ( DISTINCTCOUNT ( 'SALES DETAILS'[LinkToCustomerID] ), 'SALES DETAILS'[SalesTxn Document Type] = "Invoice", 'Calendar - Transaction Date'[Year] >= 2017 )Count of Customers Last 12MO = VAR _last12MO = DATESBETWEEN ( 'Calendar - Transaction Date'[Transaction Date], NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] ) ) ), LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] ) ) RETURN CALCULATE ( [Count of Customers], _last12MO )Total Outgoing Sales = CALCULATE ( SUM ( 'SALES DETAILS'[Sales $$] ), 'Calendar - Transaction Date'[Year] >= 2017 )Total Sales Last 12MO = VAR _last12MO = DATESBETWEEN ( 'Calendar - Transaction Date'[Transaction Date], NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] ) ) ), LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] ) ) RETURN CALCULATE ( [Total Outgoing Sales], _last12MO )Total Sales Last 12 MO Rolling Top20 = VAR _last12MO = DATESBETWEEN ( 'Calendar - Transaction Date'[Transaction Date], NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] ) ) ), LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] ) ) VAR _Top20Last12MO = CALCULATE ( SUMX ( VALUES ( 'Calendar - Transaction Date'[Month] ), CALCULATE ( [Total Outgoing Sales], TOPN ( 20, VALUES ( 'SALES DETAILS'[LinkToCustomerID] ), [Total Outgoing Sales] ) ) ), _last12MO ) RETURN _Top20Last12MOFinal Result:
heathernicole I am glad it worked for you, but I do have one other comment. Typically I frown on hard coding 'Year' values in a measure like you do below : " 'Calendar - Transaction Date'[Year] >= 2017 ". Have you tried removing them from the measures to see if they make a difference? As written, the values shouldn't change. (meaning the >= 2017 should be ignored anyway). If you want to limit the graph to showing current year I typically use a slicer for that.
But, hey, if it works for you and you are happy with it then go for it.
mattbrice - I completely agree with you. I normally do NOT hard code items like. In this sample dataset I was working with - I needed to filter out some bad data associated with 2016 and force the starting point to 2017. That's the ONLY reason I did that.
Thank you for making that point though - otherwise in a "clean" dataset - it's much better to leave that dynamic rather than hardcoding it.
Thanks! :)