Order (MDX)

Arranges members of a specified set, optionally preserving or breaking the hierarchy.

Syntax

Numeric expression syntax
Order(Set_Expression, Numeric_Expression 
[ , { ASC | DESC | BASC | BDESC } ] )

String expression syntax
Order(Set_Expression, String_Expression 
[ , { ASC | DESC | BASC | BDESC } ] )

Arguments

  • Set_Expression
    A valid Multidimensional Expressions (MDX) expression that returns a set.
  • Numeric_Expression
    A valid numeric expression that is typically a Multidimensional Expressions (MDX) expression of cell coordinates that return a number.
  • String_Expression
    A valid string expression that is typically a valid Multidimensional Expressions (MDX) expression of cell coordinates that return a number expressed as a string.

Remarks

The Order function can either be hierarchical (as specified by using the ASC or DESC flag) or nonhierarchical (as specified by using the BASC or BDESC flag; the B stands for "break hierarchy"). If ASC or DESC is specified, the Order function first arranges the members according to their position in the hierarchy, and then orders each level. If BASC or BDESC is specified, the Order function arranges members in the set without regard to the hierarchy. In no flag is specified, ASC is the default.

Examples

The following example returns, from the Adventure Works cube, the number of reseller orders for the [Sales Territory].[Northwest] member and all the ascendants of that member. The Order function reorders the set that includes the [Sales Territory].[Northwest] member and its ascendants for the ROWS axis. The Order function orders the set from the topmost to the bottommost member, in hierarchical order as determined by the [Sales Territory] hierarchy.

SELECT
    Measures.[Reseller Order Count] ON COLUMNS,
    Order(
        Ascendants(
            [Sales Territory].[Sales Territory].[Northwest]
        ),
        DESC
    ) ON ROWS
FROM
    [Adventure Works]

The following example returns the Reseller Sales Measure for the top five selling subcategories of products, irrespective of hierarchy, based on Reseller Gross Profit. The Subset function is used to return only the first 5 sets in the result after the result is ordered using the Order function.

SELECT Subset
   (Order 
      ([Product].[Product Categories].[SubCategory].members
         ,[Measures].[Reseller Gross Profit]
         ,BDESC
      )
   ,0
   ,5
   ) ON 0
FROM [Adventure Works]

The following example uses the Rank function to rank the members of the City hierarchy, based on the Reseller Sales Amount measure, and then displays them in ranked order. By using the Order function to first order the set of members of the City hierarchy, the sorting is done only once and then followed by a linear scan before being presented in sorted order.

WITH 
SET OrderedCities AS Order
   ([Geography].[City].[City].members
   , [Measures].[Reseller Sales Amount], BDESC
   )
MEMBER [Measures].[City Rank] AS Rank
   ([Geography].[City].CurrentMember, OrderedCities)
SELECT {[Measures].[City Rank],[Measures].[Reseller Sales Amount]}  ON 0 
,Order
   ([Geography].[City].[City].MEMBERS
   ,[City Rank], ASC)
    ON 1
FROM [Adventure Works]

The following example returns the number of products in the set that are unique, using the Order function to order the non-empty tuples before utilizing the Filter function. The CurrentOrdinal function is used to compare and eliminate ties.

WITH MEMBER [Measures].[PrdTies] AS Count
   (Filter
      (Order
        (NonEmpty
          ([Product].[Product].[Product].Members
          , {[Measures].[Reseller Order Quantity]}
          )
       , [Measures].[Reseller Order Quantity]
       , BDESC
       ) AS OrdPrds
    , (OrdPrds.CurrentOrdinal < OrdPrds.Count 
       AND [Measures].[Reseller Order Quantity] = 
          ( [Measures].[Reseller Order Quantity]
            , OrdPrds.Item
               (OrdPrds.CurrentOrdinal
               )
            )
         )
         OR (OrdPrds.CurrentOrdinal > 1 
            AND [Measures].[Reseller Order Quantity] = 
               ([Measures].[Reseller Order Quantity]
               , OrdPrds.Item
                  (OrdPrds.CurrentOrdinal-2)
                )
             )
          )
       )
SELECT {[Measures].[PrdTies]} ON 0
FROM [Adventure Works]

See Also

Reference

MDX Function Reference (MDX)

Help and Information

Getting SQL Server 2005 Assistance

Change History

Release History

17 July 2006

Changed content:
  • Updated syntax and arguments to improve clarity.
  • Added updated examples.