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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Top Kudoed Authors