带过滤器的MDX计数?

时间:2016-04-28 18:42:22

标签: filter count mdx

我们想要计算“库存单位”大于0的周数 我把它放在一起但它不是很有效,它计算一个项目的所有周,而不仅仅是库存> 0.我尝试了一些过滤器,但它们总是导致#error结果

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]
  )
);

2 个答案:

答案 0 :(得分: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] 

答案 1 :(得分: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]
  )
);

但这是使用过滤条件执行Count的更好方法 - 它执行得更好:

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];