Search code examples
ssasmdx

How do you get the total rows in an MDX query to use for paging?


I am attempting to implement paging to large datasets in MDX (SSAS).

I have the following to retrieve paged data which works fine:

SELECT 
{ 
  [Measures].[Mesasure1],
  [Measures].[Measure2]
} ON COLUMNS, 
SUBSET
(
  ORDER 
  (
    {
      (
        [Item].[Category].ALLMEMBERS
      )
    }, NULL, BASC
  ), 10, 50  --10 = start index, 50 = returned roes
) 
ON ROWS
FROM ( SELECT ( { [Time].[Date].&[2012-04-15T00:00:00]:[Time].[Date].&[2012-04-20T00:00:00] } ) ON COLUMNS
FROM [DataMartPerformance]
))

However I cannot for the life of me find anywhere on the internet that helps explain how to get the total rows available. Do I do it in a seperate query? If so how? Or can I wrap it into this one query somehow?


Solution

  • Similar to how you'd do TSQL paging, you'll need to run another query to count the total elements. You may have to tinker with this depending on how you've done your original query, but I use something like:

    WITH 
    MEMBER [Measures].[ElementCount] AS
    {
        NONEMPTY
        (
            {
                [Item].[Category].ALLMEMBERS *
                { [Time].[Date].&[2012-04-15T00:00:00]:[Time].[Date].&[2012-04-20T00:00:00] } 
            },
            {
                [Measures].[Mesasure1],
                [Measures].[Measure2]
            }
        )
    }.COUNT
    
    SELECT
    {
        [Measures].[ElementCount]
    }
    ON COLUMNS
    FROM 
    [DataMartPerformance]
    

    For filtering, you can do dimension filters by using an exists against your dimension attributes:

    WITH 
    MEMBER [Measures].[ElementCount] AS
    {
        NONEMPTY
        (
            EXISTS
            (
                {
                    [Item].[Category].ALLMEMBERS *
                    { [Time].[Date].&[2012-04-15T00:00:00]:[Time].[Date].&[2012-04-20T00:00:00] } 
                },
                {
                    [Dimension].[Attribute].[FilterByThisAttribute]
                }
            ),
            {
                [Measures].[Mesasure1],
                [Measures].[Measure2]
            }
        )
    }.COUNT
    
    SELECT
    {
        [Measures].[ElementCount]
    }
    ON COLUMNS
    FROM 
    [DataMartPerformance]
    

    I haven't got to writing the measure value filters yet, I need to do that next for my own MDX paging constructor...