Forum Discussion

Hemant_Jaiswar's avatar
1 year ago
Solved

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

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
  • Bibiano_Geraldo's avatar
    Bibiano_Geraldo
    1 year ago

    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

  • 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

    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's avatar
      Bibiano_Geraldo
      Icon for Super User rankSuper 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 

       

       

  • Anonymous's avatar
    Anonymous
    Not 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!