过去 6 个等效 MTD 中的天数集

Set of Days in past 6 equivalent MTDs

假设今天是 2010 年 6 月 22 日(我使用这个日期是因为 AdvWrks 立方体是旧的)

我想找到构成这个月的一组天数,即 6 月 1 日至 6 月 22 日加上迄今为止前 5 个等效月份中的天数。

所以这些天将组成一组:

(2010 年 1 月 1 日 - 2010 年 1 月 22 日)+
(2010 年 2 月 1 日 - 2010 年 2 月 22 日)+
(2010 年 3 月 1 日 - 2010 年 3 月 22 日)+
(2010 年 4 月 1 日 - 2010 年 4 月 22 日)+
(2010 年 5 月 1 日 - 2010 年 5 月 22 日)+
(2010 年 6 月 1 日 - 2010 年 6 月 22 日)

下面给我这套132天的:

WITH 
  SET [Days in Current Month] AS 
     [Date].[Calendar].[Date].&[20100601]:[Date].[Calendar].[Date].&[20100622]
  SET [Mths in Past 6 Mths] AS 
    Tail
    (
      [Date].[Calendar].[Month].MEMBERS
     ,6
    ) 
  SET [Prev Equiv MTDs] AS 
    Generate
    (
      [Mths in Past 6 Mths]
     ,Head
      (
        Descendants
        (
          [Mths in Past 6 Mths].CurrentMember
         ,[Date].[Calendar].[Date]
         ,SELF
        )
       ,[Days in Current Month].Count
      )
    ) 
SELECT 
  {} ON 0
 ,[Prev Equiv MTDs] ON 1
FROM [Adventure Works];

不使用Generate函数是否可以找到这组天数?

这是一种方法:

WITH SET FirstDateOfMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET LastDayOfMonth AS [Date].[Calendar].[Date].&[20100622]


SELECT {} ON 0,
//CurrentMonth
    {   
        FirstDateOfMonth.ITEM(0)
        :
        LastDayOfMonth.ITEM(0)
    }

+
//-1 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        1,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        1,
                        LastDayOfMonth.ITEM(0)
                      )

    }   
+
//-2 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        2,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        2,
                        LastDayOfMonth.ITEM(0)
                      )

    }   
+
//-3 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        3,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        3,
                        LastDayOfMonth.ITEM(0)
                      )

    }   
+
//-4 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        4,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        4,
                        LastDayOfMonth.ITEM(0)
                      )

    }   

+
//-5 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        5,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        5,
                        LastDayOfMonth.ITEM(0)
                      )

    }   

 ON 1
FROM [Adventure Works]

还有一个:

SELECT 
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.FirstChild
        :
        COUSIN
        (
        [Date].[Calendar].[Date].&[20100622], 
        [Date].[Calendar].[Date].&[20100622].PARENT
        )

    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(1).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(1)
              )
    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(2).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(2)
              )
    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(3).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(3)
              )
    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(4).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(4)
              )
    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(5).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(5)
              )
    }
    ON 1,
{} ON 0
FROM [Adventure Works]

还有一个...

WITH SET FirstDateOfMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET LastDayOfMonth AS [Date].[Calendar].[Date].&[20100622]
MEMBER Measures.PositionOfDate AS {FirstDateOfMonth.ITEM(0):LastDayOfMonth.ITEM(0)}.COUNT

SET Dates AS 
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(1).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(2).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(3).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(4).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(5).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )


SELECT Dates ON 0,
{} ON 1
FROM [Adventure Works]

也是这样:

WITH SET FirstDateOfSelectedMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET SelectedDate AS [Date].[Calendar].[Date].&[20100622]
MEMBER Measures.PositionOfDate AS {FirstDateOfSelectedMonth.ITEM(0):SelectedDate.ITEM(0)}.COUNT

SELECT

        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(5).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(4).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(3).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(2).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(1).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0)
                    ) 

ON 1,
{} ON 0
FROM [Adventure Works]

编辑

非重复块的慢代码:

WITH SET FirstDateOfSelectedMonth AS [Date].[Calendar].[Date].&[20100601].Parent.FirstChild
SET SelectedDate AS [Date].[Calendar].[Date].&[20100601]
MEMBER Measures.PositionOfDate AS {FirstDateOfSelectedMonth.ITEM(0):SelectedDate.ITEM(0)}.COUNT
MEMBER Measures.PositionOfCurrentDate AS 
([Date].[Calendar].currentmember.Parent.FirstChild:[Date].[Calendar].currentmember).COUNT

SELECT 
    {
    [Date].[Calendar].[Date].&[20100601].Parent.LAG(5).FirstChild.ITEM(0)
    :
    [Date].[Calendar].[Date].&[20100601]
    }
    HAVING Measures.PositionOfCurrentDate <= Measures.PositionOfDate
ON 1,
{} ON 0
FROM [Adventure Works]