cancel
Showing results 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

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

1 ACCEPTED SOLUTION
Frequent Visitor

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
)
4 REPLIES 4
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!

Frequent Visitor

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

Frequent Visitor

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
)
Frequent Visitor

Perfect. I am glad it worked for you.