MDX 按月计算的成员

MDX Calculated Members by Month

我正在 SSMS 2014 中处理一个查询,我需要 [Measures].[Percent Unique App Users] 按月计算,这样我就可以动态计算每月的增长百分比。我最好从上个月与前一个月的比较开始,这最终将成为 运行 每个月的报告(因此需要它是动态的)。

有或没有我的 DATE 成员,[CurrentMonthUniqueUsers] 和 [LastMonthUniqueUsers] 的计数保持不变(在示例中显示)。此外,如果没有我的 WHERE 语句,我只会获得非成员行的结果。

WITH
MEMBER [LastMonthUniqueUsers] as SUM({[Date].[Calendar].CurrentMember.Lag(2)},[Measures].[App Unique Users Loggedin])
MEMBER [CurrentMonthUniqueUsers] as SUM({[Date].[Calendar].CurrentMember.Lag(1)},[Measures].[App Unique Users Loggedin])
//
MEMBER [Measures].[Percent Unique App Users] AS ([CurrentMonthUniqueUsers]) / ([LastMonthUniqueUsers]), FORMAT_STRING = "Percent"
//
MEMBER [CurrentDate] As Now()
//

SELECT
    non empty[Project].[Client Name].children ON 0,
    non empty{[Measures].[Percent Unique App Users],
        [CurrentMonthUniqueUsers],
        [LastMonthUniqueUsers], 
        [Measures].[App Logins],
        [Measures].[App Unique Users Loggedin]} ON 1
FROM
    [OpsViewWarehouse]
WHERE
    [Date].[Year].[2017]

下图是我得到的结果。登录计数和唯一登录计数是所有时间的,不会按任何日期过滤。

Data Screenshot

我是 MDX 的新手,非常感谢任何帮助!

编辑 (5/22/2017):这是与 whytheq 提供的答案一起工作的查询。请注意,它还不是动态的,因为我必须在列上指定一个日期 (201705)。

WITH 
  MEMBER [2MonthsPriorUniqueUsers] AS Sum({[Date].[Calendar Year Month].CurrentMember.Lag(2)},[Measures].[App Unique Users Loggedin]) 
  MEMBER [1MonthPriorUniqueUsers] AS Sum({[Date].[Calendar Year Month].CurrentMember.Lag(1)},[Measures].[App Unique Users Loggedin]) 
  MEMBER [1MonthPriorTotalAppLogins] AS Sum({[Date].[Calendar Year Month].CurrentMember.Lag(1)},[Measures].[App Logins])
  MEMBER [Measures].[PercentUniqueIncreaseMoM] AS ([1MonthPriorUniqueUsers] / [2MonthsPriorUniqueUsers])-1,FORMAT_STRING = "Percent" 
SELECT 
   NON EMPTY 
     {[Date].[Calendar Year Month].[201705]}
   *  [Project].[Client Name].members ON 0,
  NON EMPTY 
    {
      [Measures].[PercentUniqueIncreaseMoM]
     ,[1MonthPriorUniqueUsers]
     ,[2MonthsPriorUniqueUsers]
     ,[1MonthPriorTotalAppLogins]
    } ON 1
FROM [OpsViewWarehouse]

在您的查询中,您的 CURRENTMEMBER 是 WHERE 子句中的内容 - 这是 2017 - 所以这不会返回一个月 - 它可能会返回成员 [Date].[Calendar].[All]:

[Date].[Calendar].CurrentMember

尝试在 SELECT 或 WHERE 子句中添加月份:

//
WITH 
  MEMBER [LastMonthUniqueUsers] AS 
    Sum
    (
      {[Date].[Calendar].CurrentMember.Lag(2)}
     ,[Measures].[App Unique Users Loggedin]
    ) 
  MEMBER [CurrentMonthUniqueUsers] AS 
    Sum
    (
      {[Date].[Calendar].CurrentMember.Lag(1)}
     ,[Measures].[App Unique Users Loggedin]
    ) 
  //
  MEMBER [Measures].[Percent Unique App Users] AS 
    [CurrentMonthUniqueUsers] / [LastMonthUniqueUsers] 
   ,FORMAT_STRING = "Percent" 
  //
  MEMBER [CurrentDate] AS 
    Now() 
SELECT 
  NON EMPTY 
      [Project].[Client Name].Children ON 0
 ,NON EMPTY 
    {[Date].[Calendar].[Month].&[Jan 2017]}  //<<<<<<change to a month member in your cube
  * {
      [Measures].[Percent Unique App Users]
     ,[CurrentMonthUniqueUsers]
     ,[LastMonthUniqueUsers]
     ,[Measures].[App Logins]
     ,[Measures].[App Unique Users Loggedin]
    } ON 1
FROM [OpsViewWarehouse];

以上也可以是月份的集合:

//
WITH 
  MEMBER [LastMonthUniqueUsers] AS 
    Sum
    (
      {[Date].[Calendar].CurrentMember.Lag(2)}
     ,[Measures].[App Unique Users Loggedin]
    ) 
  MEMBER [CurrentMonthUniqueUsers] AS 
    Sum
    (
      {[Date].[Calendar].CurrentMember.Lag(1)}
     ,[Measures].[App Unique Users Loggedin]
    ) 
  //
  MEMBER [Measures].[Percent Unique App Users] AS 
    [CurrentMonthUniqueUsers] / [LastMonthUniqueUsers] 
   ,FORMAT_STRING = "Percent" 
  //
  MEMBER [CurrentDate] AS 
    Now() 
SELECT 
  NON EMPTY 
    {
      [Measures].[Percent Unique App Users]
     ,[CurrentMonthUniqueUsers]
     ,[LastMonthUniqueUsers]
     ,[Measures].[App Logins]
     ,[Measures].[App Unique Users Loggedin]
    } ON 0
 ,NON EMPTY 
     {Date].[Calendar].[Month].MEMBERS  //<<<<<<change to the month hierarchy in your cube
   *  [Project].[Client Name].Children ON 1
FROM [OpsViewWarehouse];