THE SQL Server Blog Spot on the Web
Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

Microsoft OLAP by Mosha Pasumansky

Displaying duration values – MDX expressions in FORMAT_STRING

Suppose you have a calculation (or even a real measure) which returns elapsed time or duration. We want to present it to the user as such, i.e. break into hours, minutes and seconds. This sounds easy enough, we would just use appropriate FORMAT_STRING. If we read documentation, we quickly find that by using ‘hh’ for hours, ‘mm’ for minutes and ‘ss’ for seconds, we can have ‘hh:mm:ss’ formatting, i.e.

with member measures.delta as 0.14159
 ,format_string = 'hh:mm:ss'
select {measures.delta} on 0
from [Adventure Works]

will produce ‘03:23:53’ result. This works great, but if duration is greater than one day, we need to include number of days elapsed as well. So it may look like the format string ‘d “days” hh:mm:ss’ would work, but there are few problems with using ‘d’ inside format string. It is supposed to give the day of the month, therefore it can not work for more than 31 days, and it has other undesired effects on durations – like returning 31 for values between 0 and 1, and 30 for values between 1 and 2. So we need better solution. And the solution is to use real MDX expression to build FORMAT_STRING calculation property.

I discovered that even some of the advanced MDX users were surprised that MDX expressions can be used with FORMAT_STRING. Certainly, everybody knows that it is possible to use MDX expressions for other calculations properties, like BACK_COLOR – there is whole chapter dedicated to it in my “Fast Track to MDX” book, or even more exotic ones, like LANGUAGE – see this blog for example. But there are no examples of using MDX expressions in the most popular and widely used calculation property – FORMAT_STRING. (Perhaps the reason for that in AS2000 it was not possible, but it certainly is possible according to OLEDB for OLAP spec, and both AS2005 and AS2008 fully support it).

So in our case one way to write such expression would be

with member measures.delta as 3.14159
 ,format_string 
   = 
    IIF(
      measures.delta < 1
     ,'d "days" hh:mm:ss'
     ,'"' + cstr(int(measures.delta)) + ' days" hh:mm:ss')
select {measures.delta} on 0
from [Adventure Works]
And we get expected result of ‘3 days 03:23:53’ as a result.
Published Friday, September 26, 2008 9:01 PM by mosha
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Marco Russo (SQLBI) said:

I admit I never thought that an expression can be assigned to a format_string property. Nice to know!

September 28, 2008 9:42 AM
 

kevinW said:

This is exactly what I'm looking for. Can you use the MDX expressions in the Format_String property of a measure in the Cube Designer? If so, how?

Re: Timespans

Comment Reply Quote Delete Edit

I understand that, what I was asking is how do I get the code into the Format_String?

I copied the ..

IIF(

     measures.delta < 1

    ,'d "days" hh:mm: ss'

    ,'"' + cstr(int(measures.delta)) + ' days" hh:mm: ss')

... into the Format_String property of my measure in the cube designer (changing the measure name as appropriate) and it did not work.

October 6, 2008 10:54 AM
 

kevinW said:

Oops. ignore the previous remark, I had some extra stuff on the clipboard when I pasted it. What I was trying to say was ...

This is exactly what I'm looking for. Can you use the MDX expressions in the Format_String property of a measure in the Cube Designer? If so, how?

I copied the ..

IIF(

    measures.delta < 1

   ,'d "days" hh:mm: ss'

   ,'"' + cstr(int(measures.delta)) + ' days" hh:mm: ss')

... into the Format_String property of my measure in the cube designer (changing the measure name as appropriate) and it did not work.

October 6, 2008 11:11 AM
 

mosha said:

Kevin - I don't think you can put MDX expression into FormatString property of the physical measure in the designer, you will have to do it in MDX Script, i.e.

Format_String(Measures.RealMeasure) = <expression>;

October 10, 2008 12:55 AM
 

Hasanain said:

Hi Mosha,

This is something that I have also been looking for. I have implemented this by setting up a new Calculated Member.

I created a Report Model based on the new cube. The true data type of this new member is still "float", when the report model is created it reflects this data type. Is there a way to ensure that this new format appears in Report  Model and subsequently any reports that are created?

Thanks

October 14, 2008 6:42 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement