如何使用 MDX SELECT MAX 测量值和最大值的日期 return

How to SELECT MAX measure using MDX and return both Value and Date of Max Value

我有下面的 MDX 查询:

WITH 
      MEMBER [Measures].[MaxValue] AS
      MAX([Date].[Fiscal].[Fiscal Year].Members, [Measures].[Sales Amount]) 
SELECT 
      {[Measures].[MaxValue]} ON COLUMNS
  
FROM
      [Adventure Works]

它 return 的值:

MaxValue

,714,102.75

我想 return 日期和最大值。我可以通过下面的查询实现类似的效果。

WITH 
      MEMBER [Measures].[MaxValue] AS
      MAX([Date].[Fiscal].[Fiscal Year].Members, [Measures].[Sales Amount]) 
SELECT 
      {[Measures].[Sales Amount],[Measures].[MaxValue]} ON COLUMNS,
      {[Date].[Fiscal].[Fiscal Year].Members} ON ROWS 
FROM
      [Adventure Works]



+---------+----------------+----------------+
|         |  Sales Amount  |    MaxValue    |
+---------+----------------+----------------+
| FY 2005 | (null)         | ,714,102.75 |
| FY 2006 | ,360,526.01 | ,714,102.75 |
| FY 2007 | ,683,804.82 | ,714,102.75 |
| FY 2008 | ,714,102.75 | ,714,102.75 |
| FY 2009 | ,840.63     | ,714,102.75 |
| FY 2010 | (null)         | ,714,102.75 |
| FY 2011 | (null)         | ,714,102.75 |
+---------+----------------+----------------+

我的目标是输出如下:

FY 2008 ,714,102.75 ,714,102.75

我猜您不必同时输出 Sales Amount 和 MaxValue 列,因为它们具有相同的值。如果您要查找销售额最高的年份,可以使用以下查询。

SELECT 
      {[Measures].[Sales Amount]} ON COLUMNS,
      ORDER({[Date].[Fiscal].[Fiscal Year].Members},[Measures].[Sales Amount],DESC).ITEM(0) ON ROWS 
FROM
      [Adventure Works]

TheDumbRadish 答案的可能替代方案:

SELECT 
  [Measures].[Sales Amount] ON 0
 ,TopCount
  (
    [Date].[Fiscal].[Fiscal Year].MEMBERS
   ,1
   ,[Measures].[Sales Amount]
  ) ON 1
FROM [Adventure Works];