Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply

ranking

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

Jan233456
Feb233434
Mar234534
Apr342345
May232266
Jun235656


sample data 

ConvertedDateLineNameSum of DomesticNOS
5/1/2024 0:0072H53
5/1/2024 0:0072H DLX53
5/1/2024 0:0073H50
5/1/2024 0:00AC+IVE52
5/1/2024 0:00ACCESSORIES88
6/1/2024 0:000% PLIABLE61
6/1/2024 0:0072H65
6/1/2024 0:0072H DLX80
6/1/2024 0:0073H67
6/1/2024 0:00ABBEY62
6/1/2024 0:00AC+IVE91
6/1/2024 0:00ACCESSORIES76
7/1/2024 0:0072H77
7/1/2024 0:0072H DLX73
7/1/2024 0:0073H73
7/1/2024 0:00ABBEY73
7/1/2024 0:00AC+IVE88
7/1/2024 0:00ACCESSORIES74
8/1/2024 0:0072H99
8/1/2024 0:0072H DLX54
8/1/2024 0:0073H68
8/1/2024 0:00ABBEY90
8/1/2024 0:00AC+IVE89
8/1/2024 0:00ACCESSORIES53
9/1/2024 0:000% PLIABLE82
9/1/2024 0:0072H66
9/1/2024 0:0072H DLX77
9/1/2024 0:0073H53
9/1/2024 0:00AC+IVE66
9/1/2024 0:00ACCESSORIES84
10/1/2024 0:004PACK82
10/1/2024 0:0072H85
10/1/2024 0:0072H DLX57
10/1/2024 0:0073H95
10/1/2024 0:00ABBEY67
10/1/2024 0:00AC+IVE52
10/1/2024 0:00ACCESSORIES64
11/1/2024 0:002015 BACKPACK - HIGH SIERRA69
11/1/2024 0:004PACK98
11/1/2024 0:0072H90
11/1/2024 0:0072H DLX84
11/1/2024 0:0073H58
11/1/2024 0:0096H - HS PACK N GO 298
11/1/2024 0:00ABBEY97
11/1/2024 0:00AC+IVE90
11/1/2024 0:00ACCESSORIES70
2 ACCEPTED SOLUTIONS
Bibiano_Geraldo
Super User
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:

Bibiano_Geraldo_8-1734866362445.png

 

Now your calendar table should look like this:

Bibiano_Geraldo_0-1734864363546.png

 

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.

Bibiano_Geraldo_1-1734864460320.png

 

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).

Bibiano_Geraldo_2-1734864605719.png

 

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.

Bibiano_Geraldo_3-1734864723241.png

Bibiano_Geraldo_5-1734864876359.png

 

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.

Bibiano_Geraldo_6-1734865355276.png


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".

Bibiano_Geraldo_7-1734865500864.png


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.

ezgif-1-178416d885.gif

Download the project here: Powerbi.pbix 

 

 

View solution in original post

Kedar_Pande
Super User
Super User

@Hemant_Jaiswar 

Create a calculated column to identify the last 6 months excluding the current month:

Last6MonthsFlag = 
VAR CurrentMonth = EOMONTH(TODAY(), -1)
RETURN
IF(
'Table'[ConvertedDate] >= EOMONTH(CurrentMonth, -6) + 1
&& 'Table'[ConvertedDate] <= CurrentMonth,
1,
0
)

Apply this column as a filter in the report to focus only on the last 6 months of data.

 

Create a Measure for Total Sales

TotalSales = SUM('Table'[Sum of DomesticNOS])

Rank LineNames based on their total sales in the last 6 months:

LineNameRank = 
VAR TotalSalesForPeriod =
CALCULATE(
[TotalSales],
'Table'[Last6MonthsFlag] = 1
)
RETURN
RANKX(
ALL('Table'[LineName]),
TotalSalesForPeriod,
,
ASC/DESC, // Use DESC for Top N, ASC for Bottom N
Dense
)

Create a measure to filter the Top N or Bottom N LineNames:

IsInTopN = 
VAR SelectedN = SELECTEDVALUE('SlicerTable'[TopN])
RETURN
IF(
[LineNameRank] <= SelectedN,
1,
0
)

Create a parameter table (SlicerTable) for Top N selection with values like 3, 5, 10, etc., and bind it to the IsInTopN measure.

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

View solution in original post

3 REPLIES 3
Kedar_Pande
Super User
Super User

@Hemant_Jaiswar 

Create a calculated column to identify the last 6 months excluding the current month:

Last6MonthsFlag = 
VAR CurrentMonth = EOMONTH(TODAY(), -1)
RETURN
IF(
'Table'[ConvertedDate] >= EOMONTH(CurrentMonth, -6) + 1
&& 'Table'[ConvertedDate] <= CurrentMonth,
1,
0
)

Apply this column as a filter in the report to focus only on the last 6 months of data.

 

Create a Measure for Total Sales

TotalSales = SUM('Table'[Sum of DomesticNOS])

Rank LineNames based on their total sales in the last 6 months:

LineNameRank = 
VAR TotalSalesForPeriod =
CALCULATE(
[TotalSales],
'Table'[Last6MonthsFlag] = 1
)
RETURN
RANKX(
ALL('Table'[LineName]),
TotalSalesForPeriod,
,
ASC/DESC, // Use DESC for Top N, ASC for Bottom N
Dense
)

Create a measure to filter the Top N or Bottom N LineNames:

IsInTopN = 
VAR SelectedN = SELECTEDVALUE('SlicerTable'[TopN])
RETURN
IF(
[LineNameRank] <= SelectedN,
1,
0
)

Create a parameter table (SlicerTable) for Top N selection with values like 3, 5, 10, etc., and bind it to the IsInTopN measure.

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

Bibiano_Geraldo
Super User
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:

Bibiano_Geraldo_8-1734866362445.png

 

Now your calendar table should look like this:

Bibiano_Geraldo_0-1734864363546.png

 

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.

Bibiano_Geraldo_1-1734864460320.png

 

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).

Bibiano_Geraldo_2-1734864605719.png

 

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.

Bibiano_Geraldo_3-1734864723241.png

Bibiano_Geraldo_5-1734864876359.png

 

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.

Bibiano_Geraldo_6-1734865355276.png


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".

Bibiano_Geraldo_7-1734865500864.png


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.

ezgif-1-178416d885.gif

Download the project here: Powerbi.pbix 

 

 

tharunkumarRTK
Super User
Super User

@Hemant_Jaiswar 

 

Hope this helps,
TopN
Screenshot 2024-12-22 at 12.10.15 PM.png

Bottom N

Screenshot 2024-12-22 at 12.10.46 PM.png

 

Please find the attached PBIX file

 

Need a Power BI Consultation? Hire me on Upwork

 

 

 

Connect on LinkedIn

 

 

 








Did I answer your question? Mark my post as a solution!
If I helped you, click on the Thumbs Up to give Kudos.

Proud to be a Super User!


PBI_SuperUser_Rank@2x.png

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.