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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

DAX query for rank over multiple columns

Hi I have a base table as seen below.

datenameValue
01-01-2020A1
01-01-2020A16
01-01-2020A4
01-01-2020B1
01-01-2020B9
01-01-2020C19
02-01-2020A12
02-01-2020B14
02-01-2020C7
02-01-2020C9
02-01-2020D4

 

I need to rank each name for each date after adding all values for that name for a particular date. Below will be the expected output.

DateNameValueRank
01-01-2020A211
01-01-2020B103
01-01-2020C192
02-01-2020A123
02-01-2020B142
02-01-2020C161
02-01-2020D44

 

I tried using rankx but not able to do. Any idea how to achieve this?

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

@Anonymous - I did it like this:

Measure 23 = 
    VAR __Date = MAX('Table (23)'[date])
    VAR __Name = MAX('Table (23)'[name])
    VAR __Table =
            SUMMARIZE(
                FILTER(ALL('Table (23)'),[date] = __Date),
                [name],
                "Value",
                SUM([Value])
            )
    VAR __Table1 = ADDCOLUMNS(__Table,"Rank",RANKX(__Table,[Value]))
RETURN
    MAXX(FILTER(__Table1,[name] = __Name),[Rank])

PBIX is attached below sig, you want Page 23, Table (23) and Measure 23.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

@Anonymous - I thought you wanted a rank for each date and for each Name like what you showed in the example output you wanted (below). If you don't want that, get rid of the FILTER for date = __Date in your SUMMARIZE

 

Date Name Value Rank
01-01-2020 A 21 1
01-01-2020 B 10 3
01-01-2020 C 19 2
02-01-2020 A 12 3
02-01-2020 B 14 2
02-01-2020 C 16 1
02-01-2020 D 4

4


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
mahoneypat
Employee
Employee

Please try this measure expression

 

Rank =
RANKX ( ALL ( 'Rank'[name] ), CALCULATE ( SUM ( 'Rank'[Value] ) ) )

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Greg_Deckler
Super User
Super User

@Anonymous - I did it like this:

Measure 23 = 
    VAR __Date = MAX('Table (23)'[date])
    VAR __Name = MAX('Table (23)'[name])
    VAR __Table =
            SUMMARIZE(
                FILTER(ALL('Table (23)'),[date] = __Date),
                [name],
                "Value",
                SUM([Value])
            )
    VAR __Table1 = ADDCOLUMNS(__Table,"Rank",RANKX(__Table,[Value]))
RETURN
    MAXX(FILTER(__Table1,[name] = __Name),[Rank])

PBIX is attached below sig, you want Page 23, Table (23) and Measure 23.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler  - Thanks this worked perfectly fine for me. But when I select multiple dates, it gives rank for each date and not for multiple dates combined. I have a date filter in which user can select more than 1 date and then it does not give desired result. Any idea what tweakwill I have to do in code so that it works for multiple dates as well.

 

Thanks,

Amit Darak

@Anonymous - I thought you wanted a rank for each date and for each Name like what you showed in the example output you wanted (below). If you don't want that, get rid of the FILTER for date = __Date in your SUMMARIZE

 

Date Name Value Rank
01-01-2020 A 21 1
01-01-2020 B 10 3
01-01-2020 C 19 2
02-01-2020 A 12 3
02-01-2020 B 14 2
02-01-2020 C 16 1
02-01-2020 D 4

4


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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