2014-03-05 81 views
1

我正在試圖計算一個立方體中的百分位數(例如我的測量值的第90個百分點),我想我已經差不多了。我面臨的問題是,我能夠返回第90百分位數的行數,但不知道如何得到我的度量。計算SSAS中的百分位值

With 
Member [Measures].[cnt] as 
Count(NonEmpty(
-- dimensions to find percentile on (the same should be repeated again 
[Calendar].[Hierarchy].members * 
[Region Dim].[Region].members * 
[Product Dim].[Product].members 
, 
-- add the measure to group 
[Measures].[Profit])) 

-- define percentile 
Member [Measures].[Percentile] as 90 

Member [Measures].[PercentileInt] as Int((([Measures].[cnt]) * [Measures].[Percentile])/100) 

**-- this part finds the tuple from the set based on the index of the percentile point and I am using the item(index) to get the necessary info from tuple and I am unable to get the measure part 
Member [Measures].[PercentileLo] as 
(
Order(
NonEmpty(
    [Calendar].[Hierarchy].members * 
    [Region Dim].[Region].members * 
    [Product Dim].[Product].members, 
    [Measures].[Profit]), 
    [Measures].[Profit].Value, BDESC)).Item([Measures].[PercentileInt]).Item(3)** 

select 
{ 
[Measures].[cnt], 
[Measures].[Percentile],[Measures].[PercentileInt], 
[Measures].[PercentileLo], 
[Measures].[Profit] 
} 
on 0 
from 
[TestData] 

我認爲必須有一種方法來獲得通過集合的索引找到的元組度量。請幫助,讓我知道你是否需要更多信息。謝謝!

回答

0

您應該從您的集合中提取位置爲[Measures].[PercentileInt]的元組,並向其添加度量以構建一個包含四個元素的元組。然後你想返回它的價值作爲措施PercentileLo,我。即限定

Member [Measures].[PercentileLo] as 
(
[Measures].[Profit], 
Order(
NonEmpty(
    [Calendar].[Hierarchy].members * 
    [Region Dim].[Region].members * 
    [Product Dim].[Product].members, 
    [Measures].[Profit]), 
    [Measures].[Profit], BDESC)).Item([Measures].[PercentileInt]) 
) 

你實現它的方式,你試圖提取第四(如Item()開始從零計數)從僅包含三個元素的元組項。你的有序集只有三個層次。

只是另一個無關的評論:我認爲你應該避免使用[Calendar].[Hierarchy].members[Region Dim].[Region].members[Product Dim].[Product].members的完整層次結構。您的代碼看起來像包含計算中的所有級別(包括全部成員)。但我不知道你的立方體的結構和名稱,因此我可能是錯的。

+0

這樣嗎?會員[措施]。[PercentileLo]爲Order( 非空( [日曆]。[層次] .members * [地區節電]。[區域] .members * [產品節電]。[產品] .members, [措施]。[利潤]), [措施]。[利潤]。價值,BDESC))。項目([措施]。[PercentileInt]) – Prashant

+0

嘗試此並且不起作用。 – Prashant

+0

@Prashant你是什麼意思是「不起作用」?只用這句話,我不知道如何改進我的答案。你有錯誤信息嗎?它說什麼?它是否運行,但給出的價值不像您期望的那樣?如果是這樣,你期望什麼,返回的價值是多少? – FrankPl

0

另一種方法是查找表中最後20%記錄的中位數。我用這個函數組合來找到第75個百分點。通過將記錄數除以5,可以使用TopCount函數返回一組元組,該元組構成整個表的20%,並按照您的目標度量降序排列。然後,中位數函數應該將您置於正確的第90百分位值,而無需查找記錄的座標。在我自己的使用中,我對Median和TopCount函數中的最後一個參數使用了相同的度量。

這裏是我的代碼:

WITH MEMBER Measures.[90th Percentile] AS MEDIAN(
    TOPCOUNT(
     [set definition] 
     ,Measures.[Fact Table Record Count]/5 
     ,Measures.[Value by which to sort the set so the first 20% of records are chosen] 
    ) 
    ,Measures.[Value from which the median should be determined] 
) 

根據你在問題的定義所提供的是什麼,我希望你的代碼看起來是這樣的:

WITH MEMBER Measures.[90th Percentile] AS MEDIAN(
    TOPCOUNT(
     { 
      [Calendar].[Hierarchy].members * 
      [Region Dim].[Region].members * 
      [Product Dim].[Product].members 
     } 
     ,Measures.[Fact Table Record Count]/5 
     ,[Measures].[Profit] 
    ) 
    ,[Measures].[Profit] 
)