MDX 最高计数小计

MDX Top Count Sub total

我正在尝试创建一个 MDX 查询来计算某个地区最畅销的商店,然后小计该地区。

我已经使用 TOPCOUNT 函数和 GENERATE 函数为热门位置创建了一个 SET,但是我在尝试对每个地区进行小计时遇到了真正的麻烦。

我的MDX如下:

WITH SET [TopLocationsPerTerritory] AS
    GENERATE( 
        Except ([Locations].[Territory].MEMBERS, [Locations].[Territory].[All]),

        TOPCOUNT(
             {[Locations].[Territory].CurrentMember} * Except ([Locations].[Location Hierarchy].[Location].MEMBERS,[Locations].[Location Hierarchy].[Location].[All]),
            5, 
            [Measures].[SLS ($)]
        )
    )
SELECT  {
            [Measures].[SLS YTD ($)],
            [Measures].[SbD BUD SLS YTD ($)],
            [Measures].[SbD BUD v ACT SLS YTD VAR %],
            [Measures].[SLS LFL YTD %],

            [Measures].[SLS GP YTD ($)],
            [Measures].[SbD BUD GP YTD ($)],
            [Measures].[SbD BUD v ACT GP YTD VAR %],
            [Measures].[SLS LFL GP YTD %],

            [Measures].[SLS ($)],
            [Measures].[SbD BUD GP ($)],
            [Measures].[SbD BUD v ACT SLS VAR %],
            [Measures].[SLS LFL %],

            [Measures].[SLS GP ($)],
            [Measures].[SbD BUD GP ($)],
            [Measures].[SbD BUD v ACT GP VAR %],
            [Measures].[SLS LFL GP %]

        } ON COLUMNS,
        (
            [TopLocationsPerTerritory]
        ) on ROWS

而且效果还不错。 但是,我尝试了几种方法,但无法获得每个地区的小计。我设法获得了整个数据集的汇总,但这不是我需要的。

尝试 unionAll 成员:

WITH 
  SET [TopLocationsPerTerritory] AS 
    Generate
    (
      Except
      (
        [Locations].[Territory].MEMBERS
       ,[Locations].[Territory].[All]
      )
     ,Union
      (
        TopCount
        (
            {[Locations].[Territory].CurrentMember}
          * 
            Except
            (
              [Locations].[Location Hierarchy].[Location].MEMBERS
             ,[Locations].[Location Hierarchy].[Location].[All]
            )
         ,5
         ,[Measures].[SLS ($)]
        )
       ,(
          [Locations].[Territory].CurrentMember
         ,[Locations].[Location Hierarchy].[Location].[All]
        )
      )
    )
...
...
...

AdvWrks 上面的原型制作似乎工作正常:

WITH 
  SET [Top5StatesPerCountry] AS 
    Generate
    (
      [Country].[Country].MEMBERS
     ,Union
      (
        TopCount
        (
          [Country].CurrentMember * [State-Province].[State-Province].MEMBERS
         ,5
         ,[Measures].[Internet Order Count]
        )
       ,(
          [Country].CurrentMember
         ,[State-Province].[All]
        )
      )
    ) 
SELECT 
  {[Measures].[Internet Order Count]} ON COLUMNS
 ,{[Top5StatesPerCountry]} ON ROWS
FROM [Adventure Works];

这是结果的一个片段:

但下面是更简单的解决方案,它不会影响 generate 中的 union:

WITH 
  SET [Top5StatesPerCountry] AS 
    Generate
    (
      [Country].[Country].MEMBERS
     ,TopCount
      (
        (EXISTING 
          [State-Province].[State-Province].MEMBERS)
       ,5
       ,[Measures].[Internet Order Count]
      )
    ) 
SELECT 
  {[Measures].[Internet Order Count]} ON COLUMNS
 ,
    [Country].[Country].MEMBERS
  * 
    {
      [Top5StatesPerCountry]
     ,[State-Province].[All]
    } ON ROWS
FROM [Adventure Works];

编辑

您可以将上面的脚本直接修改为:

WITH 
  SET [Top5StatesPerCountry] AS 
    Generate
    (
      [Country].[Country].MEMBERS
     ,TopCount
      (
        (EXISTING 
          [State-Province].[State-Province].MEMBERS)
       ,5
       ,[Measures].[Internet Order Count]
      )
    ) 
  MEMBER [State-Province].[State-Province].[AGGREGTOP5] AS 
    AGGREGATE(EXISTING [Top5StatesPerCountry])
SELECT 
  {[Measures].[Internet Order Count]} ON COLUMNS
 ,
    [Country].[Country].MEMBERS
  * 
    {
      [Top5StatesPerCountry]
     ,[State-Province].[All]
     ,[State-Province].[State-Province].[AGGREGTOP5]
    } ON ROWS
FROM [Adventure Works];