MDX在过去12个月中为零个月=在过去12个月滚动窗口中的零交易月数

时间:2015-03-18 22:47:45

标签: mdx ssas-2008

我正在研究MDx查询,以计算从今天开始的过去12个月内每种产品的编号ZERO txn。为Adventure编写的MDX查询工作正常但我在Acutal多维数据集上运行的第二个查询正常工作。

是否可以帮助我纠正查询或任何过滤器,以获得正确的结果,如第一个样本查询?

Adventure Works上的第一个示例查询:(正常工作)

WITH Member [Measures].[Months With Zero Sales] AS 
COUNT(
    FILTER(
        DESCENDANTS(
        {[Date].[Calendar].[Month].&[2008]&[1].LAG(12):[Date].[Calendar].[Month].&[2008]&[1]}, [Date].[Calendar].[Month]) , 
[Measures].[Sales Amount] = 0 ) )
SELECT {[Measures].[Months With Zero Sales]} ON 0, 
[Product].[Product Model Lines].[Product Line].Members on 1
FROM [Adventure Works];

Product Lines  Months With Zero Sales]
-----------    -----------------
Accessory      0
Componenets    0
Mountain       0
Road           0
Touring        6  

我的多维数据集上的第二个查询:

WITH Member [Measures].[Zero Months in last 12] AS 
COUNT(
      FILTER(
            DESCENDANTS(
            {[Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].LAG(12):
            [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]}, 
            [Settlement Date].[Calendar].[Settlement Calendar Month]),
            [Measures].[Transaction Count] = 0 ) )
SELECT {[Measures].[Transaction Count] ,[Measures].[Zero Months in last 12]} ON 0, 
[Terminal].[terminal ID].members on 1
FROM [cubetxn]

I'm expecting if non zero where "transaction count" is (null), even if there is transaction count not null, there could be chances to have one month with empty tuples.

Can you please correct my query or someone can give me sample query which should able to give me total zero month txn count for last 12 month of each product.

Terminal ID    Transaction Count      Zero Months in last 12
----------     -----------------      -------------------------
All Terminals   504,112,053            0
Q1001             138,832              0
Q1002             (null)               0
Q1003              88,800              0
Q1004             (null)               0

2 个答案:

答案 0 :(得分:1)

第一个评论是,这是在进行计数/过滤时使用的更快的模式:

WITH 
  MEMBER [Measures].[Months With Zero Sales] AS 
    Sum
    (
      Descendants
      (
        {
            [Date].[Calendar].[Month].&[2008]&[1].Lag(12)
          : 
            [Date].[Calendar].[Month].&[2008]&[1]
        }
       ,[Date].[Calendar].[Month]
      )
     ,IIF
      (
        [Measures].[Sales Amount] = 0
       ,1
       ,null
      )
    ) 
SELECT 
  {[Measures].[Months With Zero Sales]} ON 0
 ,[Product].[Product Model Lines].[Product Line].MEMBERS ON 1
FROM [Adventure Works];

基于这种更有效的方法可以做到以下工作吗?

WITH 
  MEMBER [Measures].[Zero Months in last 12] AS 
    Sum
    (
      Descendants
      (
        {
            [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].Lag(12)
          : 
            [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]
        }
       ,[Settlement Date].[Calendar].[Settlement Calendar Month]
      )
     ,IIF
      (
        [Measures].[Transaction Count] = 0
       ,1
       ,0
      )
    ) 
SELECT 
  {
    [Measures].[Transaction Count]
   ,[Measures].[Zero Months in last 12]
  } ON 0
 ,[Terminal].[terminal ID].MEMBERS ON 1
FROM [cubetxn];

如果仍然存在问题,那么为什么不重新构建脚本,以便手动检查预期结果。因此,对于AdvWrks脚本,我会运行它,以便我可以确实确认6是正确的:

SELECT 
  {[Measures].[Sales Amount]} ON 0
 ,
    Descendants
    (
      {
          [Date].[Calendar].[Month].&[2008]&[1].Lag(12)
        : 
          [Date].[Calendar].[Month].&[2008]&[1]
      }
     ,[Date].[Calendar].[Month]
    )
  * 
    [Product].[Product Model Lines].[Product Line].MEMBERS ON 1
FROM [Adventure Works];

因此,针对您的多维数据集,以下内容会返回什么内容?

SELECT 
  {[Measures].[Transaction Count]} ON 0
 ,
    Descendants
    (
      {
          [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].Lag(12)
        : 
          [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]
      }
     ,[Settlement Date].[Calendar].[Settlement Calendar Month]
    )
  * 
    [Terminal].[terminal ID].MEMBERS ON 1
FROM [cubetxn];

答案 1 :(得分:0)

//Total Transactions count Weekend Sunday (i’ve date dimesion which holds day_in_week=’Sun’ for respective dates)

WITH SET AllTranSundays as
EXISTS(DATE.DATE.DATE.MEMBERS, DATE.DATE.day_in_week.&[Sun], "Sales")
//Count of all sundays which had transactions. 
MEMBER Measures.CntAllTranSundays AS
COUNT(AllTranSundays)

//YTD Days = Count of days within calendar year to date

MEMBER Measures.CntDaysYTD as 
COUNT(DESCENDANTS(ANCESTOR([DATE].[date].CURRENTMEMBER, 
3), 3).item(0).item(0) 
: [DATE].[date].CURRENTMEMBER)

//YTD Transactions = Transaction count year to date

MEMBER Measures.CntTranYTD as 
COUNT(
EXISTS(
{DESCENDANTS(ANCESTOR([DATE].[date].CURRENTMEMBER, 
3), 3).item(0).item(0) 
: [DATE].[date].CURRENTMEMBER}, , "Sales"
)

//Zero months in last 12 = Number of zero transacting months in the last 12 months rolling window

MEMBER Measures.CntZeroTransactingMonths as
COUNT(
    {ANCESTOR([DATE].[date].CURRENTMEMBER, 1).ITEM(0).LAG(12) : ANCESTOR([DATE].[date].CURRENTMEMBER, 1).ITEM(0)}
    -
    EXISTS({ANCESTOR([DATE].[date].CURRENTMEMBER, 1).ITEM(0).LAG(12) : ANCESTOR([DATE].[date].CURRENTMEMBER, 1).ITEM(0)}
    , , 
    "Sales")
)

//Consecutive zero months = Number of consecutive zero transacting months in last 12 months rolling window

问题当连续几个月没有交易时,您是否需要实例计数? 或者你想要连续没有交易的月数?

我想说的是1月,3月,6月,7月,8月,10月,11月在过去12个月里没有交易 根据案例1:答案应该是2 根据案例2:答案应为5。

根据您的澄清,我会更新我的答案。

SELECT 
{Measures.CntAllTranSundays, Measures.CntDaysYTD, Measures.CntTranYTD, Measures.CntZeroTransactingMonths} ON 0
FROM [YourCube]
where 
[date].[date].[date].&[02/28/2015]