Forum Discussion
Ranking on calculated measure
- 2 years ago
hi, GrahamR99
try to make new column
Column = RANK( DENSE, ALLSELECTED( 'Table (2)'[Sup Postcode], 'Table (2)'[Distance (km)] ), ORDERBY('Table (2)'[Distance (km)],ASC) ,,PARTITIONBY('Table (2)'[Sup Postcode]) )if you want to measure then try below code
Measure 2 = var a = ALL('Table (2)'[Distance (km)],'Table (2)'[Sup Postcode]) return RANK( DENSE, a, ORDERBY( 'Table (2)'[Distance (km)], ASC ),, PARTITIONBY('Table (2)'[Sup Postcode]) ) - 2 years ago
Hello Dangar332
Thank you for the formula, it worked when I change the data.
When I first setup the Power BI report, I used Power BI mesure to calculate the distance between two locations. When I recreated the data in SQL Server and used it's ablity to calulate the distance, the data was a column and I could use your formula and also slice the distance column in my Power BI report which I could not do when it was a mesure.
The SQL Script I used was;
DistanceInMiles = GEOGRAPHY::Point([Lat], [Long], 4326).STDistance(GEOGRAPHY::Point([latitude], [longitude], 4326)) / 1609.344
Your formula I used was.
Column = RANK( DENSE, ALLSELECTED( 'Table (2)'[Sup Postcode], 'Table (2)'[Distance (km)] ), ORDERBY('Table (2)'[Distance (km)],ASC) ,,PARTITIONBY('Table (2)'[Sup Postcode]) )Thank you for your help.
Regards
GrahamR99
Plz use this modified DAX:
Distance (Rank) =
RANKX(
FILTER(
'Legacy Events Planning 2024',
'Legacy Events Planning 2024'[Supporter_Postcode] = EARLIER('Legacy Events Planning 2024'[Supporter_Postcode])
),
CALCULATE(
SUM('Legacy Events Planning 2024'[Distance (km)]),
ALLEXCEPT('Legacy Events Planning 2024', 'Legacy Events Planning 2024'[Supporter_Postcode])
),
,
ASC,
Dense
)
if not work please sahre Pbxi file or excel file with sample data.
Hello 123abc
The last formula didn't work,.
I can't upload a file on this forum and my organisation will not let me share the file to anyone out side the organisation on Onedrive.
You are going to have to explain how I get the file to you?
Regards
GrahamR99
- GrahamR992 years ago
Resolver I
Hello 123abc
Is there another way you can help me?
I have three columns,
Site Postcode,
Sup Postcode,
Distance (km)
I have mulple sites, and a sup postcode for each site, then the distance (km) is calculated.
Site Postcode Sup Postcode Distnace (km) Rank
AAA ABC 20 2
AAA BCD 30 3
AAA DEF 10 1
BBB ABC 50 3
BBB BCD 10 2
BBB DEF 5 1
How do I write the rank formual for this?
Regards
GrahamR99
- 123abc2 years ago
Community Champion
You can try this DAX:
Rank =
RANKX (
FILTER (
YourTable,
[Site Postcode] = EARLIER ( [Site Postcode] )
),
[Distance (km)],
,
ASC,
Dense
)This formula uses the RANKX function to calculate the rank based on the 'Distance (km)' column for each 'Site Postcode' group. The EARLIER function is used to refer to the current row's 'Site Postcode' when filtering.
Here's a step-by-step explanation of the formula:
FILTER(YourTable, [Site Postcode] = EARLIER([Site Postcode])): This part of the formula filters the table to include only rows where the 'Site Postcode' is equal to the current row's 'Site Postcode'. This ensures that the ranking is done separately for each site.
RANKX(..., [Distance (km)], ..., ASC, Dense): This part of the formula calculates the rank based on the 'Distance (km)' column in ascending order (ASC) and using dense ranking (Dense).
You can add this DAX formula to your Power BI table to create the 'Rank' column. Keep in mind that you need to replace 'YourTable' with the actual name of your table.
- 123abc2 years ago
Community Champion
If you encounter a specific error or face challenges with a particular DAX formula, feel free to share the relevant portions of the code or describe the issue in detail. I'll do my best to provide guidance based on the information you provide within the text format of this conversation.
If you have specific DAX expressions or parts of your Power BI model that you'd like assistance with, please share the relevant details, and I'll try to help you troubleshoot the problem.