Wednesday, May 27, 2009

MDX for Business - Text as a calculated measure

It surprises many developers that you can have a text value as a calculated measure, since a measure usually contains an aggregated number. However, a text value is very simple and very useful in a calculated measure. It could be as simple as getting a property of a member such as a customer's mobile number -

Member Measures.MobileNumber as Customers.Customer.properties("Mobile Number")
I have a real example, in a report that shows hours on rows and the last three days on columns, and for each cell it shows the top City (customer, product, whatever) by virtue of the number of web requests in that hour. This is shown on the default page of my web dashboard on http://EasternMining.com.au/Sites/Demonstrations See down near the bottom is a table called Top Cities by Hour - Last 3 Days. It shows the city with the most activity on my site for that hour. It might look ahead of time to you, if you are in America or Europe. It is actually very close to real-time in Sydney Australia.

Here is the mdx

With Member Measures.TopCity as
Topcount([Client Host].[Client Geography].[City]
,1,measures.[request count]).item(0).member_caption
Select
tail([Date].[Year Month Day].[Day],3) on Columns,
[HoursOfDay].[HoursOfDay].[Hour Of Day] on Rows
From EasternMining
Where Measures.TopCity


See how simple the mdx is. Again, imagine having to write the same query in SQL. Also, if I want to change something in the report, such as last 5 days, last 3 months etc, it is very simple. In fact, If I chage [City] to [Region] I get the following report. This report tends to show the time zones of Asia, Europe and Americas. See how Americas tend to be early morning (Sydney time) while Europe is late at night and Asia is visiting during the online day.


It's so very easy with MDX. If the answer is in the data, then one MDX expression should be able to ask the question. Let me know if you need any assistance in writing or learning mdx.

2 comments:

Monica said...

Hi Richard,

i am facing problem with having Text as measure. I have requirement wher I hvae GroupType and GroupName for a customer site. Each customer has defined GroupType. Now what result I am looking for is, Have GroupType in Column and CustomerSIte in Row and measure as GroupName. But i am not getting how to write MDX for this!
Can you please help me out in this!

Thanks,
Monica

Richard Lees said...

Hi Monica,
I am sure this is possible. In fact, any question can be answered in MDX if the cube holds the information.
Here is a query I wrote to determine the Internet Explorer of my users, Region by Resource.
with member measures.Browser as
topcount(nonempty([Client Agent].[Client Agent],[Measures].[Bytes Total]),1)(0).member_caption

select non empty [Client Host].[Client Geography].[Region] on 0,

non empty filter([Target].[Resource Hierarchy].[Resource Type],[Measures].[Hits]>1000) on 1

from [EasternMining]
where measures.Browser

Naturally, the syntax of the query will differ depending on your cube structure, but you can do it.