MDX How to retrieve data based on the latest date

风流意气都作罢 提交于 2019-12-12 06:17:05

问题


How to retrieve data based on the latest date for that particular record only.

SELECT 
  [Measures].[Assessment Patients Detail] ON COLUMNS
 ,NON EMPTY 
    (
      [DimAssessment].[Assessment Text].&[Employee Wellness HRA]
     ,[DimAssessment].[Question Text].&[Do you use tobacco products?]
     ,[DimPatient].[Patient Key].[Patient Key]
     ,Generate
      (
        [DimAssessment].[Answer Text].[Answer Text].MEMBERS
       ,
          [DimAssessment].[Answer Text].CurrentMember
        * 
          Tail
          (
            NonEmpty
            (
              [DimDate].[Full Date Alternate Key].[Full Date Alternate Key].MEMBERS
             ,[DimAssessment].[Answer Text].CurrentMember
            )
           ,[DimPatient].[Patient Key]
          )
      )
    ) ON ROWS
FROM [CareManagement];

回答1:


what you are after is probably LastNonEmpty aggregation

The LastNonEmpty function is a semi-additive function, and requires a dimension of type Time in order to be applicable (this can be set in the Dimension Properties), this is because it behaves differently when a measure is aggregating along the Time dimension, than it would when aggregating that same measure along any other (non-Time) dimension.

Taken from the following link: http://thinknook.com/ssas-lastnonempty-aggregation-function-2012-08-18/

  • LastNonEmpty Behavior along the Time Dimension: The last (earliest) existing value along the time segment is returned, so if you are aggregating monthly, then the value on the last day of the month that has a value is returned.
  • LastNonEmpty Behavior along Non-Time Dimensions: The SUM aggregation function is applied, and the value returned is the SUM of the measure along all aggregated (non-Time typed) dimensions.



回答2:


I think you're using the wrong hierarchy inside the Generate?

SELECT 
  [Measures].[Assessment Patients Detail] ON 0
 ,NON EMPTY 
      {[DimAssessment].[Assessment Text].&[Employee Wellness HRA]}*
      {[DimAssessment].[Question Text].&[Do you use tobacco products?]}*
      {Generate
      (
        [DimPatient].[Patient Key].[Patient Key].MEMBERS
       ,
          [DimPatient].[Patient Key].CurrentMember
        * 
          Tail
          (
            NonEmpty
            (
              [DimDate].[Full Date Alternate Key].[Full Date Alternate Key].MEMBERS
             ,[DimPatient].[Patient Key].CurrentMember
            )
          )
      )}*
      {[DimAssessment].[Answer Text].[Answer Text].MEMBERS} ON 1
FROM [CareManagement];

Here is a similar scenario in AdvWrks - several users on SO use this cube so we can all play:

SELECT 
  [Measures].[Internet Sales Amount] ON 0
 ,NON EMPTY 
      {
        [Customer].[Customer].[Alexis Thomas]
       ,[Customer].[Customer].[Carlos Morgan]
       ,[Customer].[Customer].[Carlos Scott]
      }*
      [Customer].[Customer Geography].[Country]*
      [Date].[Calendar].[Month] ON 1
FROM [Adventure Works];

It gives this:

I can start adding in the Generate attempt and I get as far as the following but as soon as I add in Tail the results disappear - unsure why it is behaving this way:

SELECT 
  [Measures].[Internet Sales Amount] ON 0
 ,NON EMPTY 
    Generate
    (
      {
        [Customer].[Customer].[Alexis Thomas]
       ,[Customer].[Customer].[Carlos Morgan]
       ,[Customer].[Customer].[Carlos Scott]
      } AS S
     ,
        s.CurrentMember
      * 
        NonEmpty
        (
            [Date].[Calendar].[Month].MEMBERS
          * 
            [Customer].[Customer Geography].[Country]
         ,s.CurrentMember
        )
    ) ON 1
FROM [Adventure Works];


来源:https://stackoverflow.com/questions/36152936/mdx-how-to-retrieve-data-based-on-the-latest-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!