MDX 查询中的 where 子句中的 Now()

Now() in where clause in MDX query

我想找出当前日期 -30 天之间的所有记录。但无法为此编写 MDX 过滤器。我当前的查询是:

 WITH
  SET [~FILTER] AS
     {[Created_Date.Created_Hir].[Created_On].Members}
 SET [~ROWS] AS
    {[Sales Order Attributes SO.Sales_order].[Sales Order ID].Members}
    SELECT
   NON EMPTY {[Measures].[CONT_AMT_GROSS]} ON COLUMNS,
   NON EMPTY [~ROWS] ON ROWS
   FROM [SALES_ORDER]
   WHERE [~FILTER]

正在获取所有记录。

以下应作为您的约会对象 "filter"

Filter 
    ( 
     [Created_Date].[Created_Hir].[Created_On].Members, 
     DateDiff 
        ( 
         "d", 
         CDate([Created_Date].[Created_Hir].MEMBER_VALUE), 
         Now() 
        ) <=30 
    ) 

如果您不想 运行 遇到性能问题,请不要 使用示例中的命名集。

如果性能不够好,请告诉我。

这是在 AdvWrks 多维数据集中查找今天以及之前 30 天的示例:

WITH 
  MEMBER [Measures].[Key for Today] AS 
    Format
    (
      Now()
     ,'yyyyMMdd'
    ) 
  MEMBER [Measures].[Key for Today (AW)] AS 
    '2007' + Right([Measures].[Key for Today],4) 
  MEMBER [Measures].[Today string] AS 
    '[Date].[Calendar].[Date].&[' + [Measures].[Key for Today (AW)] + ']' 
  SET [Today] AS 
    StrToMember
    (
      [Measures].[Today string]
     ,constrained
    ) 
  SET [Last30Days] AS 
    [Today].Item(0).Item(0).Lag(30) : [Today].Item(0).Item(0) 
SELECT 
  {} ON 0
 ,[Last30Days] ON 1
FROM [Adventure Works];

结果:

所以如果我们想修改上面的内容以便我们只获得过去 30 天的互联网销售额:

WITH 
  MEMBER [Measures].[Key for Today] AS 
    Format
    (
      Now()
     ,'yyyyMMdd'
    ) 
  MEMBER [Measures].[Key for Today (AW)] AS 
    '2007' + Right([Measures].[Key for Today],4) 
  MEMBER [Measures].[Today string] AS 
    '[Date].[Calendar].[Date].&[' + [Measures].[Key for Today (AW)] + ']' 
  SET [Today] AS 
    StrToMember
    (
      [Measures].[Today string]
     ,constrained
    ) 
  SET [Last30Days] AS 
    [Today].Item(0).Item(0).Lag(30) : [Today].Item(0).Item(0) 
  MEMBER [Date].[Calendar].[All].[Last30Days] AS 
    Aggregate([Last30Days]) 
SELECT 
  {[Measures].[Internet Sales Amount]} ON 0
FROM [Adventure Works]
WHERE [Date].[Calendar].[All].[Last30Days];

将以上内容应用到您的场景中将如下所示:

WITH 
  MEMBER [Measures].[Key for Today] AS 
    Format
    (
      Now()
     ,'yyyyMMdd'
    ) 
  MEMBER [Measures].[Today string] AS 
    '[Created_Date.Created_Hir].[Created_On].&[' + [Measures].[Key for Today] + ']' 
  SET [Today] AS 
    StrToMember
    (
      [Measures].[Today string]
     ,constrained
    ) 
  SET [~FILTER] AS 
    [Today].Item(0).Item(0).Lag(30) : [Today].Item(0).Item(0) 
  MEMBER [Created_Date.Created_Hir].[All].[~FILTERaggregated] AS 
  //MEMBER [Created_Date.Created_Hir].[Created_On].[All].[~FILTERaggregated] AS //<<if above is throwing an exception then try this instead
    Aggregate([~FILTER]) 
  SET [~ROWS] AS 
    {
      [Sales Order Attributes SO.Sales_order].[Sales Order ID].MEMBERS
    } 
SELECT 
  NON EMPTY 
    {[Measures].[CONT_AMT_GROSS]} ON COLUMNS
 ,NON EMPTY 
    [~ROWS] ON ROWS
FROM [SALES_ORDER]
WHERE 
  [Created_Date.Created_Hir].[All].[~FILTERaggregated];