简体   繁体   中英

Rankx function for every month DAX Power BI

So I have a table in Power BI with Snapshot dates, Company Name and a Rating. I want to Rank the Ratings for each particular month (no month has repeated companies).

So as a simple example, for the table below:

Snapshot_date Company Name Rating
31/03/2021 A 1
31/03/2021 B 8
31/03/2021 C 6
31/03/2021 D 5
30/04/2021 A 4
30/04/2021 B 7
30/04/2021 C 9
30/04/2021 D 3

I want to end up with:

Snapshot_date Company Name Rating Rank
31/03/2021 A 1 1
31/03/2021 B 8 4
31/03/2021 C 6 3
31/03/2021 D 5 2
30/04/2021 A 4 2
30/04/2021 B 7 3
30/04/2021 C 9 4
30/04/2021 D 3 1

It seems the measure should be something like:

Rank = Rankx(filter(
                    all(table),
                    table[Snapshot_date].[month] = Earlier(table[Snapshot_date].month)
                    && table[Snapshot_date].[year] = Earlier(table[Snapshot_date].year)
                     ),
                     Calculate(Sum(table[Rating])),,1)

But here I get an error where it states that parameter in the Earlier function is not the correct type, and I am referring to an earlier row context which doesn't exist.

I have also tried:

Rank = Rankx(allexcept(table,
                    table[Snapshot_date].[month], table[Snapshot_date].[year]
                     ),
                     Calculate(Sum(table[Rating])),,1)

However this simply does the rank for the whole table rather than for each month.

In the simple example with the table above with only two months, what has worked is:

Rank = Rankx(filter(
                    all(table),
                    table[Snapshot_date].[month] = 1
                    && table[Snapshot_date].[year] = 2021)
                     ),
                     Calculate(Sum(table[Rating])),,1)

This weirdly works for both months if the month and year is one of the two dates. However if the month and year I use is not one of the two dates the ranks all become 1.

Also I want this to work on a large table with many dates and want it to be dynamic. Any help would be greatly appreciated!

You need to remove filter context from your calculation (row filter). ALLEXCEPT is really useful, put your group here.

Use this measure:

Rank = 

RANKX( 
            ALLEXCEPT( Sheet1,Sheet1[Snapshot_date] ),
             CALCULATE(SUM(Sheet1[Rating]))
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM