Forum Discussion
TOP N filter across previous months based on current Month
- 8 years ago
Hi MV13
Here's something I mocked up earlier in the day and just got home to post.
I set up a basic data model with a Calendar table and a Data table, with Data containing columns Date, Category, Value.
To calculate the sum of Value in any time period for the top 10 Categories (determined in the latest month), the measure I wrote has 3 parts:
- Define the time period where the top 10 Categories will be determined (the 'max' month in this case)
I added a parameter to choose between the max date filtered & the max date present in the data.
Regardless, the max date is expanded out to a calendar month (in variable MaxMonth), and this time period is used to determine the top 10 Categories. - Determine the top 10 Categories in that month (stored in variable TopCategories)
- Calculate sum of Value filtered to those categories.
Here is the actual measure (colour-coding matches above):
Value Sum For Top 10 Categories in Max Month = // Get Relative or Absolute selection VAR MaxDateOption = SELECTEDVALUE ( 'Max Date Option'[Max Date Option] )
// Get Max Date VAR MaxDate = SWITCH ( MaxDateOption, "Latest Data", CALCULATE ( MAX ( Data[Date] ), ALL ( Data ) ), "Current Date Filter", CALCULATE ( MAX ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) ) ) // Expand this to a calendar month VAR MaxMonth = CALCULATETABLE ( PARALLELPERIOD ( 'Calendar'[Date], 0, MONTH ), 'Calendar'[Date] = MaxDate ) // Get Top 10 Categories in that month VAR TopCategories = CALCULATETABLE ( TOPN ( 10, ALL ( Data[Category] ), CALCULATE ( [Value Sum], MaxMonth ) ) ) // Return Value Sum filtered to those Categories RETURN CALCULATE ( [Value Sum], KEEPFILTERS ( TopCategories ) )Well, that's how I would approach it.
The part in red can be adjusted to whatever method you want to use to determine the 'max' month.
Regards,
Owen 🙂
- Define the time period where the top 10 Categories will be determined (the 'max' month in this case)
Hi SK87
Sorry about the broken link! A change in my email address invalidated some of my older OneDrive links.
I have updated the link in the post above and also attached the file directly to that post.
Let me know if you have any issues. If you come across any of my other links that are not working, you can replace
owenauger_ozerconsulting_onmicrosoft_com
with
owen_owenaugerbi_com
Regards,
Owen
Hi OwenAuger
Thanks a lot for the solution. It helped me. I was struggling to resolve this problem for some days. But now I'm having a second problem. I am using this example to filter the top 10 also based on the amount... but I want to show the % variation month by month, and when I change this part:
// Return Value Sum filtered to those Categories
RETURN CALCULATE ( [Value Sum], KEEPFILTERS ( TopCategories ) )
to this:
// Return Value Sum filtered to those Categories
RETURN CALCULATE ( [% variation month by month], KEEPFILTERS ( TopCategories ) )
the measure lose it self and go back to show all the categories.
Here is my [% variation month by month] calculation:
- Anonymous3 years agoNot applicable
Hi OwenAuger,
I have a third problem.
I'm trying to use those measures inside a Field Parameter, that will switch between TopN based on [Value Sum] from current month (like your explanation) and [Value Sum] of all data, so I can choose with a field parameter, which one I prefer to see on the chart.
I did some changes and it is like this:
// Get Top 10 Categories in that month VAR TopCategories = CALCULATETABLE ( TOPN ( 10, ALL ( Data[Category] ), ALL ( 'Calendar Date'[Date] ) ) ) )But it is filtering in a weird way, because there is not all categories, but there are more than 10. I think it is choosing the Top 10 from each month, and not from all data.