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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

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
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors