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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Martynas_BI
Frequent Visitor

Ranking by country and date

Hi,

 

I would like to rank the following 'Annual population' table by year and country.

 

For instance, there are 10 country categories each year. I would like the ranking to reset each year (please refer to the snip below)

Martynas_BI_0-1686234523250.png

Many thanks in advance.

 

Martynas

 

1 ACCEPTED SOLUTION

I used the following code and it worked:

 

Ranking =
VAR CurrentYear = YEAR('Annual population'[Date])
RETURN
    RANKX(
        FILTER(
            ALL('Annual population'),
            YEAR('Annual population'[Date]) = CurrentYear
        ),
        'Annual population'[Population],
        ,
        DESC,
        Dense
    )

View solution in original post

4 REPLIES 4
aaitelkadi
Frequent Visitor

One way to rank the 'Annual population' table by year and country, with the ranking resetting each year, is to use the RANKX function in DAX. Here's an example formula that implements this:

```
Rank = RANKX(
FILTER(
'Annual population',
'Annual population'[Country] = EARLIER('Annual population'[Country]) &&
'Annual population'[Year] = EARLIER('Annual population'[Year])
),
'Annual population'[Population],
,
DESC,
Dense
)
```

Hope this helps! 

Hi @aaitelkadi ,

 

Unfortunately, I get an error using this DAX formula regarding the EARLIER function

Martynas_BI_0-1686236424529.png

 

Any advice?

I used the following code and it worked:

 

Ranking =
VAR CurrentYear = YEAR('Annual population'[Date])
RETURN
    RANKX(
        FILTER(
            ALL('Annual population'),
            YEAR('Annual population'[Date]) = CurrentYear
        ),
        'Annual population'[Population],
        ,
        DESC,
        Dense
    )

Perfect. I am glad it worked for you. 

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

July Newsletter

Fabric Community Update - July 2024

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

Top Solution Authors