Search code examples
powerbidaxpowerquery

Excluding rows and create rank


I am trying to create a rank that only have ranking on rows that have certain values. I have created a column called "RankingTop" that ranks by ReportType and ReportDate. So there are 2 separate rankings. I am trying to create a rank only for rows that have isCurrent = "no". This is my code & dataset I have right now along with what I am trying to achieve. Please let me know if this is possible. Or could I do this in PowerQuery.

RankingTop = RANKX (
FILTER(
    Table,
    Table[ReportType]
        = EARLIER(Table[ReportType])
)
, Table[ReportDate]
,
, DESC
)

Current dataset

| ReportType | ReportDate | RankingTop | MonthYear | isCurrent |
|------------|------------|------------|-----------|-----------|
| Weekly     | 12/27/2021 | 1          | Dec-21    | no        |
| Weekly     | 12/20/2021 | 2          | Dec-21    | no        |
| Weekly     | 12/13/2021 | 3          | Dec-21    | no        |
| Weekly     | 12/6/2021  | 4          | Dec-21    | no        |
| Weekly     | 11/29/2021 | 5          | Nov-21    | no        |
| Weekly     | 11/22/2021 | 6          | Nov-21    | no        |
| Weekly     | 11/15/2021 | 7          | Nov-21    | no        |
| Weekly     | 11/8/2021  | 8          | Nov-21    | no        |
| Weekly     | 11/1/2021  | 9          | Nov-21    | no        |
| Monthly    | 7/1/2021   | 7          | Jul-21    | no        |
| Monthly    | 8/1/2021   | 6          | Aug-21    | no        |
| Monthly    | 9/1/2021   | 5          | Sep-21    | no        |
| Monthly    | 10/1/2021  | 4          | Oct-21    | no        |
| Monthly    | 11/1/2021  | 3          | Nov-21    | no        |
| Monthly    | 12/1/2021  | 2          | Dec-21    | no        |
| Monthly    | 1/1/2022   | 1          | Jan-22    | yes       |

What I am trying to achieve

| ReportType | ReportDate | RankingTop | MonthYear | isCurrent |
|------------|------------|------------|-----------|-----------|
| Weekly     | 12/27/2021 | 1          | Dec-21    | no        |
| Weekly     | 12/20/2021 | 2          | Dec-21    | no        |
| Weekly     | 12/13/2021 | 3          | Dec-21    | no        |
| Weekly     | 12/6/2021  | 4          | Dec-21    | no        |
| Weekly     | 11/29/2021 | 5          | Nov-21    | no        |
| Weekly     | 11/22/2021 | 6          | Nov-21    | no        |
| Weekly     | 11/15/2021 | 7          | Nov-21    | no        |
| Weekly     | 11/8/2021  | 8          | Nov-21    | no        |
| Weekly     | 11/1/2021  | 9          | Nov-21    | no        |
| Monthly    | 7/1/2021   | 6          | Jul-21    | no        |
| Monthly    | 8/1/2021   | 5          | Aug-21    | no        |
| Monthly    | 9/1/2021   | 4          | Sep-21    | no        |
| Monthly    | 10/1/2021  | 3          | Oct-21    | no        |
| Monthly    | 11/1/2021  | 2          | Nov-21    | no        |
| Monthly    | 12/1/2021  | 1          | Dec-21    | no        |
| Monthly    | 1/1/2022   |            | Jan-22    | yes       |

Solution

  • You can tweak your calculated column to return BLANK if isCurrent = "yes", and to filter isCurrent as well as ReportType:

    RankingTop = 
        IF ( 
            'Table'[isCurrent] = "yes",
            BLANK(),
            RANKX (
                FILTER(
                    'Table',
                    'Table'[ReportType] = EARLIER ( 'Table'[ReportType] ) && 
                    'Table'[isCurrent] <> "yes"
                ), 
                'Table'[ReportDate],
                ,
                DESC
            )
        )