Blog Post
MDX - Ranking And Ordering By a Measure Value


The following example is based on an Adventure Works cube and shows you how to create a measure that ranks countries according to sales revenue.
It implements the following MDX functions:
Rank
CurrentMember
Children
Order
Rank by Largest to Smallest
The MDX WITH MEMBER statement creates a temporary measure for the rank value based on total internet slaes in the calendar year 2006, and the associated SELECT part uses the rank to order the countries.
The WITH MEMBER statement is required here as the Rank function returns a vaue and not a tuple or set. The only way we work with values in MDX is as measures, therefore, we must create a measure from the Rank result.
The MDX Rank function takes up to three arguments.
Rank(Tuple_Expression, Set_Expression [ ,Numeric Expression ] )
The first identifies the member that will be reordered
The second is the set of members that will be reordered
The third (optional) argument is the value that determines the order
So, in the above example it is the country members that we are ranking within the set of all countries (top level of the [Customer Geography] hierarchy) based on descending sales amount value.
The default behaviour is to rank from largest value to smallest. The resulting set will not be ordered according to the rank
Here is the output from the above query:
You can see in the screenshot that the rank values are correct, but the results are not in rank order.
We can add an Order function to the SELECT statement to return the results in rank order as shown in the following example;
Here is the screenshot showing the ordered results:
The third argument is optional. If it is not provided then the ranking is based on the order of the set passed in to the Rank function:
This version of the query will rank by the alphabetical order of the country members as that is the default order for the country members definied in the Customer dimension.
You can see from the screenshot above that the rank value follows the alphabetical country name and not the sales amount.
Ranking By Smallest to Largest
The Rank function does not have an argument to determine the order for the ranking, but we can use the behaviour of rank without the third argument to achieve ranking from smallest to largest:
If you would like to learn more about MDX queries and programming why not take a look at the SQL Server MDX training course - Introduction to SQL Server MDX course.
Share This Post
Mandy Doward
Managing Director
PTR’s owner and Managing Director is a Microsoft MCSE certified Business Intelligence (BI) Consultant, with over 30 years of experience working with data analytics and BI.
Frequently Asked Questions
Couldn’t find the answer you were looking for? Feel free to reach out to us! Our team of experts is here to help.
Contact Us