试图理解mdx计算

时间:2016-09-22 23:23:43

标签: sql-server ssas mdx

我试图理解这个MDX查询背后的数学。

TL; DR:这个查询是如何运行的? (" div"如何计算?)

WITH 
member [Customer].[Country].[x] as (([Customer].[Country].[Australia]) - ([Customer].[Country].[France], [measures].[sum sales]))
member [measures].[div] as ([Measures].[Sum Sales] / [Measures].[Sum Cost])

SELECT  
{ [measures].[div] } 
ON COLUMNS, 
{ [Customer].[Country].[x] } 
ON ROWS 

长篇故事:

我有[客户]的层次结构。[国家]并衡量销售和成本。

对于此查询:

SELECT  
{ [Measures].[Sum Sales], [Measures].[Sum Cost] } 
ON COLUMNS, 
{ [Customer].[Country].[Australia] , [Customer].[Country].[France] } 
ON ROWS 

我得到的结果是:

Australia: cost = 8266005, sales = 18005422
France:    cost = 4478131, sales = 9610041

我创建了这个成员:

// substract france from australia
member [Customer].[Country].[x] as (([Customer].[Country].[Australia]) - ([Customer].[Country].[France]))

并使用它而不是2个成员运行查询:

WITH 
member [Customer].[Country].[x] as (([Customer].[Country].[Australia]) - ([Customer].[Country].[France]))

SELECT  
{ [Measures].[Sum Sales], [Measures].[Sum Cost] } 
ON COLUMNS, 
{ [Customer].[Country].[x] } 
ON ROWS 

得到了结果:

x: cost = 3787874, sales = 8395381

这是有道理的。然后我改变法国在会员中使用销售,如下所示:

WITH 
member [Customer].[Country].[x] as (([Customer].[Country].[Australia]) - ([Customer].[Country].[France], [measures].[sum sales]))

SELECT  
{ [Measures].[Sum Sales], [Measures].[Sum Cost] } 
ON COLUMNS, 
{ [Customer].[Country].[x] } 
ON ROWS 

得到了这个结果:

x: cost = -1344036, sales = 8395381

我可以看到,对于澳大利亚来说,该栏目的衡量标准正在被使用,对于法国而言,元组中的销售额是强大的"一个,并保持成本。意思是"成本"减法实际上是(AU,成本) - (FR,销售)。

所以我试试这个(这就是问题!)

我设置了一个自定义度量:

member [measures].[div] as ([Measures].[Sum Sales] / [Measures].[Sum Cost])

并将其与其他措施一起放在列上:

WITH 
member [Customer].[Country].[x] as (([Customer].[Country].[Australia]) - ([Customer].[Country].[France], [measures].[sum sales]))
member [measures].[div] as ([Measures].[Sum Sales] / [Measures].[Sum Cost])

SELECT  
{ [Measures].[Sum Sales], [Measures].[Sum Cost], [measures].[div] } 
ON COLUMNS, 
{ [Customer].[Country].[x] } 
ON ROWS 

x销售和成本的结果是相同的。 " DIV" = -6.2463950147143。 它是如何计算的? 我猜它应该是:

(AU,sales) / (AU,cost) - (FR,sales) = 18005422 / 8266005 - 9610041 = -9610038.82175

但那错了......

我从法国购买了销售

 member [Customer].[Country].[x] as (([Customer].[Country].[Australia]) - ([Customer].[Country].[France]]))

并得到了预期的结果:

 div = ((AU,sales) - (FR,sales)) / ((AU,cost) - (FR,cost)) = (18005422 - 9610041) / (8266005 - 4478131) = 2.216383

感谢您提供的任何帮助

1 个答案:

答案 0 :(得分:0)

最后它来找我了!

我得到的数字是:

div = ((AU,sales) - (FR,sales)) / ((AU,cost) - (FR,sales)) = (18005422 - 9610041) / (8266005 - 9610041) = -6.2463959

当法国需要在计算中使用一种措施时,它就会得到销售。这是规则。

希望这将有助于未来的某些人,虽然似乎没有人对此感兴趣...... :(