Search code examples
ssasmdxolap

OrderBy MDX Queries?


This is my MDX Query.

select
{
    [Measures].[Quantity],
    [Measures].[Net Sales]
}
on columns,
NON EMPTY
{(
    [Products].[Item Description].children,
    [Calendar].[Date].[Date]
)}
on rows
from
(
select 
{
    [Calendar].[Date].&[2015-03-23T00:00:00],
    [Calendar].[Date].&[2015-03-22T00:00:00],
    [Calendar].[Date].&[2015-03-21T00:00:00]
}
ON columns
FROM [SalesReport])

This gives the sales records in quantity and net sales as expected. We need now to sort it to get the item which has the highest quantity in first. In other words, we're looking to apply 'descending' sort here.

I tried all order method which gives me error. Can you please help me to get my wish done?


Solution

  • Have you tried the below ?

    select
    {
        [Measures].[Quantity],
        [Measures].[Net Sales]
    }
    on columns,
    NON EMPTY
    ORDER((
        [Products].[Item Description].children,
        [Calendar].[Date].[Date]
    ), [Measures].[Quantity], DESC)
    on rows
    from
    (
    select 
    {
        [Calendar].[Date].&[2015-03-23T00:00:00],
        [Calendar].[Date].&[2015-03-22T00:00:00],
        [Calendar].[Date].&[2015-03-21T00:00:00]
    }
    ON columns
    FROM [SalesReport])
    

    Second attempt [WORKING SOLUTION]

    select
    {
        [Measures].[Quantity],
        [Measures].[Net Sales]
    }
    on columns,
    NON EMPTY
    ORDER((
        [Products].[Item Description].children    
    ), [Measures].[Quantity], DESC)
    *[Calendar].[Date].[Date]
    on rows
    from
    (
    select 
    {
        [Calendar].[Date].&[2015-03-23T00:00:00],
        [Calendar].[Date].&[2015-03-22T00:00:00],
        [Calendar].[Date].&[2015-03-21T00:00:00]
    }
    ON columns
    FROM [SalesReport])