简体   繁体   中英

Average only for NonEmpty - SSAS MDX

For one day (several datetime records possible) I want to create a 2nd measure computing an AVG only for nonempty values based on my 1st measure. I've tried the following :

"CREATE MEMBER CURRENTCUBE.[Measures].[M2] 
AS AVG([Measures].[M1]),
VISIBLE = 1 ,  ASSOCIATED_MEASURE_GROUP = 'DATA' ;"

But the SSAS AVG function seems to return me a Sum:

DateTime "M1"
1/1/16 12:10 "10"
1/1/16 13:10 "12"
1/1/16 14:10
1/1/16 15:10 "9"
1/1/16 16:10
1/1/16 17:10 "21"
1/1/16 18:10
1/1/16 19:10 "2"

Average for nonempty Measure values (10+12+9+21+2)/5 nonempty Values = 10,8

If you want an average daily value then @whytheq showed a way to do it in MDX. But I believe you want a simple average. The proper way to do that in SSAS is to create a measure M1 tied to SQL column M1 with AggregateFunction=Sum (which you already have) and a second measure M1_Count on SQL column M1 in that measure group with an AggregateFunction=Count.

Then create a calculated measure:

CREATE MEMBER CURRENTCUBE.[Measures].[M1 Avg] as
IIF([Measures].[M1_Count]=0,null,[Measures].[M1]/[Measures].[M1_Count]);

You need to calculate your average over a set so you need to introduce a first argument to the AVG function.

From here:
https://msdn.microsoft.com/en-us/library/ms146067.aspx?f=255&MSPPError=-2147217396

This is the definition:

Avg( Set_Expression [ , Numeric_Expression ] )

Instead of AVG([Measures].[M1]) try this:

AVG(
 nonempty([Date].[Calendar].[Date], [Measures].[M1])
 ,[Measures].[M1]
)

...but I believe the AVG function will take care of nulls for you. So the following should be equivalent:

AVG(
  [Date].[Calendar].[Date]
 ,[Measures].[M1]
)

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