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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Yikester20
Regular Visitor

DAX Adding up ranks over another grouping

I have a table with data like this:

 

StoreID | Week/End | Sales
===========================
1           | 9/2             | 10
2           | 9/2             | 5
3           | 9/2             | 3
1           | 9/9             | 5
2           | 9/9             | 2
3           | 9/9             | 10


What I need to do is rank the sales by week, then add up the value of the ranking.

 

I tried a measure using RANKX like this:

 

=RANKX(ALL(Stores), SUMX(RELATEDTABLE(Sales), [Sales]))


Which will give me the following table:

 

 

StoreID | Week/End | Sales | Rank
=====================================
1       | 9/2      | 10    | 1
2       | 9/2      | 5     | 2
3       | 9/2      | 3     | 3
1       | 9/9      | 5     | 2
2       | 9/9      | 2     | 3
3       | 9/9      | 10    | 1

 


What I need to do now is to add up the rank column by Store, so in the end I would get a table like this:

 

 

StoreID | Overall Rank
=======================
1       | 3
2       | 5
3       | 4


So store 1 would have a value of 3, because of the w/e 9/2 of 1 and a w/e 9/9 rank of 2. store 2 would have a overall rank of 5, because of the w/e 9/2 of 2 and a w/e 9/9 rank of 3, and so on.

 

 

I would try to do this in SQL, but unfortunately some of my base data is in excel and some of it is in SQL, so I need to do this in DAX or M.

 

Any ideas would be greatly appreciated!

1 ACCEPTED SOLUTION
Eric_Zhang
Microsoft Employee
Microsoft Employee

@Yikester20

Try to create a measure as below. See more details in the attached pbix file.

Overall Rank =
VAR tbl =
    ADDCOLUMNS (
        sales,
        "rank", RANKX (
            CALCULATETABLE ( sales, ALLEXCEPT ( sales, sales[Week/End] ) ),
            sales[Sales],
            ,
            DESC,
            DENSE
        )
    )
RETURN
    SUMX ( tbl, [rank] )

Capture.PNG

View solution in original post

2 REPLIES 2
Eric_Zhang
Microsoft Employee
Microsoft Employee

@Yikester20

Try to create a measure as below. See more details in the attached pbix file.

Overall Rank =
VAR tbl =
    ADDCOLUMNS (
        sales,
        "rank", RANKX (
            CALCULATETABLE ( sales, ALLEXCEPT ( sales, sales[Week/End] ) ),
            sales[Sales],
            ,
            DESC,
            DENSE
        )
    )
RETURN
    SUMX ( tbl, [rank] )

Capture.PNG

Ashish_Mathur
Super User
Super User

Hi,

 

A simple SUM measure should work

 

=SUM([Rank])


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors