我是MDX查询的新手,正在尝试弄清楚如何使用日期维度过滤结果集。
让我们采用这样的立方体(人为的例子):
我想向用户提供要选择的项目列表,并显示所选项目期间(即开始日期和结束日期之间)发生的所有事件的成本。但是,事件与项目无关。
使用查询:
SELECT NON EMPTY
{
[Measures].[Cost]
}
ON COLUMNS,
NON EMPTY
{
(
[Project Details].[Project].[Project].ALLMEMBERS
* [Project Details].[Start Date].[Start Date].ALLMEMBERS
* [Project Details].[End Date].[End Date].ALLMEMBERS
* [Event Details].[Date of Occurrence].[Date of Occurrence].ALLMEMBERS
)
}
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME
ON ROWS
FROM [Cube]
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
我可以得到这样的项目列表:
Project Start Date End Date Date of Occurrence Cost
------------------------------------------------------------------
Project 1 01-Jan-15 31-Jan-15 27-Dec-14 750
Project 1 01-Jan-15 31-Jan-15 01-Jan-15 680
Project 1 01-Jan-15 31-Jan-15 02-Jan-15 320
Project 1 01-Jan-15 31-Jan-15 03-Jan-15 150
Project 1 01-Jan-15 31-Jan-15 01-Feb-15 700
Project 1 01-Jan-15 31-Jan-15 05-Feb-15 175
如果我仅运行Project 1的查询,则应排除第一个事件和最后两个事件。
最好的方法是使用WHERE还是FILTER?因为这些是维度而不是度量,我如何比较WHERE [Date of Occurrence] BETWEEN [Start Date] AND [End Date]
?
非常感谢任何帮助。
答案 0 :(得分:1)
我会尝试这样的事情:
WITH MEMBER [Measures].[Cost in period] AS
IIF(
[Event Details].[Date of Occurrence].CurrentMember.Properties('Key') >=
[Project Details].[Start Date].CurrentMember.Properties('Key') &&
[Event Details].[Date of Occurrence].CurrentMember.Properties('Key') <=
[Project Details].[End Date].CurrentMember.Properties('Key'),
[Measures].[Cost], NULL)
SELECT NON EMPTY
{
[Measures].[Cost in period]
}
ON COLUMNS,
NON EMPTY
{
(
[Project Details].[Project].[Project].ALLMEMBERS
* [Project Details].[Start Date].[Start Date].ALLMEMBERS
* [Project Details].[End Date].[End Date].ALLMEMBERS
* [Event Details].[Date of Occurrence].[Date of Occurrence].ALLMEMBERS
)
}
ON ROWS
FROM [Cube]
基本上,您创建一个计算度量,当出现日期位于开始日期 - 结束日期间隔之外时,该度量为NULL。感谢Row成员上的NON EMPTY
,数据应该从结果中过滤掉。
答案 1 :(得分:0)
如果[Event Details]
和[Project Details]
为role playing dimensions,您可以使用LINKMEMBER
MDX功能来帮助您。此外,我假设您将使用某种前端(也许是SSRS)来让用户自由选择开始和结束日期(作为参数或日历控件)。在这种情况下,他们将以字符串形式输入MDX查询。 STRTOMEMBER
函数将这些字符串转换为成员。
使用LINKMEMBER
,一旦我生成了一组日期,我就会使用AGGREGATE
函数来获取这组日期的度量的聚合值。
with set [Start Date] as
linkmember(STRTOMEMBER('[Project Details].[Start Date].[Start Date].&[01/01/2014]'), [Event Details].[Date of Occurrence])
set [End Date] as
linkmember(STRTOMEMBER('[Project Details].[End Date].[End Date].&[01/01/2015]'), [Event Details].[Date of Occurrence])
set ListOfDate as
{[Start Date].item(0):[End Date].item(0)}
member [Measure.NetCost] as
aggregate(ListOfDates, [Measures].[Cost])
SELECT NON EMPTY
{
[Measure.NetCost]
}
ON COLUMNS,
NON EMPTY [Project Details].[Project].[Project].ALLMEMBERS
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME
ON ROWS
FROM [Cube]
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
免责声明:如上所述,仅当[Event Details]
和[Project Details]
是角色扮演尺寸时才有效。
我不确定此代码是否适用于您的方案,但我发现此功能有时非常方便。要详细了解LINKMEMBER
功能,请参阅here。
答案 2 :(得分:0)
未经测试。我没用过&amp;&amp;之前 - 也许只是关键字AND就足够了:
SELECT NON EMPTY
{
[Measures].[Cost]
}
ON COLUMNS,
NON EMPTY
{
(
[Project Details].[Project].[Project].ALLMEMBERS
* [Project Details].[Start Date].[Start Date].ALLMEMBERS
* [Project Details].[End Date].[End Date].ALLMEMBERS
* [Event Details].[Date of Occurrence].[Date of Occurrence].ALLMEMBERS
)
}
HAVING [Event Details].[Date of Occurrence].CurrentMember.MEMBERVALUE >=
[Project Details].[Start Date].CurrentMember.MEMBERVALUE
&& //<< OR IS THIS JUST "AND"?
[Event Details].[Date of Occurrence].CurrentMember.MEMBERVALUE <=
[Project Details].[End Date].CurrentMember.MEMBERVALUE
ON ROWS
FROM [Cube]