2016-04-28 117 views
0

我們想要統計「庫存單位」大於0的那些星期。
我已經把它放在了一起,但它不是很有效,它計算了一個物品的所有周數,而不僅僅是庫存> 0的那些物品。嘗試過的一些過濾器,但他們總是導致#ERROR作爲結果MDX用過濾器計數?

WITH 
    SET [PreviousSixWeeks] AS 
    Tail 
    (
     Filter 
     (
     [Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Week] 
     , 
     [Measures].[Inventory Units] > 0 
    ) 
    ,6 
    ) 
    MEMBER [Measures].[INVWeekCount] AS 
    DistinctCount(PreviousSixWeeks*[Measures].[Inventory Units]*[Item].[Item Code]) 
SELECT 
    {[Measures].[INVWeekCount]} ON COLUMNS 
,{[Item].[Item Code].[Item Code]} ON ROWS 
FROM 
(
    SELECT 
    {[Location].[Location].&[64]} ON COLUMNS 
    FROM 
    (
    SELECT 
     { 
     [Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[2].&[8] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[2].&[9] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[10] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[11] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[12] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[13] 
     } ON COLUMNS 
    FROM [RFP] 
) 
); 

回答

0

試試和庫存數週,確保它返回預期的結果,然後再添加任何你想要的過濾器。

WITH MEMBER [Measures].[InvWeekCount] AS 
     FILTER([Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Week].MEMBERS, 
     [Measures].[Inventory Units]>0).COUNT 
    SELECT [Measures].[InvWeekCount] ON COLUMNS, 
      [Item].[Item Code].[Item Code].MEMBERS ON ROWS 
    FROM [Test Cube] 
0

更好地避免Filter,如果你能爲它的性能是不是很大:

WITH 
    SET [PreviousSixWeeks] AS 
    Tail 
    (
     [Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Week].MEMBERS 
    ,6 
    ) 
    MEMBER [Measures].[INVWeekCount] AS 
    Count(NonEmpty((EXISTING PreviousSixWeeks),[Measures].[Inventory Units])) 
SELECT 
    {[Measures].[INVWeekCount]} ON COLUMNS 
,{[Item].[Item Code].[Item Code]} ON ROWS 
FROM 
(
    SELECT 
    {[Location].[Location].&[64]} ON COLUMNS 
    FROM 
    (
    SELECT 
     { 
     [Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[2].&[8] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[2].&[9] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[10] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[11] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[12] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[13] 
     } ON COLUMNS 
    FROM [RFP] 
) 
); 

但是,這是一個過濾條件做計數的更好的方式 - 它執行好了很多:

WITH 
    MEMBER [Measures].[InvWeekCount] AS 
    Sum 
    (
     Time.[Yr-Qtr-Per-Wk-Day].[Fiscal Week].MEMBERS 
    ,IIF 
     (
     (NOT 
      IsEmpty([Measures].[Inventory Units])) 
     ,1 
     ,0 
    ) 
    ) 
SELECT 
    [Measures].[InvWeekCount] ON COLUMNS 
,[Item].[Item Code].[Item Code].MEMBERS ON ROWS 
FROM 
(
    SELECT 
    {[Location].[Location].&[64]} ON COLUMNS 
    FROM 
    (
    SELECT 
     { 
     [Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[2].&[8] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[2].&[9] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[10] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[11] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[12] 
     ,[Time].[Yr-Qtr-Per-Wk-Day].[Fiscal Year].&[2016].&[1].&[3].&[13] 
     } ON COLUMNS 
    FROM [RFP] 
) 
); 

在簡單AdvWrks代碼這方面是你使用的是什麼:

WITH 
    MEMBER [Measures].[InvWeekCount] AS 
    Filter 
    (
     [Product].[Product].[Product] 
    , 
     [Measures].[Internet Order Quantity] > 0 
    ).Count 
SELECT 
    [Measures].[InvWeekCount] ON COLUMNS 
,[Customer].[Customer Geography].[Country] ON ROWS 
FROM [Adventure Works]; 

但這是推薦的有效方法:

WITH 
    MEMBER [Measures].[InvWeekCount] AS 
    Sum 
    (
     [Product].[Product].[Product] 
    ,IIF 
     (
     (NOT 
      IsEmpty([Measures].[Internet Order Quantity])) 
     ,1 
     ,NULL 
    ) 
    ) 
SELECT 
    [Measures].[InvWeekCount] ON COLUMNS 
,[Customer].[Customer Geography].[Country] ON ROWS 
FROM [Adventure Works];