Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum Rss Feed

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
Free Newsletters:

News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted Aug 9, 2004

MDX Essentials: Basic Member Functions: The .Item() Function - Page 2

By William Pearson



To reinforce our understanding of the basics we have covered so far, we will use the .Item() function in a manner that illustrates its operation. We will do so in simple scenarios that place .Item() within the context of meeting a business need.

To begin, we will construct a SELECT query with a clearly defined set, then put .Item() to use in manipulating that set to meet the business needs of a group of hypothetical information consumers. The intent is, of course, to demonstrate the operation of the .Item() 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 units shipped for a given time frame upon which they plan to perform some basic analysis. The Finance information consumers specifically wish to know the total units shipped figure for the US store states, for each of the quarters of 1998, having become focused on this state / time combination initially to do some per unit analysis of various logistics costs.

We will first create a basic query that returns the units shipped totals for each of the US store states, over the quarters under consideration. It is from the results dataset that is returned that the consumers state that they may want to narrow their request, once they get a look at overall figures, to more focused view(s) of specific state(s) only.

Let's construct a simple query, therefore, to return the units shipped information, presented by quarters (as columns) and the store states (as rows).

5.  Type the following query into the Query pane:

-- MDX022-1, Preparation for Use of .Item() Function, Numerical Index, 
-- in a    Basic Query
   {[Time].[1998].Children} ON COLUMNS,
   {[Store].[All Stores].[USA].Children}  ON ROWS
   ([Measures].[Units Shipped])

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 2 appears.

Illustration 2: Result Dataset - Preparation for Use of .Item() Function

We see the four quarters of 1998 populating the columns across, and the Store States (from the Stores dimension) appearing on the row axis.

7.  Select File -> Save As, name the file MDX022-1, and place it in a meaningful location.

8.  Leave the query open for the next section.

Next, let's say that our information consumers are provided with the full set of data that we have generated. They examine the metrics, and then request that we narrow the report scope to "Oregon-only" stores.

We realize, from looking at our first results dataset, and perhaps in Analysis Manager, etc., that Oregon is the second of the three members of the tuple we have defined within our initial query. We decide that the .Item() function, with an index setting of 1 ("0" being the first member position, and "1" the second) will serve to generate the new limitation on the results.

We take the following actions to meet the new consumer request:

9.  Within the query we have saved as MDX022-1, replace the top comment line of the query with the following:

-- MDX022-2, Use of .Item() Function, Numerical Index, in a  Basic Query

10.  Save the query as MDX022-2, to prevent damaging MDX022-1.

11.  Append the .Item() function to the rows axis definition of the query, changing the following line:

   {[Store].[All Stores].[USA].Children}  ON ROWS

to the following

                 {[Store].[All Stores].[USA].Children.Item(1)} ON ROWS

The Query pane appears as shown in Illustration 3, with the new addition circled.

Illustration 3: The Query with Added .Item() Function on the Row Axis

12.  Execute the query by clicking the Run Query button in the toolbar.

The Results pane is populated, and the dataset depicted in Illustration 4 appears.

Illustration 4: Result Dataset - .Item() Function with Numerical Index

13.  Re-save the file as MDX022-2.

14.  Leave the query open for the next step.

We have used the .Item() function, in its member incarnation, with a simple numerical index. Let's see an example where we use a string within the index, based upon our original query above, where we selected all three store states.

15.  Within the query we have saved as MDX022-2, replace the top comment line of the query with the following:

-- MDX022-3, Use of .Item() Function, String Index, in a  Basic Query

16.  Save the query as MDX022-3.

17.  Inside the parentheses of .Item() function, replace the existing numeral ("1") with the following string:


The Query pane appears as shown in Illustration 5, with changes.

Illustration 5: The Query with Index modified to String

18.  Execute the query by clicking the Run Query button in the toolbar.

The Results pane is populated, and the dataset depicted in Illustration 6 appears.

Illustration 6: Result Dataset - .Item() Function with String Index

19.  Resave the query as MDX022-3, leaving the sample application open for the steps that follow.

We deliver the new results to the Finance consumers, who accept our work thus far with stated satisfaction. They promise to "get back to us" with follow-up requests in short order.

We next hear from a representative of Finance a couple of days later, with a request that contains elements similar to the specifications so far. The group needs the units shipped totals for the Washington stores, from the perspective of non-consumable goods sold only, for the first three quarters of 1998.

We recognize instantly the opportunity, in this case, to combine both the member and tuple versions of the .Item() function into a single query, and set out to do so in the following steps.

20.  Create a new query by selecting Query --> New on the Sample Application toolbar.

21.  Type the following into the Query pane:

-- MDX022-4, Use of .Item() Function, Member and Tuple (MultiString Index)
   {[Time].[1998].Children.Item(0): [Time].[1998].Children.Item(2) } ON COLUMNS,
{CROSSJOIN([Store].[All Stores].[USA].Children, [Product].
   [Product Family].Members).Item("[WA]","[Non-Consumable]")} ON ROWS
   ([Measures].[Units Shipped])

As we can see, we are using the member variation of the .Item() function to return the three quarters (indexes 0, 1, and 2, respectively), the range of which we manage conveniently with the colon (":") operator. In addition, we are using the tuple variation of .Item(), with multiple string indexing option, in the rows axis, where we are cross joining store states and product family. We comply with the rules for the multi-string index approach, in that we stipulate each dimension present in the tuple, in appropriate order, and so we obtain the desired results. (Try changing the order of the strings, or otherwise manipulate them, to see the results, as convenient).

NOTE: For more information on the CrossJoin() function, see my Database Journal article MDX Essentials: Basic Set Functions: The CrossJoin() Function .

22.  Execute the query by clicking the Run Query button in the toolbar.

The Results pane is populated, and the dataset shown in Illustration 7 appears.

Illustration 7: Result Dataset - Combined .Item() Functions

23.  Save the file as MDX22-4.

Summary ...

In this lesson, we explored the MDX .Item() function. We discussed the tandem general objectives of the function, to return a member from a tuple, or a tuple from a set, and then we examined the syntax for each of the two variations.

We then undertook illustrative practice examples of business uses for the .Item() function, generating queries that capitalized on its primary features. Finally, we briefly discussed the results datasets we obtained from each of the queries we constructed.

» See All Articles by Columnist William E. Pearson, III

Discuss this article in the MSSQL Server 2000 Analysis Services and MDX Topics Forum.

MS SQL Archives

Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM