<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Boyan Penev on Microsoft BI &#187; formatting</title>
	<atom:link href="http://www.bp-msbi.com/tag/formatting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bp-msbi.com</link>
	<description>A practical blog about Microsoft BI tools, techniques and practices written by a developer for other fellow developers.</description>
	<lastBuildDate>Sun, 29 Jan 2012 03:23:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Problems with FORMAT_STRING, VBA functions and NON_EMPTY_BEHAVIOR</title>
		<link>http://www.bp-msbi.com/2009/10/problems-with-formatstring-vba/</link>
		<comments>http://www.bp-msbi.com/2009/10/problems-with-formatstring-vba/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 02:28:00 +0000</pubDate>
		<dc:creator>Boyan Penev</dc:creator>
				<category><![CDATA[SSAS]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[FORMAT_STRING]]></category>
		<category><![CDATA[MDX]]></category>
		<category><![CDATA[NON_EMPTY_BEHAVIOR]]></category>
		<category><![CDATA[truncation]]></category>

		<guid isPermaLink="false">http://www.bp-msbi.com/2009/10/problems-with-format_string-vba-functions-and-non_empty_behavior/</guid>
		<description><![CDATA[Consider the following requirement: &#8220;We want our measure to be truncated to 4 decimal places without any trailing 0s after the decimal point and a comma as a thousands separator.&#8221; First, let&#8217;s focus on the truncation part. If we want that to happen for a measure, we can do it through the following formula, as [...]]]></description>
			<content:encoded><![CDATA[<p>Consider the following requirement: <em>&#8220;We want our measure to be truncated to 4 decimal places without any trailing 0s after the decimal point and a comma as a thousands separator.&#8221;<br />
</em><br />
First, let&#8217;s focus on the truncation part. If we want that to happen for a measure, we can do it through the following formula, as I have described previously:</p>
<blockquote><p><span style="font-family: courier new; font-size: 85%; color: #000066;">SCOPE([Measures].[Our Measure]);<br />
This = Fix([Measures].[Our Measure]*10^4)/10^4;<br />
END SCOPE;</span></p></blockquote>
<p>This takes care of our truncation. So far so good.</p>
<p>Now let&#8217;s have a look at the formatting. If we want to apply custom formatting through FORMAT_STRING for a number such as 12345.1234, which states: &#8220;#,0.####&#8221;, in order to obtain 12,345.1234 we run into a problem. The same FORMAT_STRING expression applied to 12345 gives us 12,345. &#8211; including a trailing decimal point (in our case a trailing period). There is no way to get rid of it through FORMAT_STRING. Even in the specifications for FORMAT_STRING it is pointed out that:</p>
<p><em>If the format expression contains only number sign (#) characters to the left of the period (.), numbers smaller than 1 start with a decimal separator.<br />
</em><br />
This holds true for #s to the right of the period, as well.</p>
<p>What we can do in this case is either conditionally format the number, with some sort of a rule, which checks if the number is whole or not (I will avoid that), or we can use the VBA Format function like this:</p>
<blockquote><p><span style="font-family: courier new; font-size: 85%; color: #000066;">SCOPE([Measures].[Our Measure]);<br />
Format([Measures].[Our Measure], &#8220;#,0.####&#8221;);<br />
END SCOPE;</span></p></blockquote>
<p>This yields the correct result, so in the end, we can do:</p>
<blockquote><p><span style="font-family: courier new;"><span style="font-size: 85%; color: #000066;">SCOPE([Measures].[Our Measure]);<br />
This = Format(Fix([Measures].[Our Measure]*10^4)/10^4, &#8220;#,0.####&#8221;);<br />
END SCOPE;</span><br />
</span></p></blockquote>
<p>That may be achieveing the result, but let&#8217;s look at perfromance of a calculated measure utilising this approach. The first thing I noticed when I implemented this is the huge increase in query processing time and the large number of 0 valued cells. It turned out that the VBA functions in MDX do not skip empty (NULL) cells. If you try <span style="color: #000066;">Fix(NULL)</span>, you&#8217;ll get 0. So, after Fix-ing our measure, we get 0s for every empty cell in our cube. The same is valid for Format.</p>
<p>Next step was trying to find a way to skip these empties. I tried:</p>
<blockquote><p><span style="font-family: courier new; font-size: 85%; color: #000066;">SCOPE([Measures].[Our Measure]);<br />
This = Format(Fix([Measures].[Our Measure]*10^4)/10^4, &#8220;#,0.####&#8221;);<br />
NON_EMPTY_BEHAVIOR(This) = [Measures].[Our Measure];<br />
END SCOPE;</span></p></blockquote>
<p>but it did not work. I still got the 0s in my result set. I suppose that it got ignored by SSAS. Because of this issue I decided to write an IIF statement like this:</p>
<blockquote><p><span style="font-family: courier new; font-size: 85%; color: #000066;">SCOPE([Measures].[Our Measure]);<br />
This = IIF([Measures].[Our Measure] = 0, NULL, Format(Fix([Measures].[Our Measure]*10^4)/10^4, &#8220;#,0.####&#8221;));<br />
END SCOPE;</span></p></blockquote>
<p>This also worked. However, now we have an IIF statement, which serves no purpose other than filtering our empty cells, because of the VBA functions&#8217; behavior. It would be interesting if there is any other way of implementing this, avoiding the problem. It would be nice if:</p>
<blockquote><p><span style="color: #660000;">1. VBA functions can skip empty cells<br />
2. FORMAT_STRING behaves just like Format<br />
3. NON_EMPTY_BEHAVIOR actually works with SCOPE (in queries)</span></p></blockquote>
<p>Please comment if you have a solution for the problems above and let me know if you would like to see the above issues fixed (I am considering raising a feedback/recommendation issue on Connect).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bp-msbi.com/2009/10/problems-with-formatstring-vba/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

