MDX - TopCount plus 'Other' or 'The Rest' by group (over a set of members)

前端 未结 2 1421
不知归路
不知归路 2021-01-24 08:01

I\'ve got requirement to display top 5 customer sales by customer group, but with other customers sales within the group aggregated as \'Others\'. Something similar to this qu

2条回答
  •  有刺的猬
    2021-01-24 08:35

    The following is against AdvWrks and uses a technique I saw on Chris Webb's blog which he outlines here:
    https://cwebbbi.wordpress.com/2007/06/25/advanced-ranking-and-dynamically-generated-named-sets-in-mdx/

    The section of the script that creates the set MyMonthsWithEmployeesSets I find very difficult to get my head around - maybe @AlexPeshik could shed a little more light on what is happening in the following script.

    WITH 
      SET MyMonths AS 
        TopPercent
        (
          [Date].[Calendar].[Month].MEMBERS
         ,20
         ,[Measures].[Reseller Sales Amount]
        ) 
      SET MyEmployees AS 
        [Employee].[Employee].[Employee].MEMBERS 
      SET MyMonthsWithEmployeesSets AS 
        Generate
        (
          MyMonths
         ,Union
          (
            {[Date].[Calendar].CurrentMember}
           ,StrToSet
            ("
                 Intersect({}, 
                 {TopCount(MyEmployees, 10, ([Measures].[Reseller Sales Amount],[Date].[Calendar].CurrentMember))
                 as EmployeeSet"
                + 
                  Cstr(MyMonths.CurrentOrdinal)
              + "})"
            )
          )
        ) 
      MEMBER [Employee].[Employee].[RestOfEmployees] AS 
        Aggregate
        (
          Except
          (
            MyEmployees
           ,StrToSet
            (
              "EmployeeSet" + Cstr(Rank([Date].[Calendar].CurrentMember,MyMonths))
            )
          )
        ) 
      MEMBER [Measures].[EmployeeRank] AS 
        Rank
        (
          [Employee].[Employee].CurrentMember
         ,StrToSet
          (
            "EmployeeSet" + Cstr(Rank([Date].[Calendar].CurrentMember,MyMonths))
          )
        ) 
    SELECT 
      {
        [Measures].[EmployeeRank]
       ,[Measures].[Reseller Sales Amount]
      } ON 0
     ,Generate
      (
        Hierarchize(MyMonthsWithEmployeesSets)
       ,
          [Date].[Calendar].CurrentMember
        * 
          {
            Order
            (
              Filter
              (
                MyEmployees
               ,
                [Measures].[EmployeeRank] > 0
              )
             ,[Measures].[Reseller Sales Amount]
             ,BDESC
            )
           ,[Employee].[Employee].[RestOfEmployees]
          }
      ) ON 1
    FROM [Adventure Works];
    

    Edit - solution for Alex's third attempt:

    WITH 
      SET [AllCountries] AS [Country].[Country].MEMBERS 
      SET [AllStates]    AS [State-Province].[State-Province].MEMBERS 
      SET [Top2States] AS 
        Generate
        (
          [AllCountries]
         ,TopCount
          (
            (EXISTING 
              [AllStates])
           ,3
           ,[Measures].[Internet Order Count]
          )
        ) 
      MEMBER [State-Province].[All].[RestOfCountry] AS 
        Aggregate({(EXISTING {[AllStates]} - [Top2States])}) 
    SELECT 
      {[Measures].[Internet Order Count]} ON COLUMNS
     ,{
          [AllCountries]
        * 
          {
            [Top2States]
           ,[State-Province].[All].[RestOfCountry]
           ,[State-Province].[All]
          }
      } ON ROWS
    FROM [Adventure Works];
    

提交回复
热议问题