简体   繁体   中英

how to use MDX average function?

I am VERY new to mdx, and about to learn it. i got into trouble right now ,because i want to get average amount for each weekday between two dates. For example between 1st of november and 1st of december there are 4 mondays, so average would be [Measures].[Amount] / 4, but i really cant figure out how to express this simple function in MDX

    with  member [Measures].[Avg] as
                    avg([Dim Date].[Day Of Week].[Day Of Week]
                     , [Measures].[amount])

 SELECT NON EMPTY { ([Measures].[Avg]), ([Measures].[Amount])} ON COLUMNS,
 NON EMPTY { ([Dim Date].[Day Of Week].[Day Of Week].ALLMEMBERS  * [Dim Date].[Date Int].[Date Int].ALLMEMBERS )} 
  ON ROWS FROM ( SELECT ( { [Dim Client].[Common Client UID].&[{xx}] } ) 
                         ON COLUMNS FROM ( SELECT ( [Dim Date].[Date Int].&[20151115] : [Dim Date].[Date Int].&[20151215] )
                         ON COLUMNS FROM [ff])) 
                         WHERE ( [Dim Client].[Common Client UID].&[{xx}] )

This query give me each weekday and all the dates which are bound to the specific weekday, and for each date there are an total amount.

在此处输入图片说明 like you can see my average is the same as total. I thought that i could just select all the weekdays between two date, find out for example how many mondays there are and divide it number with amount. but i couldnt figure that out either.

    with  member [Measures].[avg] as
                    avg([Dim Date].[Day Of Week].[Day Of Week],  [Measures].[Amount])



 SELECT NON EMPTY {[Measures].[avg], [Measures].[Amount]} ON COLUMNS, NON EMPTY { ([Dim Date].[Day Of Week].[Day Of Week].ALLMEMBERS  )} 
  ON ROWS FROM ( SELECT ( { [Dim Client].[Common Client UID].&[{xx}] } ) 
                         ON COLUMNS FROM ( SELECT ( [Dim Date].[Date Int].&[20151115] : [Dim Date].[Date Int].&[20151215] )
                         ON COLUMNS FROM [ff])) 
                         WHERE ( [Dim Client].[Common Client UID].&[{xx}] )

result :

在此处输入图片说明

How do i solve this?

What you really want is average amount over the dates corresponding to that weekday for the given date range.

You can just move the date range to the definition of calculated member, so that when value for a particular weekday is evaluated, the date range is also considered.

with  member [Measures].[Avg] as
avg
  (
   [Dim Date].[Day Of Week].CURRENTMEMBER * {[Dim Date].[Date Int].&[20151115] : [Dim Date].[Date Int].&[20151215]}
   ,[Measures].[amount]
  )

 SELECT NON EMPTY { ([Measures].[Avg]), ([Measures].[Amount])} ON COLUMNS,
 NON EMPTY [Dim Date].[Day Of Week].[Day Of Week].ALLMEMBERS * [Dim Date].[Date Int].[Date Int].ALLMEMBERS ON ROWS 
 FROM [ff]
 WHERE ( [Dim Client].[Common Client UID].&[{xx}] )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM