mdx从期间

时间:2016-05-26 12:47:17

标签: reporting-services ssas mdx olap

对于基于OLAP的报告,我需要在当年和上一年的时间段内提取数据。

如果今天是2016-05-26, 然后我需要这些日期:2016-01-01 - 2016-05-26和2015-01-01 - 2015-05-26

 SELECT NON EMPTY 
    { [Measures].[Sell In Return] } ON COLUMNS, 

NON EMPTY { (

[Date].[Date].[Date].ALLMEMBERS * 
[Product].[Prod Name].[Prod Name].ALLMEMBERS * 
[Product].[Product].[Product].ALLMEMBERS * 
[Date].[Month Calendar].[Month Calendar].ALLMEMBERS

) } 

DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME 
ON ROWS 

FROM 
[Table]

3 个答案:

答案 0 :(得分:0)

这是在立方体中找到今天的开始。我已将Measures移动到切片器,今天移到了列上:

WITH 
  MEMBER [Measures].[Key for Today] AS 
    Format
    (
      Now()
     ,'yyyyMMdd'
    ) 
  MEMBER [Measures].[Today string] AS 
      '[Date].[Month Calendar].[Calendar Day].&[' + [Measures].[Key for Today]
    + ']' 
  SET [Today] AS 
    StrToMember
    (
      [Measures].[Today string]
     ,constrained
    ) 
  SET [TodayLstYr] AS 
    ParallelPeriod
    (
      [Date].[Date - Calendar Month].[Calendar Year]
     ,1
     ,[Today]
    ) 
  SET [ThisYear] AS 
    {
        Ancestor
        (
          [Today]
         ,[Date].[Month Calendar].[Calendar Day]
        ).Item(0)
      : 
        [Today]
    } 
  MEMBER [Date].[Date - Calendar Month].[All].[ThisYear] AS 
    Aggregate([ThisYear]) 
  SET [LastYear] AS 
    {
        Ancestor
        (
          [TodayLstYr]
         ,[Date].[Month Calendar].[Calendar Day]
        ).Item(0)
      : 
        [TodayLstYr]
    } 
  MEMBER [Date].[Date - Calendar Month].[All].[LastYear] AS 
    Aggregate([LastYear]) 
SELECT 
  NON EMPTY 
    {
      [Today]
     ,[Date].[Date - Calendar Month].[All].[ThisYear]
     ,[Date].[Date - Calendar Month].[All].[LastYear]
    } ON COLUMNS
 ,NON EMPTY 
      [Product].[Prod Name].[Prod Name].ALLMEMBERS
    * 
      [Product].[Product].[Product].ALLMEMBERS ON ROWS
FROM [Table]
WHERE 
  [Measures].[Sell In Return];

答案 1 :(得分:0)

这是获得它的另一种方法:

with set Today as  //today's date
filter([Date].[Month Calendar].[Date].members,
     Format([Date].[Month Calendar].currentmember.member_name, "Short Date") = 
format(now(), "Short Date"))

set MonthBegToToday as  //range of dates from current month's beginning till today
{today.item(0).firstsibling : today.item(0)}

set LastYearMonthBegToToday as  //range of dates from current month's beginning till today (using parallelperiod function to get the "same date last year")
PARALLELPERIOD([Date].[Month Calendar].[Year], 1, 
today.item(0).firstsibling)
:
PARALLELPERIOD([Date].[Month Calendar].[Year], 1, 
today.item(0))

member Measures.SalesMonthBegToToday as   //total sales by using SUM()
sum(MonthBegToToday, [Measures].[Sell In Return])

member Measures.SalesLastYearMonthBegToToday as 
sum(LastYearMonthBegToToday, [Measures].[Sell In Return])

答案 2 :(得分:0)

感谢您的帮助

我使用成员定义了2个句点,并将列添加到列

WITH 

  MEMBER [Measures].[Key for Today] AS Format   (Now() ,'yyyyMMdd'  ) 
  member [measures].[CurrentYear] as Format   (Now() ,'yyyy') 
  MEMBER [Measures].[TodayPrevYear] AS Format   (DateAdd("YYYY",-1,Now()) ,'yyyyMMdd') 
 Member [measures].[PrevYear] as Format ( dateadd("YYYY",-1,Now()) ,'yyyy') 

SELECT 
  NON EMPTY 
    {
        StrToMember ( '[Date].[Date Key].[Date Key].&[' +[measures].[PrevYear]+'0101'+  ']',constrained )
    :StrToMember ( '[Date].[Date Key].[Date Key].&[' + [Measures].[TodayPrevYear]  + ']',constrained )
    ,
    StrToMember ( '[Date].[Date Key].[Date Key].&[' +[measures].[CurrentYear]+'0101'+  ']',constrained )
    :StrToMember ( '[Date].[Date Key].[Date Key].&[' + [Measures].[Key for Today]  + ']',constrained )


    } ON COLUMNS
 ,NON EMPTY 

      [Product].[Product].[Product].ALLMEMBERS 
      * [Product].[SKUs].[SKU].ALLMEMBERS 

      ON ROWS
FROM [Table]
WHERE 
  [Measures].[Sell In Return];