繁体   English   中英

如何在 MDX 中按度量值进行过滤,同时在两个轴上都有维度成员

[英]How to filter by measure values in MDX while having dimension members in both axis

我正在开发一个使用表格数据库来显示一些业务数据的应用程序。

我需要对度量值(等于、大于、小于等)提供一些基本过滤,我目前正在分析生成 MDX 的正确方法。

查看一些文档(以及本站点上的其他线程),我发现最有效的方法是使用FILTERHAVING函数来过滤掉不需要的值。

不幸的是,所有示例通常都包括一个轴上的度量和另一个轴上的维度成员,但我可能在两个轴上都有维度成员,并且找不到使用此类函数按度量值过滤的正确解决方案。

到目前为止我做了什么?

为了便于解释,假设我们希望按产品类别过滤数量获得年销售数量 > 130 万

尝试使用 HAVING 或 FILTER 函数,我想出的结果 MDX 是

SELECT 
NON EMPTY {[YearList].[Year].[Year].MEMBERS * [Measures].[Qty]} 
    HAVING [Measures].[Qty] > 1.3e6 ON COLUMNS,
NON EMPTY {[Classes].[cClass].[cClass].MEMBERS} 
    HAVING [Measures].[Qty] > 1.3e6 ON ROWS
FROM [Model]

或者

SELECT 
NON EMPTY FILTER({[YearList].[Year].[Year].MEMBERS * [Measures].[Qty]}, 
    [Measures].[Qty] > 1.3e6) ON COLUMNS,
NON EMPTY FILTER({[Classes].[cClass].[cClass].MEMBERS} , 
    [Measures].[Qty] > 1.3e6) ON ROWS
FROM [Model]

但这当然会给最终用户带来意想不到的结果,因为过滤器仅在该轴上的维度对数量进行聚合,该维度大于 1.3M

结果错误

到目前为止,我发现实现我需要的唯一方法是使用IIF语句定义自定义成员

WITH
    MEMBER [Measures].[FilteredQty] AS 
    IIF ( [Measures].[Qty] > 1.3e6, [Measures].[Qty], NULL)

SELECT 
    NON EMPTY {[YearList].[Year].[Year].MEMBERS * [Measures].[FilteredQty]} ON COLUMNS,
    NON EMPTY {[Classes].[cClass].[cClass].MEMBERS} ON ROWS
FROM [Model]

结果是预期的:

预期结果

这是最好的方法还是我应该继续使用 FILTER 和 HAVING 函数 有没有更好的方法我仍然缺少? 谢谢

这是最好的方法。 您需要考虑 MDX 如何解析结果。 在上面的示例中,您的有效数据位于第一行前四列的连续区域中是巧合。 让我们放宽过滤条款并使其> 365000。 现在看看结果的最后一行,前两列和最后一列是符合条件的单元格,但第三和第四列不符合条件。 但是,您的查询会将其报告为 null,非空函数将无济于事。 原因是非空需要整行都为空 现在问题是为什么过滤器不消除单元格? 当条件大于另一个轴上的总和时,过滤器将消除一行或一列。 因此,如果过滤器位于列上,则过滤器值必须大于该列的行总和。 删除评论后,请查看下面的示例,最后一列将被删除。

select 
non empty
filter(
([Measures].[Internet Sales Amount]
,{[Date].[Calendar Year].&[2013],[Date].[Calendar Year].&[2014]}
,[Date].[Calendar Quarter of Year].[Calendar Quarter of Year]
),([Date].[Calendar Year].currentmember,[Date].[Calendar Quarter of Year].currentmember,[Product].[Subcategory].currentmember,[Measures].[Internet Sales Amount])>45694.70--+0.05
)
on columns 
,
non empty
[Product].[Subcategory].members
on rows
from
[Adventure Works]

在此处输入图片说明

编辑添加的另一个示例。

with 
member [Measures].[Internet Sales AmountTest]
as 
iif(([Date].[Calendar Year].currentmember,[Date].[Calendar Quarter of Year].currentmember,[Product].[Subcategory].currentmember,[Measures].[Internet Sales Amount])>9000,
([Date].[Calendar Year].currentmember,[Date].[Calendar Quarter of Year].currentmember,[Product].[Subcategory].currentmember,[Measures].[Internet Sales Amount]),
null
)


select 
non empty
({[Measures].[Internet Sales Amount],[Measures].[Internet Sales AmountTest]}
,{[Date].[Calendar Year].&[2013]}
,[Date].[Calendar Quarter of Year].[Calendar Quarter of Year]
)
on columns 
,
non empty
[Product].[Subcategory].[Subcategory]
on rows
from
[Adventure Works]

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM