Practice

Preparation

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.

The Results pane is populated by Analysis Services, and the dataset shown in Illustration 1 appears.

Illustration 1: Result Dataset - Basic Use of the AVG() Function

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 ` SaveAs, 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!

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.

Illustration2: Result Dataset - More Sophisticated Use of the AVG() Function

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 ` SaveAs, 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.