MDX查询-由于位置原因,YTD产生不正确的结果

时间:2019-04-18 02:45:54

标签: mdx

我想返回多个期间(例如2012年3月至2013年1月)的年初至今(即1月到当前期间)值。然而,可以理解的是,当我将where子句限制为“ 2012年3月至2013年1月”时,我们会损失YTD的1月和2月值。下面是一些示例代码:

learmore.php?cuarto="+results[i].Num_Quarto

上述查询的日期迄今不正确:

WITH 
  MEMBER [Measures].[Last Day of Month] AS 
    TAIL(EXISTING [Date].[Date].[Date].MEMBERS).ITEM(0).ITEM(0).MEMBER_CAPTION
  MEMBER [Measures].[Calculate YTD] AS 
    Sum
    (
      periodstodate(
        [Date].[Calendar].[Year],[Date].[Calendar].CurrentMember
      )
     ,[Measures].[Internet Total Sales]
    ) 
SELECT 
  { 
   [Measures].[Last Day of Month]
   ,[Measures].[Internet Total Sales]
   ,[Measures].[Calculate YTD]
  } ON COLUMNS,
  [Date].[Calendar].[Month] 
  ON ROWS
FROM [Adventure Works Internet Sales Model]
WHERE 
(
 [Date].[Date].&[2012-03-01T00:00:00]:
 [Date].[Date].&[2013-01-01T00:00:00]
);

我希望查询返回的值(正确的YTD值):

3-Mar   $373,483.01

4-Apr   $773,818.62

5-May   $1,132,696.51

6-Jun   $1,687,856.65

7-Jul   $2,132,414.88

8-Aug   $2,656,332.26

9-Sep   $3,142,509.71

10-Oct  $3,677,669.20

11-Nov  $4,215,624.71

12-Dec  $4,840,126.88

1-Jan   $29,456.25

2 个答案:

答案 0 :(得分:0)

尝试使用YTD功能。无论是否带有where子句,它都会为您提供正确的结果。

(AdventureWorks 2014,年初至今用于互联网销售量度量)

WITH 
  MEMBER [Measures].[Calculate YTD] AS 
   AGGREGATE(YTD(), [Measures].[Internet Sales Amount])
SELECT 
  { 
  [Measures].[Internet Sales Amount]
  ,[Measures].[Calculate YTD]
  } ON COLUMNS,
  [Date].[Calendar].[Month] 
  ON ROWS
FROM [Adventure Works]
WHERE 
(
 [Date].[Date].&[20120301]:
 [Date].[Date].&[20130101]
);

或带有PeriodsToDate:

WITH 

   MEMBER [Measures].[Calculate YTD] AS 
    sum
    (
      periodstodate(
       [Date].[Calendar].[Calendar Year],   
            [Date].[Calendar].currentmember
      )
     ,[Measures].[Internet Sales Amount]
    ) 


SELECT 
  { 
  [Measures].[Internet Sales Amount]
  ,[Measures].[Calculate YTD]
  } ON COLUMNS,
  [Date].[Calendar].[Month] 
  ON ROWS
FROM [Adventure Works]
WHERE 
(
 [Date].[Date].&[20120301]:
 [Date].[Date].&[20130101]
);

两个查询都返回相同的结果:

March 2012  $1,375,841.32
April 2012  $1,776,176.93
May 2012    $2,135,054.82
June 2012   $2,690,214.97
July 2012   $3,134,773.20
August 2012 $3,658,690.58
September 2012  $4,144,868.03
October 2012    $4,680,027.51
November 2012   $5,217,983.03
December 2012   $5,842,485.20
January 2013    $857,689.91

答案 1 :(得分:-1)

不太确定这两种方法中的任何一种都行得通,但是无论如何这里都可以...

Sub-select

WITH 
  MEMBER [Measures].[Last Day of Month] AS 
    TAIL(EXISTING [Date].[Date].[Date].MEMBERS).ITEM(0).ITEM(0).MEMBER_CAPTION
  MEMBER [Measures].[Calculate YTD] AS 
    Sum
    (
      periodstodate(
        [Date].[Calendar].[Year],[Date].[Calendar].CurrentMember
      )
     ,[Measures].[Internet Total Sales]
    ) 
SELECT 
  { 
   [Measures].[Last Day of Month]
   ,[Measures].[Internet Total Sales]
   ,[Measures].[Calculate YTD]
  } ON COLUMNS,
  [Date].[Calendar].[Month] 
  ON ROWS
FROM 
( 
SELECT
  [Date].[Date].&[2012-03-01T00:00:00]:
  [Date].[Date].&[2013-01-01T00:00:00] ON 0
FROM [Adventure Works Internet Sales Model] 
);

HAVING子句:

WITH 
MEMBER [Measures].[Last Day of Month VALUE] AS 
    TAIL(EXISTING [Date].[Date].[Date].MEMBERS).ITEM(0).ITEM(0).MEMBERVALUE 

  MEMBER [Measures].[Last Day of Month] AS 
    TAIL(EXISTING [Date].[Date].[Date].MEMBERS).ITEM(0).ITEM(0).MEMBER_CAPTION
  MEMBER [Measures].[Calculate YTD] AS 
    Sum
    (
      periodstodate(
        [Date].[Calendar].[Year],[Date].[Calendar].CurrentMember
      )
     ,[Measures].[Internet Total Sales]
    ) 
SELECT 
  { 
   [Measures].[Last Day of Month VALUE]
   ,[Measures].[Last Day of Month]
   ,[Measures].[Internet Total Sales]
   ,[Measures].[Calculate YTD]
  } ON COLUMNS,
  [Date].[Calendar].[Month] 
  HAVING 
    (
      [Measures].[Last Day of Month VALUE] >=CDATE('3/1/2012')   
      AND  [Measures].[Last Day of Month VALUE] <=CDATE('1/1/2013')
    ) 
  ON ROWS
FROM [Adventure Works Internet Sales Model];