Forum Discussion
power bi ranking calculation
Requirement:
"I need to display the sales of the Top N or Bottom N LineNames over the last 6 months, excluding the current month, in a table. The months should appear as rows, and only the selected Top N or Bottom N LineNames should be shown as columns. For each LineName, the table should display the sales for each of the last 6 months.
For example, if I select Top 3 LineNames, the table should show the sales for the top 3 LineNames based on their performance over the last 6 months, with each LineName having its own column and the respective monthly sales as values."
Let’s say you select Top 3 LineNames. The result would look like this:
Month LineName 1 LineName 2 LineName 3
| Jan | 23 | 34 | 56 |
| Feb | 23 | 34 | 34 |
| Mar | 23 | 45 | 34 |
| Apr | 34 | 23 | 45 |
| May | 23 | 22 | 66 |
| Jun | 23 | 56 | 56 |
sample data
| ConvertedDate | LineName | Sum of DomesticNOS |
| 5/1/2024 0:00 | 72H | 53 |
| 5/1/2024 0:00 | 72H DLX | 53 |
| 5/1/2024 0:00 | 73H | 50 |
| 5/1/2024 0:00 | AC+IVE | 52 |
| 5/1/2024 0:00 | ACCESSORIES | 88 |
| 6/1/2024 0:00 | 0% PLIABLE | 61 |
| 6/1/2024 0:00 | 72H | 65 |
| 6/1/2024 0:00 | 72H DLX | 80 |
| 6/1/2024 0:00 | 73H | 67 |
| 6/1/2024 0:00 | ABBEY | 62 |
| 6/1/2024 0:00 | AC+IVE | 91 |
| 6/1/2024 0:00 | ACCESSORIES | 76 |
| 7/1/2024 0:00 | 72H | 77 |
| 7/1/2024 0:00 | 72H DLX | 73 |
| 7/1/2024 0:00 | 73H | 73 |
| 7/1/2024 0:00 | ABBEY | 73 |
| 7/1/2024 0:00 | AC+IVE | 88 |
| 7/1/2024 0:00 | ACCESSORIES | 74 |
| 8/1/2024 0:00 | 72H | 99 |
| 8/1/2024 0:00 | 72H DLX | 54 |
| 8/1/2024 0:00 | 73H | 68 |
| 8/1/2024 0:00 | ABBEY | 90 |
| 8/1/2024 0:00 | AC+IVE | 89 |
| 8/1/2024 0:00 | ACCESSORIES | 53 |
| 9/1/2024 0:00 | 0% PLIABLE | 82 |
| 9/1/2024 0:00 | 72H | 66 |
| 9/1/2024 0:00 | 72H DLX | 77 |
| 9/1/2024 0:00 | 73H | 53 |
| 9/1/2024 0:00 | AC+IVE | 66 |
| 9/1/2024 0:00 | ACCESSORIES | 84 |
| 10/1/2024 0:00 | 4PACK | 82 |
| 10/1/2024 0:00 | 72H | 85 |
| 10/1/2024 0:00 | 72H DLX | 57 |
| 10/1/2024 0:00 | 73H | 95 |
| 10/1/2024 0:00 | ABBEY | 67 |
| 10/1/2024 0:00 | AC+IVE | 52 |
| 10/1/2024 0:00 | ACCESSORIES | 64 |
| 11/1/2024 0:00 | 2015 BACKPACK - HIGH SIERRA | 69 |
| 11/1/2024 0:00 | 4PACK | 98 |
| 11/1/2024 0:00 | 72H | 90 |
| 11/1/2024 0:00 | 72H DLX | 84 |
| 11/1/2024 0:00 | 73H | 58 |
| 11/1/2024 0:00 | 96H - HS PACK N GO 2 | 98 |
| 11/1/2024 0:00 | ABBEY | 97 |
| 11/1/2024 0:00 | AC+IVE | 90 |
| 11/1/2024 0:00 | ACCESSORIES | 70 |
Hi Hemant_Jaiswar ,
Lets go step by step:
1. Create a Calendar Table
You start by ensuring that you have a calendar table. If you don't already have one, you can create one with the DAX formula CALENDARAUTO(), which automatically generates a date range based on the data in your model.Calendar = CALENDARAUTO()2. Create a Month & Year Column
In the calendar table, you create a calculated column that combines the month and year in the format "Mmm-YYYY". This will be useful for grouping data by month and year.Month & Year = FORMAT('Calendar'[Date],"Mmm-Yyyy")3. Create a Sorter Column
Next, create a "Sorter" column to ensure that the "Month & Year" column is sorted correctly (chronologically). You can achieve this by creating a column that formats the date as "YYYYMM", which ensures the proper order.Sorter = FORMAT('Calendar'[Date],"YyyyM")
Make sure that the sorter column data type is Whole Number:Now your calendar table should look like this:
4. Sort Month & Year Column
After creating the "Sorter" column, select the "Month & Year" column in your calendar table and sort it by the "Sorter" column to ensure that months are sorted correctly.5. Create Relationship Between Sales and Calendar Table
You need to create a relationship between the "Sales" table and the "Calendar" table. Use the "convertedDate" from the "Sales" table and the "Date" from the "Calendar" table. Make sure that the relationship has a cardinality of "Many to One" from "Sales" to "Calendar" (many sales to one date).6. Create a Numeric Range Parameter
Next, create a parameter (usually a slicer) that lets users specify how many top items to show. This is commonly used for filtering to show a specific number of items, like the top N. Create the parameter with the name TopNParameter and ensure the "Add slicer to this page" checkbox is selected.7. Create Last6Months Measure
This measure marks months within the last six months (excluding the current month) as "1" and all other months as "0". It uses EOMONTH to get the start and end of the range for the last six months.Last6Months = VAR CurrentDate = TODAY() VAR StartDate = EOMONTH(CurrentDate, -7) + 1 VAR EndDate = EOMONTH(CurrentDate, -1) RETURN IF( MAX('Calendar'[Date]) >= StartDate && MAX('Calendar'[Date]) <= EndDate, 1, 0 )8. Create Sales by Rank Measure
Now, create a measure to calculate sales for the top N items based on a ranking of sales. This measure uses RANKX to rank "LineName" based on sales (in descending order), then filters by the rank, and limits the results based on the TopN parameter.Sales by Rank = VAR vRank = RANKX( ALLSELECTED(Sales[LineName]), -- Remove o contexto de datas CALCULATE(SUM(Sales[DomesticNOS]), REMOVEFILTERS('Calendar')), -- Calcula o total sem filtro de datas , DESC, DENSE ) VAR TotalVisible = CALCULATE( SUM(Sales[DomesticNOS]), FILTER( ALLSELECTED(Sales[LineName]), RANKX( ALLSELECTED(Sales[LineName]), CALCULATE(SUM(Sales[DomesticNOS]), REMOVEFILTERS('Calendar')), , DESC, DENSE ) <= topNParameter[TopNParameter Value] ) ) RETURN IF( ISINSCOPE(Sales[LineName]), IF(vRank <= topNParameter[TopNParameter Value], SUM(Sales[DomesticNOS]), BLANK()), TotalVisible )9. Create Matrix Visual
Now, create a matrix visual in Power BI. In the "Rows" field, drag the "Month & Year" column from the "Calendar" table. In the "Columns" field, drag the "LineName" column from the "Sales" table. In the "Values" field, drag the Sales by Rank measure.
10. Apply Filter to the Matrix Visual
With the matrix visual selected, go to the "Filters" pane. Drag the Last6Months measure into the "Filters on this visual" section, and set the filter to only show items where the value is "1".
11. Result
At this point, your matrix visual should display only the sales for the top N items from the last six months, excluding the current month.
Download the project here: Powerbi.pbix
4 Replies
- Hemant_Jaiswar
Helper I
ConvertedDate LineName Sum of DomesticNOS 5/1/2024 0:00 72H 53 5/1/2024 0:00 72H DLX 53 5/1/2024 0:00 73H 50 5/1/2024 0:00 AC+IVE 52 5/1/2024 0:00 ACCESSORIES 88 6/1/2024 0:00 0% PLIABLE 61 6/1/2024 0:00 72H 65 6/1/2024 0:00 72H DLX 80 6/1/2024 0:00 73H 67 6/1/2024 0:00 ABBEY 62 6/1/2024 0:00 AC+IVE 91 6/1/2024 0:00 ACCESSORIES 76 7/1/2024 0:00 72H 77 7/1/2024 0:00 72H DLX 73 7/1/2024 0:00 73H 73 7/1/2024 0:00 ABBEY 73 7/1/2024 0:00 AC+IVE 88 7/1/2024 0:00 ACCESSORIES 74 8/1/2024 0:00 72H 99 8/1/2024 0:00 72H DLX 54 8/1/2024 0:00 73H 68 8/1/2024 0:00 ABBEY 90 8/1/2024 0:00 AC+IVE 89 8/1/2024 0:00 ACCESSORIES 53 9/1/2024 0:00 0% PLIABLE 82 9/1/2024 0:00 72H 66 9/1/2024 0:00 72H DLX 77 9/1/2024 0:00 73H 53 9/1/2024 0:00 AC+IVE 66 9/1/2024 0:00 ACCESSORIES 84 10/1/2024 0:00 4PACK 82 10/1/2024 0:00 72H 85 10/1/2024 0:00 72H DLX 57 10/1/2024 0:00 73H 95 10/1/2024 0:00 ABBEY 67 10/1/2024 0:00 AC+IVE 52 10/1/2024 0:00 ACCESSORIES 64 11/1/2024 0:00 2015 BACKPACK - HIGH SIERRA 69 11/1/2024 0:00 4PACK 98 11/1/2024 0:00 72H 90 11/1/2024 0:00 72H DLX 84 11/1/2024 0:00 73H 58 11/1/2024 0:00 96H - HS PACK N GO 2 98 11/1/2024 0:00 ABBEY 97 11/1/2024 0:00 AC+IVE 90 11/1/2024 0:00 ACCESSORIES 70 Bibiano_Geraldo please help solving this problem,
Requirement:
"I need to display the sales of the Top N or Bottom N LineNames over the last 6 months, excluding the current month, in a table. The months should appear as rows, and only the selected Top N or Bottom N LineNames should be shown as columns. For each LineName, the table should display the sales for each of the last 6 months.
For example, if I select Top 3 LineNames, the table should show the sales for the top 3 LineNames based on their performance over the last 6 months, with each LineName having its own column and the respective monthly sales as values."
- Bibiano_Geraldo
Super User
Hi Hemant_Jaiswar ,
Lets go step by step:
1. Create a Calendar Table
You start by ensuring that you have a calendar table. If you don't already have one, you can create one with the DAX formula CALENDARAUTO(), which automatically generates a date range based on the data in your model.Calendar = CALENDARAUTO()2. Create a Month & Year Column
In the calendar table, you create a calculated column that combines the month and year in the format "Mmm-YYYY". This will be useful for grouping data by month and year.Month & Year = FORMAT('Calendar'[Date],"Mmm-Yyyy")3. Create a Sorter Column
Next, create a "Sorter" column to ensure that the "Month & Year" column is sorted correctly (chronologically). You can achieve this by creating a column that formats the date as "YYYYMM", which ensures the proper order.Sorter = FORMAT('Calendar'[Date],"YyyyM")
Make sure that the sorter column data type is Whole Number:Now your calendar table should look like this:
4. Sort Month & Year Column
After creating the "Sorter" column, select the "Month & Year" column in your calendar table and sort it by the "Sorter" column to ensure that months are sorted correctly.5. Create Relationship Between Sales and Calendar Table
You need to create a relationship between the "Sales" table and the "Calendar" table. Use the "convertedDate" from the "Sales" table and the "Date" from the "Calendar" table. Make sure that the relationship has a cardinality of "Many to One" from "Sales" to "Calendar" (many sales to one date).6. Create a Numeric Range Parameter
Next, create a parameter (usually a slicer) that lets users specify how many top items to show. This is commonly used for filtering to show a specific number of items, like the top N. Create the parameter with the name TopNParameter and ensure the "Add slicer to this page" checkbox is selected.7. Create Last6Months Measure
This measure marks months within the last six months (excluding the current month) as "1" and all other months as "0". It uses EOMONTH to get the start and end of the range for the last six months.Last6Months = VAR CurrentDate = TODAY() VAR StartDate = EOMONTH(CurrentDate, -7) + 1 VAR EndDate = EOMONTH(CurrentDate, -1) RETURN IF( MAX('Calendar'[Date]) >= StartDate && MAX('Calendar'[Date]) <= EndDate, 1, 0 )8. Create Sales by Rank Measure
Now, create a measure to calculate sales for the top N items based on a ranking of sales. This measure uses RANKX to rank "LineName" based on sales (in descending order), then filters by the rank, and limits the results based on the TopN parameter.Sales by Rank = VAR vRank = RANKX( ALLSELECTED(Sales[LineName]), -- Remove o contexto de datas CALCULATE(SUM(Sales[DomesticNOS]), REMOVEFILTERS('Calendar')), -- Calcula o total sem filtro de datas , DESC, DENSE ) VAR TotalVisible = CALCULATE( SUM(Sales[DomesticNOS]), FILTER( ALLSELECTED(Sales[LineName]), RANKX( ALLSELECTED(Sales[LineName]), CALCULATE(SUM(Sales[DomesticNOS]), REMOVEFILTERS('Calendar')), , DESC, DENSE ) <= topNParameter[TopNParameter Value] ) ) RETURN IF( ISINSCOPE(Sales[LineName]), IF(vRank <= topNParameter[TopNParameter Value], SUM(Sales[DomesticNOS]), BLANK()), TotalVisible )9. Create Matrix Visual
Now, create a matrix visual in Power BI. In the "Rows" field, drag the "Month & Year" column from the "Calendar" table. In the "Columns" field, drag the "LineName" column from the "Sales" table. In the "Values" field, drag the Sales by Rank measure.
10. Apply Filter to the Matrix Visual
With the matrix visual selected, go to the "Filters" pane. Drag the Last6Months measure into the "Filters on this visual" section, and set the filter to only show items where the value is "1".
11. Result
At this point, your matrix visual should display only the sales for the top N items from the last six months, excluding the current month.
Download the project here: Powerbi.pbix
- Bibiano_Geraldo
Super User
- AnonymousNot applicable
Hi Hemant_Jaiswar , hello Bibiano_Geraldo, thank you for your prompt reply!
Is there any progress on this issue?
If you find Bibiano_Geraldo's answer is helpful to you, please remember to accept it.
It will help others who meet the similar question in this forum.
If you have any further related issues, please feel free to reply!