Forum Discussion
Rank by group and sum
Hi, I have difficulties to calculate the rank over my table with a sum.
Here my fact table :
| year_quarter | country | value |
| 2024-Q1 | US | 10 |
| 2024-Q1 | US | 14 |
| 2024-Q1 | Belgium | 23 |
| 2024-Q1 | Australia | 34 |
| 2024-Q2 | Belgium | 43 |
| 2024-Q2 | US | 23 |
(note that country comes from a another dim table dim entity)
Expected output : (mandatory Rank is a calculated column)
| year_quarter | country | sum(value) | rank |
| 2024-Q1 | US | 24 | 2 |
| 2024-Q1 | Belgium | 23 | 3 |
| 2024-Q1 | Australia | 34 | 1 |
| 2024-Q2 | Belgium | 43 | 1 |
| 2024-Q2 | US | 23 | 2 |
I tried this calculated column but the sum does not seem to work :
Hi Anonymous ,
Your reply helped me, I just adjusted it a little bit to sum by quarter and country :
rank = RANKX(FILTER('Table',[year_quarter]=EARLIER('Table'[year_quarter])),SUMX(FILTER('Table',[year_quarter]=EARLIER('Table'[year_quarter])&&[country]=EARLIER('Table'[country])),[value]),,ASC,Dense)Thanks a lot !
Regards.
7 Replies
- AnonymousNot applicable
Hi Gabry ,
If you want a calculated column, please try:
rank = RANKX(FILTER('Table',[year_quarter]=EARLIER('Table'[year_quarter])),SUMX(FILTER('Table',[country]=EARLIER('Table'[country])),[value]),,ASC,Dense)Result:
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- RinnFrequent Visitor
Hi Anonymous ,
Your reply helped me, I just adjusted it a little bit to sum by quarter and country :
rank = RANKX(FILTER('Table',[year_quarter]=EARLIER('Table'[year_quarter])),SUMX(FILTER('Table',[year_quarter]=EARLIER('Table'[year_quarter])&&[country]=EARLIER('Table'[country])),[value]),,ASC,Dense)Thanks a lot !
Regards.
- Gabry
Super User
Hello Rinn
here is your formula:
Rank Measure =VAR CurrentYearQuarter = SELECTEDVALUE('Table'[year_quarter])VAR CurrentCountry = SELECTEDVALUE('Table'[country])
VAR CurrentTotalValue =CALCULATE(SUM('Table'[value]),'Table'[year_quarter] = CurrentYearQuarter,'Table'[country] = CurrentCountry)
VAR RankingTable =SUMMARIZE(ALL('Table'),'Table'[year_quarter],'Table'[country],"TotalValue", SUM('Table'[value]))
RETURNRANKX(FILTER(RankingTable,[year_quarter] = CurrentYearQuarter),[TotalValue],CurrentTotalValue,DESC,DENSE)
let me know if this help 😉 - Ashish_Mathur
Super User
Hi,
Why are you solving this with a calculated column formula? Why not with a measure?