MDX 查询在另一个度量上过滤了 count distinct,与其他过滤器一起使用

MDX query with count distinct filtered on another measure, that plays with other filters

在 SSAS AdventureWorks 架构中,我试图计算出有多少不同客户的 Internet 订单超过 1000 美元的不同计数。

我试图像这样进行 MDX 查询,它似乎 几乎 工作,除了新度量似乎忽略了切片器轴/WHERE 条件,显示 所有 个国家的客户数量,而不仅仅是澳大利亚:

With 
member [measures].[DistinctCustomersHighSales] as 
distinctcount(
   filter([Customer].[Full Name].Members, [Measures].[Internet Sales-Sales Amount] > 1000)
)
SELECT {
   [Measures].[Internet Sales-Sales Amount],
   [Measures].[DistinctCustomersHighSales]
    }
 on columns,
([Date].[Calendar Date].[Calendar Year].Members) on rows
FROM [Analysis Services Tutorial]
WHERE [Customer].[Customer Geography].[Country-Region].[Australia]

我做错了什么?

如果我在 SQL 中这样做,我会寻找这样的东西

 SELECT Year(OrderDate) as year,
     sum(SalesAmount) 
     count(case when count(distinct case when SalesAmount > 1000 then dc.customerkey end)
 FROM dbo.FactInternetSales  fis
    join dbo.dimcustomer dc on fis.CustomerKey=dc.CustomerKey
    join dbo.DimGeography  dg on dc.GeographyKey=dg.GeographyKey
 WHERE EnglishCountryRegionName='Australia'
 GROUP BY year(OrderDate)
 ORDER BY year

尝试添加 EXISTING:

With 
member [measures].[DistinctCustomersHighSales] as 
distinctcount(
   filter(EXISTING [Customer].[Full Name].Members, [Measures].[Internet Sales-Sales Amount] > 1000)
)
SELECT {
   [Measures].[Internet Sales-Sales Amount],
   [Measures].[DistinctCustomersHighSales]
    }
 on columns,
([Date].[Calendar Date].[Calendar Year].Members) on rows
FROM [Analysis Services Tutorial]
WHERE [Customer].[Customer Geography].[Country-Region].[Australia]

作用域是MDX中一个非常重要的概念。只有当你加上EXISTING时,引擎才实现切片器。

成员定义中的初始集合是:

[Customer].[Full Name].Members

集合在 MDX 中是静态的。因此,此集合默认包含所有客户。当您添加 EXISTING 时,在形成集合之前,会检查上下文。根据多维数据集中的维度使用(关系),它能够过滤属于澳大利亚的客户。这样就可以了。