2016-04-25 41 views
1

我有SSAS多維立方體,我需要查詢在日期範圍中的每一天的3個月運行平均值。如何從每個日期的SSAS多維立方體(MDX)查詢3個月的運行平均值?

例如,對於2016-04-25我必須獲取數據從2016-01-01到2016-03-31。 所以我不能使用此查詢(因爲我不知道我有多麼的日子必然落後,直到上月):

WITH MEMBER [Measures].[SalesAmount 3m average] AS 
(
SUM(
    ([Date].[Date].CurrentMember.Lag(90) : 
    ([Date].[Date].CurrentMember.Lag(1), 
    [Measures].[SalesAmount] 
    ) 
) 

我想我需要使用祖先函數來獲得一個月和使用滯後到月份粒度。

好吧,讓我們試試這個:

WITH MEMBER [Measures].[SalesAmount 3m average] AS 
(
SUM(
    (Ancestor ([Date].[Date].CurrentMember, [Date].[Month])).Lag(3) : 
    (Ancestor ([Date].[Date].CurrentMember, [Date].[Month])).Lag(1), 
    [Measures].[SalesAmount] 
    ) 
) 
SELECT { [Measures].[SalesAmount 3m average] } ON Columns, 
{ [Date].[Date].&[2016-01-01T00:00:00] : [Date].[Date].&[2016-02-28T00:00:00]} On Rows 
FROM [Cube] 

不幸的是,這個查詢無法正常工作(返回null)。

如何解決這個問題?

更新: 好吧,我tryed查詢成員標題:

MEMBER [Measures].[Test] AS 
(
(Ancestor 
    ( [Date].[Date].CurrentMember, 
     [Date].[Date].[Month]) 
    ).Item(0).Member_Caption 
) 

而且我越來越沒有一個月標題,但日期(日) - 在同一日期[日期]。 [日期] .CurrentMember。

然後我tryed這個查詢:

--First try 
MEMBER [Measures].[Test] AS 
(
IsAncestor([Date].[Date].CurrentMember, [Date].[Month].&[2016-05-01T00:00:00]) 
) 

--Second try 
MEMBER [Measures].[Test] AS 
(
IsAncestor([Date].[Date].CurrentMember, [Date].[Date].[Month].&[2016-05-01T00:00:00]) 
) 

--Third try 
MEMBER [Measures].[Test] AS 
(
IsAncestor([Date].[Date].CurrentMember, [Date].[Year - Quarter - Month - Date].[Month].&[2016-05-01T00:00:00]) 
) 

在所有查詢結果是 「假」。那麼,月份會員不是Date會員的祖先?現在我真的很困惑。

我的約會尺寸看起來是這樣的:

enter image description here

更新2:

MEMBER [Measures].[Test] AS 
(
(Ancestor 
    ( [Date].[Date].CurrentMember, 
     1) 
    ).Member_Caption 
) 

返回:所有

+0

你的代碼看起來不錯 - 我會嘗試複製對付AdvWrks – whytheq

+0

好的 - 測試給了我們答案 - 我會更新我的答案 – whytheq

回答

1

您的代碼似乎罰款。我會在幾個地方,以使事情變得更加明顯增加項目(0),但這不應該有太大變化

WITH 
    MEMBER [Measures].[SalesAmount 3m average] AS 
    Sum 
    (
     Ancestor 
     (
      [Date].[Calendar].CurrentMember 
     ,[Date].[Calendar].[Month] 
     ).Item(0).Lag(3) 
     : 
     Ancestor 
     (
      [Date].[Calendar].CurrentMember 
     ,[Date].[Calendar].[Month] 
     ).Item(0).Lag(1) 
    ,[Measures].[Internet Sales Amount] 
    ) 
SELECT 
    {[Measures].[SalesAmount 3m average]} ON COLUMNS 
,{ 
     [Date].[Calendar].[Date].&[20060222] 
    : 
     [Date].[Calendar].[Date].&[20060722] 
    } ON ROWS 
FROM [Adventure Works]; 

返回此:

enter image description here

檢查你的所有水平的表達是正確的:

[Date].[Date] ? 

[Date].[Month] ? 

還作爲進一步的測試,這返回什麼?

WITH 
    MEMBER [Measures].[test1] AS 
    Ancestor 
    (
     [Date].[Date].CurrentMember 
    ,[Date].[Month] 
    ).Item(0).Lag(3).Member_Caption 
    MEMBER [Measures].[test2] AS 
    Ancestor 
    (
     [Date].[Date].CurrentMember 
    ,[Date].[Month] 
    ).Item(0).Lag(1).Member_Caption 
SELECT 
    { 
    [Measures].[test1] 
    ,[Measures].[test2] 
    } ON COLUMNS 
,{ 
     [Date].[Date].&[2016-01-01T00:00:00] 
    : 
     [Date].[Date].&[2016-02-28T00:00:00] 
    } ON ROWS 
FROM [Cube]; 

祖先將不同層次的同一層次即函數之間:

enter image description here

這意味着是[Date].[Date][Date].[Month]不通過祖先多大關係。試試這個:

WITH 
MEMBER [Measures].[SalesAmount 3m average] AS 
    (
    SUM(
     Ancestor ( 
     [Date].[Year - Quarter - Month - Date].CurrentMember, 
     [Date].[Year - Quarter - Month - Date].[Month]).Lag(3) 
     : 
     Ancestor( 
     [Date].[Year - Quarter - Month - Date].CurrentMember 
     ,[Date].[Year - Quarter - Month - Date].[Month]).Lag(1) 
     ,[Measures].[SalesAmount] 
    ) 
) 
SELECT 
{ [Measures].[SalesAmount 3m average] } ON Columns, 
{ 
    [Date].[Year - Quarter - Month - Date].[Date].&[2016-01-01T00:00:00] 
: [Date].[Year - Quarter - Month - Date].[Date].&[2016-02-28T00:00:00] 
//I suspect the above 2 lines can be replaced by following: 
// [Date].[Date].&[2016-01-01T00:00:00] 
//: [Date].[Date].&[2016-02-28T00:00:00] 
} 
    On Rows 
FROM [Cube]; 

這應該返回All因爲層次[Date].[Date]只有兩個層次......葉級與單級以上的所有日期:

MEMBER [Measures].[Test] AS 
(
(Ancestor 
    ( [Date].[Date].CurrentMember, 
     1) 
    ).Member_Caption 
) 
+0

非常感謝你,whytheq,我試過你查詢測試並更新了我的問題。 – Oblomingo