MDX中的四分位计算

时间:2017-10-25 16:11:52

标签: reporting-services ssas mdx

以下是我用于计算四分位数的代码,该代码适用于单个国家/地区和品牌。

WITH
SET [Contracts] AS 
FILTER ( CrossJoin ([Dim Temps].[Hierarchy].[YEAR MONTH].&[201707],[Dim CONTRAT].[LABEL].Children,[Dim Brand].[BRAND  FILENAME].&[BRAND_A], [DIM AAA Country].[COUNTRY CODE].&[FR]), 
[Measures].[Nb 12M]  >= 40 AND [Measures].[MyMeasure] > 0)

SET [ContratEligible] AS TopCount ([Contracts], Count([Contracts]), [Measures].[MyMeasure]) 

    MEMBER [Measures].[RowCount] AS Count([Contracts])
    MEMBER [Measures].[i25] AS 0.25 * ([RowCount] - 1) + 1
    MEMBER [Measures].[i25Lo] AS Fix([i25]) - 1
    MEMBER [Measures].[i25Rem] AS [i25] - Fix([i25])
    MEMBER [Measures].[n25Lo] AS ( [ContratEligible].Item([i25Lo]), [Measures].[MyMeasure])
    MEMBER [Measures].[n25Hi] AS ( [ContratEligible].Item([i25Lo] + 1), [Measures].[MyMeasure])
    MEMBER [Measures].[i75] AS 0.75 * ([RowCount] - 1) + 1 
    MEMBER [Measures].[i75Lo] AS  Fix([i75]) - 1
    MEMBER [Measures].[i75Rem] AS [i75] - Fix([i75])
    MEMBER [Measures].[n75Lo] AS ([ContratEligible].Item([i75Lo]), [Measures].[MyMeasure])
    MEMBER [Measures].[n75Hi] AS ([ContratEligible].Item([i75Lo] + 1) ,[Measures].[MyMeasure])
    MEMBER [Measures].[Quartile1] AS [n25Lo] + [i25Rem] * ([n25Hi] - [n25Lo])
    MEMBER [Measures].[Quartile2] AS  Median ([ContratEligible],[Measures].[MyMeasure])
    MEMBER [Measures].[Quartile3] AS [n75Lo] + [i75Rem] * ([n75Hi] - [n75Lo])
    SET [ContratEligible_Quartile1] AS NonEmpty(Filter([ContratEligible], [Measures].[MyMeasure] >= [Measures].[Quartile1]))
    SET [ContratEligible_Quartile2] AS NonEmpty(Filter([ContratEligible], [Measures].[MyMeasure] >= [Measures].[Quartile2] AND [Measures].[MyMeasure] < [Measures].[Quartile1]))
    SET [ContratEligible_Quartile3] AS NonEmpty(Filter([ContratEligible], [Measures].[MyMeasure] >= [Measures].[Quartile3] AND [Measures].[MyMeasure] < [Measures].[Quartile2]))
    SET [ContratEligible_Quartile4] AS NonEmpty(Filter([ContratEligible],[Measures].[MyMeasure] < [Measures].[Quartile3]))
    MEMBER    [Measures].[maxQ4]    AS max([ContratEligible_Quartile4],[Measures].[MyMeasure])
    MEMBER    [Measures].[minQ4]     AS min([ContratEligible_Quartile4],[Measures].[MyMeasure])
    MEMBER    [Measures].[avgQ4]      AS avg([ContratEligible_Quartile4],[Measures].[MyMeasure])
    MEMBER    [Measures].[maxQ3]    AS max([ContratEligible_Quartile3],[Measures].[MyMeasure])
    MEMBER    [Measures].[minQ3]     AS min([ContratEligible_Quartile3],[Measures].[MyMeasure])
    MEMBER    [Measures].[avgQ3]     AS avg([ContratEligible_Quartile3],[Measures].[MyMeasure])
    MEMBER    [Measures].[maxQ2]   AS max([ContratEligible_Quartile2],[Measures].[MyMeasure])
    MEMBER    [Measures].[minQ2]    AS min([ContratEligible_Quartile2],[Measures].[MyMeasure])
    MEMBER    [Measures].[avgQ2]     AS avg([ContratEligible_Quartile2],[Measures].[MyMeasure])
    MEMBER    [Measures].[maxQ1]     AS max([ContratEligible_Quartile1],[Measures].[MyMeasure])
    MEMBER    [Measures].[minQ1]     AS min([ContratEligible_Quartile1],[Measures].[MyMeasure])
    MEMBER    [Measures].[avgQ1]     AS avg([ContratEligible_Quartile1],[Measures].[MyMeasure])

 SELECT NON EMPTY {  [Measures].[Quartile1], [Measures].[Quartile2],[Measures].[Quartile3],
[Measures].[maxQ1] ,[Measures].[avgQ1] ,[Measures].[minQ1],
[Measures].[maxQ2] ,[Measures].[avgQ2] ,[Measures].[minQ2] ,
[Measures].[maxQ3] ,[Measures].[avgQ3] ,[Measures].[minQ3] ,
[Measures].[maxQ4] ,[Measures].[avgQ4] ,[Measures].[minQ4]} ON COLUMNS,
NON EMPTY  {([DIM AAA Country].[COUNTRY CODE].&[FR],[Dim Brand].[BRAND FILENAME].&[BRAND_A])} ON ROWS
 FROM [CUBE_A]

当我更换查询中的某些字段以查找多个国家/地区和品牌的四分位数时,我会为所有国家/地区和品牌获得相同的结果。 以下是我用来计算多个国家和品牌的四分位数的查询,

WITH
SET [Contracts] AS 
FILTER ( CrossJoin ([Dim Temps].[Hierarchy].[YEAR MONTH].&[201707],[Dim CONTRAT].[LABEL].Children,[Dim Brand].[BRAND  FILENAME].CurrentMember, [DIM AAA Country].[COUNTRY CODE].CurrentMember), 
[Measures].[Nb 12M]  >= 40 AND [Measures].[MyMeasure] > 0)
....
-- No changes 
....

SELECT NON EMPTY {  [Measures].[Quartile1], [Measures].[Quartile2], [Measures].[Quartile3],
[Measures].[maxQ1] ,[Measures].[avgQ1] ,[Measures].[minQ1],
[Measures].[maxQ2] ,[Measures].[avgQ2] ,[Measures].[minQ2] ,
[Measures].[maxQ3] ,[Measures].[avgQ3] ,[Measures].[minQ3] ,
[Measures].[maxQ4] ,[Measures].[avgQ4] ,[Measures].[minQ4]} ON COLUMNS,
NON EMPTY  {([DIM AAA Country].[COUNTRY CODE].Children*[Dim Brand].[BRAND FILENAME].Children)} ON ROWS
FROM [CUBE_A]

0 个答案:

没有答案