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

时间:2015-12-23 06:16:32

标签: mdx mondrian

我想找出当前日期-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]

获取所有记录。

2 个答案:

答案 0 :(得分:1)

以下内容应作为您的日期"过滤"

Filter 
    ( 
     [Created_Date].[Created_Hir].[Created_On].Members, 
     DateDiff 
        ( 
         "d", 
         CDate([Created_Date].[Created_Hir].MEMBER_VALUE), 
         Now() 
        ) <=30 
    ) 
如果您不想遇到性能问题,

不要使用您在示例中使用的命名集。

如果表现不够好,请告诉我。

答案 1 :(得分:0)

以下是今天在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];

结果如下:

enter image description here

因此,如果我们想修改上述内容,以便我们只获得过去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];