Hilmar Buchta

SQL Server 2005 | SQL Server 2008

It’s commonly known that you can provide a format string for measures like in the following example:

with MEMBER [Measures].[Sales Amount Fmt] AS  [Measures].[Sales Amount],
FORMAT_STRING = "$0,000",
NON_EMPTY_BEHAVIOR = { [Measures].[Sales Amount] }

select  [Measures].[Sales Amount Fmt] on 0,
[Product].[Subcategory].[Subcategory] on 1
from [Adventure Works]

The important part of the example above is the FORMAT_STRING property. Here, it formats our measure sales amount as a dollar value without positions after the decimal point and with a separator for thousands.

The result looks like this:

image

It should be noted that the locale (i.e. using the comma as separator character) is derived from certain system settings (see http://support.microsoft.com/kb/950598 for details). However, you may overwrite the local using the language property as shown below:

with MEMBER [Measures].[Sales Amount Fmt] AS  [Measures].[Sales Amount],
FORMAT_STRING = "$0,000",
LANGUAGE=1031,
NON_EMPTY_BEHAVIOR = { [Measures].[Sales Amount] }

select  [Measures].[Sales Amount Fmt] on 0,
[Product].[Subcategory].[Subcategory] on 1
from [Adventure Works]

The only difference compared to the query above is the LANGUAGE property. Being set to 1031 for Germany the result displays a point as separator character:

image

To get back to the topic of this post, it’s interesting that the FORMAT_STRING property doesn’t have to be a constant. You might play around with simple expressions like "$0,0"+"00" but you have full support for MDX expressions here.

Let’s assume you want to format your values differently depending on their size, you can easily do so with an expression. For example, let’s assume you want to display dollar values like this

573

as $573

12,319

as $12K

9,312,131

as $9M

This can be done with the following FORMAT_STRING property which can be used in your cube script or in your MDX query:

FORMAT_STRING =
iif([Measures].[Sales Amount]<1000,"$0",
iif([Measures].[Sales Amount]<1000000,"$0,K",
"$0,,M"
))

Using this expression for the FORMAT_STRING property, the result of the query above looks like this (I also kept the originally formatted value of the example above to make it easier to compare the results):

image

As you can see, the values in the example above show as Thousand or Million Dollars. I think that this looks a little bit confusing and I would prefer showing all values at the same scale but at least you can use this feature to set different formatting options.

Furthermore you could think of displaying the correct unit (like meters, square meters, kilogram) with the values depending on a dimension attribute or you could display the right currency code depending on the currency of the transaction. In the Adventure Works cube for example, you can find the following cube script, which sets the language property for the destination currency dimension:

/*– Set language property for the Destination Currency cube dimension –*/
Scope
(
   [Destination Currency].[Destination Currency Code].Members,
   [Destination Currency].[Destination Currency].[Destination Currency].Members

) ;

   Language( This ) =
   [Destination Currency].[Destination Currency].Properties( "Locale" ) ;

End Scope ;

In this case, the locale is stored as a dimension attribute (for example 1033 for US, see http://msdn.microsoft.com/en-us/library/0h88fahh(VS.85).aspx for an overview of the locale codes). The script results in the correct visualization on the currency dimension like shown in the following query:

select  [Measures].[Sales Amount] on 0,
[Destination Currency].[Destination Currency].[Destination Currency] on 1
from [Adventure Works]

image