MDX Essentials: Numeric Functions: Introduction to the AVG() Function - Page 2September 13, 2004 PracticePreparation To reinforce our understanding of the basics we have covered so far, we will use the AVG() function in a manner that illustrates its operation. We will do so in illustrative scenarios that place AVG() within the context of meeting a business need. To begin, we will construct a SELECT query with a clearly defined calculated member that contains the AVG() function, and then present the returned dataset, via the core query, in a way that meets the business needs of a group of hypothetical information consumers. The intent is, of course, to demonstrate the operation of the AVG() function in a straightforward manner. Let's return to the MDX Sample Application as a platform from which to construct and execute the MDX we examine, and to view the results datasets we obtain. 1. Start the MDX Sample Application. 2. Clear the top area (the Query pane) of any queries or remnants that might appear. 3. Ensure that FoodMart 2000 is selected as the database name in the DB box of the toolbar. 4. Select the Warehouse cube in the Cube drop-down list box. Let's assume, for our practice example, that we have received a call from the Finance department of the FoodMart organization, requesting some information surrounding Warehouse Sales for a given time frame upon which they plan to perform some basic analysis. The Finance information consumers specifically wish to know the average total Warehouse Sales figure by Store Type, for the entire FoodMart organization, and for the time periods included in the current Warehouse cube. In addition, the consumers would like to see the average totals broken out by Product Family, ideally "across the x-axis," as they describe it, to make the performance of the various family types readily obvious in relation to each other. We will meet the business need expressed by the Finance group by composing a query using a calculated member that contains the AVG() function. We will then present the returned data with a basic query that defines the presentation as requested, with the requested averages appearing in the appropriate arrangement. 5. Type the following query into the Query pane: -- MDX023-1 Basic Use of AVG() Function WITH MEMBER [Product].[AverageSales] AS 'Avg({[Product].[Product Family].Members}, [Measures].[Warehouse Sales])' SELECT { [Product].[Product Family].Members, [Product].[AverageSales]} ON COLUMNS, NON EMPTY { [Store Type].Members } on rows FROM [Warehouse] WHERE ([Measures].[Warehouse Sales]) 6. Execute the query by clicking the Run Query button in the toolbar. The Results pane is populated by Analysis Services, and the dataset shown in Illustration 1 appears. The above presentation meets the Finance group's requirements, permitting them to analyze average Warehouse Sales by Product Family component, for each of the Store Type groups. In addition, the column to the far right also allows them to see a simple combined average for all Products. (We will examine the generation of weighted averages in a subsequent article). 7. Select File ` Save As, name the file MDX023-1, and place it in a meaningful location. 8. Leave the query open for the next section. We provide the results we have assembled to the information consumers, who are delighted with the presentation. The following day, we receive a call asking for more information in a somewhat similar, but a little more sophisticated vein. This time, the Finance group wants us to generate averages for two additional measures, Unit Shipped and Warehouse Profits, with the same row axis of Store Type groups. But they throw us another complication with their next stipulation: They want the total averages for products that number among the top ten individual product performers. We are amused, once again, at the way that our consumers rapidly refine their requirements from the most basic requirements to gradually more elegant analysis. Seeing what they can have only leads to more in-depth requirements. The beautiful thing is that, the more we have met such needs, the more we can offer from the very start, and offer solutions within a preemptive, pilot development that gets our audiences where they need to be far faster than they could have imagined. Everyone benefits with well-founded business intelligence! We take the following actions to meet the new consumer request: 9. Select File ` New to open a blank query: 10. Type the following query into the Query pane: -- MDX023-2 More Sophisticated Example of Avg() Function in Use WITH SET [Leading10Products] AS 'TopCount ([Product].[Product Name].Members, 10, [Measures].[Warehouse Sales])' MEMBER [Product].[OverLeadingAvg] AS 'Avg ([Leading10Products])' SELECT { [Measures].[Warehouse Sales], [Measures].[Units Shipped], [Measures].[Warehouse Profit] } ON COLUMNS, NON EMPTY { [Store Type].Members } ON ROWS FROM [Warehouse] WHERE ([Product].[OverLeadingAvg]) 11. Execute the query by clicking the Run Query button in the toolbar. The Results pane is populated by Analysis Services, and the dataset shown in Illustration 2 appears. The above presentation meets the Finance group's requirements, permitting them to perform the analysis they have envisioned. In effect, the results dataset is comprised of three measures that are broken down by six Store Types (including the All Store Types level). Each of the measures is averaged for each of the ten highest performing Products within the context of Warehouse Sales. Another nuance in our solution is the absence of the numeric expression component in the AVG() function within our second calculated member. The function interprets this to mean that we are specifying the current members of the dimension and measure that belong to the set we have created just above. (Creation of a set whose purpose is to act as a proxy within a subsequent calculation, or other manipulation, in such a manner is an excellent way to achieve many such ends.) Finally, we employ the calculated member as the slicer via the WHERE clause. We thus generate averages for each measure across each of the ten leading products, coming to an "aggregate average," as it were, as shown in Illustration 2 above. 12. Select File ` Save As, name the file MDX023-2, and place it in a meaningful location. 13. Select File -` Exit when ready to close the Sample Application. Summary ...In this lesson, we explored the MDX AVG() function. We commented generally upon the purpose of the function, to return the average value of a numeric expression as assessed over a specified set, and overviewed its operation. We examined the syntax employed in using AVG(), and then undertook illustrative practice examples based upon providing solutions for the expressed business needs of a hypothetical group of information consumers. Finally, we concluded each illustrative scenario with a brief discussion of the results datasets we obtained. » See All Articles by Columnist William E. Pearson, III Discuss this article in the MSSQL Server 2000 Analysis Services and MDX Topics Forum. MDX Essentials Series
The LEVEL_NUMBER Member Property
The LEVEL_UNIQUE_NAME Intrinsic Member Property Intrinsic Member Properties: The HIERARCHY_UNIQUE_NAME Property Intrinsic Member Properties: The DIMENSION_UNIQUE_NAME Property Further Combination of BottomCount() with Other MDX Functions Combine BottomCount() with Other MDX Functions to Add Sophistication Basic Set Functions: The BottomCount() Function, Part I Intrinsic Member Properties: The MEMBER_VALUE Property Intrinsic Member Properties: The MEMBER_UNIQUE_NAME Property Intrinsic Member Properties: The MEMBER_NAME Property Intrinsic Member Properties: The MEMBER_KEY Property Intrinsic Member Properties: The MEMBER_CAPTION Property Set Functions: The StripCalculatedMembers() Function Set Functions: The AddCalculatedMembers() Function MDX Numeric Functions: The Min() Function MDX Numeric Functions: The Max() Function Set Functions: The .AllMembers Function MDX Essentials: Set Functions: The MeasureGroupMeasures() Function String Functions: The .Properties Function, Part II String Functions: The .Properties Function Logical Functions: IsGeneration(): Conditional Logic within Filter Expressions MDX Scripting Statements: Introducing the Simple CASE Statement Logical Functions: IsGeneration(): Conditional Logic within Calculations Logical Functions: IsAncestor(): Conditional Logic within Filter Expressions MDX Clauses and Keywords: Use HAVING to Filter an Axis Logical Functions: IsAncestor(): Conditional Logic within Calculations Logical Functions: IsSibling(): Conditional Logic within Filter Expressions Logical Functions: IsSibling(): Conditional Logic within Calculations MDX Operators: The IsLeaf() Operator: Conditional Logic within Filter Expressions MDX Operators: The IsLeaf() Operator: Conditional Logic within Calculations MDX Numeric Functions: The .Ordinal Function Other MDX Entities: Perspectives MDX Operators: The IS Operator MDX Set Functions: The Distinct() Function MDX Set Functions: The ToggleDrillState() Function Set Functions: The DrillUpLevel() Function Set Functions: The DrillDownLevelTop() and DrillDownLevelBottom() Functions MDX Set Functions: DrillDownLevel() MDX Set Functions: The DRILLUPMEMBER() Function MDX Essentials: Set Functions: The DRILLDOWNMEMBERTOP() and DRILLDOWNMEMBERBOTTOM() Functions MDX Essentials : Set Functions: The DRILLDOWNMEMBER() Function MDX Essentials: Drilling Through with MDX: The DRILLTHROUGH Statement MDX Essentials: String Functions: The .UniqueName Function MDX Essentials: String Functions: The .Name Function MDX Essentials: String / Numeric Functions: The CoalesceEmpty() Function MDX Essentials: Basic Set Functions: The TopCount() Function, Part II MDX Essentials: Basic Set Functions: The TopCount() Function, Part I MDX Essentials: Enhancing CROSSJOIN() with Calculated Members MDX Essentials: Set and String Functions: The GENERATE() Function MDX Essentials: The CROSSJOIN() Function: Breaking Bottlenecks MDX Essentials: String / Numeric Functions: More on the IIF() Function MDX Essentials: String / Numeric Functions: Introducing the IIF() Function MDX Essentials: Logical Functions: The IsEmpty() Function MDX Essentials: Basic Set Functions: The EXTRACT() Function MDX Essentials: Numeric Functions: Introduction to the AVG() Function MDX Essentials: Basic Member Functions: The .Item() Function MDX Essentials: Basic Set Functions: Subset Functions: The Subset() Function MDX Essentials: Basic Set Functions: Subset Functions: The Tail() Function MDX Essentials: Basic Set Functions: Subset Functions: The Head() Function MDX Essentials: Basic Set Functions: The CrossJoin() Function MDX Essentials: Basic Numeric Functions: The Count() Function MDX Essentials: Basic Set Functions: The Filter() Function MDX Essentials: Basic Set Functions: The EXCEPT() Function MDX Essentials: Basic Set Functions: The Intersect() Function MDX Essentials: Basic Set Functions: The Union() Function MDX Essentials: Basic Set Functions: The Order() Function MDX Essentials - MDX Time Series Functions, Part III: The LastPeriods() and ParallelPeriod() Functions MDX Time Series Functions, Part II: The OpeningPeriod () and ClosingPeriod() Functions MDX Essentials - MDX Time Series Functions, Part I: PeriodsToDate() and Kindred Functions MDX Essentials: MDX Member Functions: "Relative" Member Functions MDX Member Functions: The Cousin () Function MDX Essentials: Member Functions: More "Family" Functions MDX Member Functions: The "Family" Functions MDX Essentials: MDX Members: Introducing Members and Member MDX Essentials : MDX Operators: The Basics MDX Essentials: Structure of the MDX Data Model MDX at First Glance: Introduction to SQL Server MDX Essentials |