mdx 从周期中选择数据
Posted
技术标签:
【中文标题】mdx 从周期中选择数据【英文标题】:mdx select data from period 【发布时间】:2016-05-26 12:47:17 【问题描述】:对于基于 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]
【问题讨论】:
您的多维数据集的日期维度中是否有未来的日期,或者是今天多维数据集中的最后一个日期? (我们的立方体没有未来的日子,所以我知道Tail([Date].[Month Calendar].[Date])
是今天)
你有这样[Date].[Month Calendar].[Calendar Day]
的关卡吗?
是的,我有未来几天的预测
我有层次结构 date.month.week.day
【参考方案1】:
这是在多维数据集中查找 Today 的开始。我已将“度量”移至切片器并将“今天”移至列:
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];
【讨论】:
【参考方案2】:这里还有另一种方法:
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])
【讨论】:
【参考方案3】:感谢您的帮助
我用成员定义了 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];
【讨论】:
感谢 Sourav 和我的帮助,您可以“提升”我们的答案以上是关于mdx 从周期中选择数据的主要内容,如果未能解决你的问题,请参考以下文章