<?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>Made2Mentor &#187; Report Customization</title>
	<atom:link href="http://www.made2mentor.com/category/report-customization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.made2mentor.com</link>
	<description>Data Warehousing, Microsoft Business Intelligence, and Other Cool Stuff</description>
	<lastBuildDate>Thu, 19 Jan 2012 19:41:18 +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>Using a Date Table to Track Custom Fiscal Years/Periods</title>
		<link>http://www.made2mentor.com/2011/04/using-a-date-table-to-track-custom-fiscal-yearsperiods/</link>
		<comments>http://www.made2mentor.com/2011/04/using-a-date-table-to-track-custom-fiscal-yearsperiods/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 18:25:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Data Warehouse]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Reporting]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=5918</guid>
		<description><![CDATA[
			
				
			
		
<p>Calendar Tables are only useful if they reflect how your company evaluates its data. Therefore, any company using non-standard financial fiscal years and periods will require customization with their Calendar or Date Table. In this post, I&#8217;ll provide an example of how to do so using the M2M ERP System Database. For those who don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F04%2Fusing-a-date-table-to-track-custom-fiscal-yearsperiods%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F04%2Fusing-a-date-table-to-track-custom-fiscal-yearsperiods%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Calendar Tables are only useful if they reflect how your company evaluates its data. Therefore, any company using non-standard financial fiscal years and periods will require customization with their <a href="http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/">Calendar or Date Table</a>. In this post, I&#8217;ll provide an example of how to do so using the M2M ERP System Database. For those who don&#8217;t use M2M, the notes should serve as an example of how to do so with other systems. </p>
<p>The most effective way to determine the fiscal setup of a company is to determine how its major systems track the data. In M2M the source is the GL Rules and Periods (GLRule) table. There are four primary fields of interest, which I&#8217;ve aliased, and they are listed in the following query and screenshot.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> GLR.<span style="color: #202020;">fcname</span> AcctYearName
      ,GLR.<span style="color: #202020;">fnnumber</span> PeriodNo
      ,GLR.<span style="color: #202020;">fdstart</span> FirstDayOfPeriod
      ,GLR.<span style="color: #202020;">fdend</span> LastDayOfPeriod
  <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">glrule</span> GLR</pre></div></div>

<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/04/acct_year.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/04/acct_year.png" alt="" title="acct_year" width="422" height="290" class="aligncenter size-full wp-image-5921" /></a></p>
<p>As you can see, there is one record for each fiscal period in GLRule.  FCName is a Char(20) field and typically contains values like &#8220;FY 2005&#8243; or &#8220;Fiscal Year 2005&#8243;. FNNumber contains the period number and is an Int datatype. FDStart and FDEnd are Datetime fields and represent the first and last days of each period. </p>
<p>What follows are the update statements which I use to customize the fiscal periods of my calendar table. For your convenience, you can access the entire script here****. Copy and paste the entire query, replace the database and table name, and run it. I&#8217;ve dissected each step of the script below so those not using M2M can understand my logic and mimic those steps with their own systems. </p>
<p>Six fields are updated by the first statement. Notice that I&#8217;ve joined the Calendar Table to GLRule with a between statement. I&#8217;m using the pattern matching of Patindex to find the four digit number for Fiscal Year. However, if the user entered something like &#8220;Two Thousand Five&#8221; as a Fiscal Year, this method will not work. Start and end of period fields are taken directly out of GLRule as well. FiscalDayOfPeriod is calculated in this step as well for good measure.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Use</span> M2MDataXX
GO
<span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> TST.<span style="color: #808080;">&#91;</span>FiscalYearName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fcName</span>, <span style="color: #FF00FF;">patindex</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[1,2][9,0,1,2][0-9][0-9]%'</span>, GLR.<span style="color: #202020;">fcName</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalYearPeriod<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fcName</span>, <span style="color: #FF00FF;">patindex</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[1,2][9,0,1,2][0-9][0-9]%'</span>, GLR.<span style="color: #202020;">fcName</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">+</span> <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fnnumber</span> <span style="color: #808080;">/</span> <span style="color: #000;">100.00</span><span style="color: #808080;">&#41;</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalPeriod<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> GLR.<span style="color: #202020;">fnnumber</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalStartOfPeriodDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> GLR.<span style="color: #202020;">fdstart</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalEndOfPeriodDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> GLR.<span style="color: #202020;">fdend</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalDayOfPeriod<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEDIFF</span> <span style="color: #808080;">&#40;</span>dd, GLR.<span style="color: #202020;">fdstart</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">FROM</span>  DateTestStnd.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
       <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
        glrule GLR
       <span style="color: #0000FF;">ON</span> TST.<span style="color: #0000FF;">DATE</span> <span style="color: #808080;">BETWEEN</span> GLR.<span style="color: #202020;">fdstart</span> <span style="color: #808080;">AND</span> GLR.<span style="color: #202020;">fdend</span></pre></div></div>

<p>In this step, I&#8217;ve calculated the beginning and end of Fiscal Years. I group the record set in GLRule by Fiscal Year and then use min/max to determine the first and last day in each group.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> TST.<span style="color: #808080;">&#91;</span>FiscalStartOfYearDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> M2MYEARS.<span style="color: #808080;">&#91;</span>FiscalStartOfYearDate<span style="color: #808080;">&#93;</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalEndOfYearDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> M2MYEARS.<span style="color: #808080;">&#91;</span>FiscalEndOfYearDate<span style="color: #808080;">&#93;</span>
  <span style="color: #0000FF;">FROM</span>  DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
       <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
        <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fcName</span>, <span style="color: #FF00FF;">patindex</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[1,2][9,0,1,2][0-9][0-9]%'</span>, GLR.<span style="color: #202020;">fcName</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>
                 FiscalYearName
               ,<span style="color: #FF00FF;">min</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fdstart</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&#91;</span>FiscalStartOfYearDate<span style="color: #808080;">&#93;</span>
               ,<span style="color: #FF00FF;">max</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fdend</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&#91;</span>FiscalEndOfYearDate<span style="color: #808080;">&#93;</span>
           <span style="color: #0000FF;">FROM</span> glrule GLR
         <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> GLR.<span style="color: #202020;">fcname</span><span style="color: #808080;">&#41;</span> M2MYEARS
       <span style="color: #0000FF;">ON</span> TST.<span style="color: #202020;">FiscalYearName</span> <span style="color: #808080;">=</span> M2MYEARS.<span style="color: #202020;">FiscalYearName</span></pre></div></div>

<p>In the next step, I calculate the FiscalDayofYear and FiscalYearDayOfYear field. While I could have included this code in the previous step, it&#8217;s much easier to read and understand as a separate step. The FiscalDayOfYear calculation is simple and is simply the number of days between the current record and the first day Fiscal Year calculated previously. The FiscalYearDayOfYear is the same calculation, but I convert the integer (day) to a decimal and add it to the Fiscal Year integer. This makes drill down and grouping easier.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> TST.<span style="color: #808080;">&#91;</span>FiscalDayOfYear<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEDIFF</span> <span style="color: #808080;">&#40;</span>dd, <span style="color: #808080;">&#91;</span>FiscalStartOfYearDate<span style="color: #808080;">&#93;</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalYearDayOfYear<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">datepart</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">year</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
         <span style="color: #808080;">+</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>
            <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">datediff</span> <span style="color: #808080;">&#40;</span>dd, <span style="color: #FF00FF;">dateadd</span> <span style="color: #808080;">&#40;</span>yy, <span style="color: #FF00FF;">datediff</span> <span style="color: #808080;">&#40;</span>yy, <span style="color: #000;">0</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
            <span style="color: #808080;">/</span> <span style="color: #000;">1000.00</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NUMERIC</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">3</span>, <span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
   <span style="color: #0000FF;">FROM</span>  DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST</pre></div></div>

<p>The next section looks complex because I have to determine whether the number of Fiscal Periods can be broken into Quarters and Semesters. If a company used 10 Fiscal Periods per year, the concept of Quarters doesn&#8217;t make sense, but Semesters do. To make the code easier to understand, I&#8217;ve numbered the sections. </p>
<p>Section 1 checks whether the number of periods per fiscal year can be divided into quarters and semesters and the number of periods in each. Section 2 includes the period start and end dates so that the values of quarter and semester can be related back to the date table. Values for quarter or semester will be set to 0 if they aren&#8217;t valid in a particular M2M install.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> <span style="color: #808080;">&#91;</span>FiscalQuarter<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #0000FF;">CASE</span> M2MYEARS.<span style="color: #202020;">Quarterific</span>
         <span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Yes'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">ceiling</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span> <span style="color: #808080;">&#40;</span>M2MYEARS.<span style="color: #202020;">PeriodNo</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DECIMAL</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> M2MYEARS.<span style="color: #202020;">QuarterSize</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
         <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span>
        <span style="color: #0000FF;">END</span>
      ,<span style="color: #808080;">&#91;</span>FiscalSemester<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #0000FF;">CASE</span> M2MYEARS.<span style="color: #202020;">Semesterific</span>
         <span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Yes'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">ceiling</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span> <span style="color: #808080;">&#40;</span>M2MYEARS.<span style="color: #202020;">PeriodNo</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DECIMAL</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> M2MYEARS.<span style="color: #202020;">SemesterSize</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
         <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span>
        <span style="color: #0000FF;">END</span> 
  <span style="color: #0000FF;">FROM</span>  DateTestStnd.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
       <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
 <span style="color: #008080;">-------------- 2. Include Start and End of Period Values. </span>
        <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> fcname FYName
               ,fdstart PeriodStart
               ,fdend PeriodEnd
               ,fnnumber PeriodNo
               ,GLRAGG.<span style="color: #202020;">QuarterSize</span>
               ,GLRAGG.<span style="color: #202020;">Quarterific</span>
               ,GLRAGG.<span style="color: #202020;">SemesterSize</span>
               ,GLRAGG.<span style="color: #202020;">Semesterific</span>
           <span style="color: #0000FF;">FROM</span>  dbo.<span style="color: #202020;">glrule</span> GLR
                <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
  <span style="color: #008080;">-------------- 1. Calculate whether Quarters and Semesters are Valid (and their values)</span>
                 <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> fcname AGGFYName
                        ,<span style="color: #FF00FF;">MAX</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> PeriodCount
                        , <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">Max</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> QuarterSize
                        ,<span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">Max</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">4</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Yes'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'No'</span> <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> Quarterific
                        , <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">Max</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> SemesterSize
                        ,<span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">Max</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">2</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Yes'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'No'</span> <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> Semesterific
                    <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">glrule</span>
                  <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> fcname<span style="color: #808080;">&#41;</span> GLRAGG
                <span style="color: #0000FF;">ON</span> GLR.<span style="color: #202020;">FCNAME</span> <span style="color: #808080;">=</span> GLRAGG.<span style="color: #202020;">AGGFYNAME</span><span style="color: #808080;">&#41;</span> M2MYEARS
 <span style="color: #008080;">-------------- End of 1</span>
       <span style="color: #0000FF;">ON</span> TST.<span style="color: #0000FF;">DATE</span> <span style="color: #808080;">&gt;=</span> M2MYEARS.<span style="color: #202020;">PeriodStart</span> <span style="color: #808080;">AND</span> TST.<span style="color: #0000FF;">DATE</span> <span style="color: #808080;">&lt;=</span> M2MYEARS.<span style="color: #202020;">PeriodEnd</span>
  <span style="color: #008080;">-------------- End of 2</span></pre></div></div>

<p>Workday calculation is also somewhat difficult to understand. In M2M this information is kept in the Production Calendar (SCCALN) table. The fields we care about are: </p>
<ul>
<li>fcYrMon &#8211; Year and month in the format of YYYY/MM (2011/02). Unfortunately these are not per Fiscal Year or Period, they are by Calendar Year and Month. </li>
<li>fcShifts &#8211; A Varchar (31) field with the number of Shifts per day. </li>
</ul>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/04/Workdays.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/04/Workdays.png" alt="" title="Workdays" width="254" height="316" class="aligncenter size-full wp-image-5942" /></a><br />
Needless to say, we need an easier way than parsing through this table every time we need to calculate work days. The join is a little strange because the Date Table CalendarYear and CalendarMonth fields are SmallInt and need to be cast as Varchar. I&#8217;ve used a left join because many M2M companies don&#8217;t use the Production Calendar so SCCALN may lack records for fiscal periods. </p>
<p>Basically the code checks for a corresponding record in SCCALN. If one does not exist, then the typical Monday through Friday work week is used by default. If it does exist the fcShifts field is parsed, and if there are one or more shifts for a particular day, then the date is designated a work day.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> WorkDay <span style="color: #808080;">=</span>
        <span style="color: #0000FF;">CASE</span>
         <span style="color: #0000FF;">WHEN</span> wc.<span style="color: #202020;">fcShifts</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span>
           <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> TST.<span style="color: #202020;">CalendarDayOfWeek</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'No Work'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'Work Day'</span> <span style="color: #0000FF;">END</span>
         <span style="color: #0000FF;">ELSE</span>
           <span style="color: #0000FF;">CASE</span>
            <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>WC.<span style="color: #202020;">fcShifts</span>, TST.<span style="color: #202020;">CalendarDayOfMonth</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'0'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'No Work'</span>
            <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'Work Day'</span>
           <span style="color: #0000FF;">END</span>
        <span style="color: #0000FF;">END</span>
      ,IsWorkDay <span style="color: #808080;">=</span>
        <span style="color: #0000FF;">CASE</span>
         <span style="color: #0000FF;">WHEN</span> WC.<span style="color: #202020;">fcShifts</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span> 
           <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> TST.<span style="color: #202020;">CalendarDayOfWeek</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">END</span>
         <span style="color: #0000FF;">ELSE</span> 
           <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>WC.<span style="color: #202020;">fcShifts</span>, TST.<span style="color: #202020;">CalendarDayOfMonth</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'0'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> 
           <span style="color: #0000FF;">END</span>
        <span style="color: #0000FF;">END</span>
  <span style="color: #0000FF;">FROM</span>  DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
       <span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span>
        dbo.<span style="color: #202020;">ScCaln</span> WC
       <span style="color: #0000FF;">ON</span> WC.<span style="color: #202020;">fcYrMon</span> <span style="color: #808080;">=</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">CalendarYear</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Varchar</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
           <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span>
           <span style="color: #808080;">+</span> <span style="color: #0000FF;">right</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">CalendarMonth</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Varchar</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span></pre></div></div>

<p>The final bit of code updates the FiscalWeekName and FiscalYearWeekName. This code is <strong>tailored to my current employer, and I consider it optional</strong> because it may not pertain to you. The logic is as follows: </p>
<ol>
<li>The first fiscal week starts on the first day of the fiscal period.</li>
<li>Fiscal weeks end on Friday and begin on Saturday.</li>
<li>A &#8220;partial week&#8221; where the week starts on a day other than Saturday is added to the next full week. Partial weeks ending a period are considered their own week.</li>
<li>Week names follow the pattern of [FiscalPeriod].[WeekNumber] both of which are integers. For example, the second week of the fifth period would be designated as &#8220;5.2&#8243;. </li>
</ol>
<p>Again, this is a specific requirement and it may not apply to most companies.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> FiscalWeekName <span style="color: #808080;">=</span>
        TST.<span style="color: #202020;">FiscalPeriod</span>
        <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> <span style="color: #808080;">&#40;</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">count</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span>
                    <span style="color: #0000FF;">FROM</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TMP
                   <span style="color: #0000FF;">WHERE</span> TMP.<span style="color: #202020;">CalendarDayOfWeek</span> <span style="color: #808080;">=</span> <span style="color: #000;">7</span>
                         <span style="color: #808080;">AND</span> TMP.<span style="color: #0000FF;">Date</span> <span style="color: #808080;">BETWEEN</span> TST.<span style="color: #202020;">FiscalStartOfPeriodDate</span> <span style="color: #808080;">AND</span> TST.<span style="color: #0000FF;">Date</span><span style="color: #808080;">&#41;</span>
                <span style="color: #808080;">/</span> <span style="color: #000;">10.0</span><span style="color: #808080;">&#41;</span>
           <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span>
            .1
           <span style="color: #0000FF;">ELSE</span>
            <span style="color: #808080;">&#40;</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">count</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span>
                 <span style="color: #0000FF;">FROM</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TMP
                <span style="color: #0000FF;">WHERE</span> TMP.<span style="color: #202020;">CalendarDayOfWeek</span> <span style="color: #808080;">=</span> <span style="color: #000;">7</span>
                      <span style="color: #808080;">AND</span> TMP.<span style="color: #0000FF;">Date</span> <span style="color: #808080;">BETWEEN</span> TST.<span style="color: #202020;">FiscalStartOfPeriodDate</span> <span style="color: #808080;">AND</span> TST.<span style="color: #0000FF;">Date</span><span style="color: #808080;">&#41;</span>
             <span style="color: #808080;">/</span> <span style="color: #000;">10.0</span><span style="color: #808080;">&#41;</span>
          <span style="color: #0000FF;">END</span>
  <span style="color: #0000FF;">FROM</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
<span style="color: #008080;">-- Updates FiscalYearWeekName from FiscalWeekName</span>
<span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> TST.<span style="color: #202020;">FiscalYearWeekName</span> <span style="color: #808080;">=</span>
          <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">FiscalYearName</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">CHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span>
        <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #FF00FF;">len</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">FiscalWeekName</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">3</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'0'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">+</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">FiscalWeekName</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">CHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">FROM</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST</pre></div></div>

<p>In most systems, new fiscal years and periods are added once a year. The update script will need to be run after that happens. With Made2Manage, and presumably other systems, triggers could be used to detect those changes and then run the update script. In my own situation, I&#8217;ve chosen to save this update script as a scheduled job which runs weekly. This may seem wasteful, but the update script completes very quickly. </p>
<p>Technically, I could have integrated this script with the <a href="http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/">one I posted previously</a> which would have been faster and more efficient. However, I use two scripts for several reasons. First, this method is more flexible as I can create any Fiscal Update script I want without editing the main calendar script. Second, the Fiscal Update script will need to be run periodically to get updates and dropping/recreating the entire date table would be wasteful. Finally, most systems do not track Fiscal Years and Periods a decade or more into the future. Therefore, the first script is required to set default values for the Fiscal fields and only those that exist in the source system are updated. The user will not receive an error on a report because a future fiscal date value was not supplied.  </p>
<p>Well, there you have it. The last two articles have described how I create date tables and customize the Fiscal fields from a source system, in this case Made2Manage. </p>
<p>Any questions or suggestions to make the process better? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/04/using-a-date-table-to-track-custom-fiscal-yearsperiods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Calendar Tables and Why You Need One.</title>
		<link>http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/</link>
		<comments>http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 15:00:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Data Warehouse]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=5870</guid>
		<description><![CDATA[
			
				
			
		
What is a Date (Calendar) Table?
<p>For the purposes of this article a Date Table is a dedicated table containing a single record for each day in a defined range. They include fields with descriptive attributes for each day such as Year, Month, Week, and whether a particular day is a work day or not. Date [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F04%2Fcalendar-tables-why-you-need-one%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F04%2Fcalendar-tables-why-you-need-one%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h3>What is a Date (Calendar) Table?</h3>
<p>For the purposes of this article a Date Table is a dedicated table containing a single record for each day in a defined range. They include fields with descriptive attributes for each day such as Year, Month, Week, and whether a particular day is a work day or not. Date Tables (Dimensions) are an integral part of every Data Warehouse, but they can be used with a regular (OLTP) database as well. </p>
<h3>Why do I need a Date Table?</h3>
<p>There are several situations in which a date table is extremely helpful. Let&#8217;s consider just a few. Suppose you need to&#8230;. </p>
<ul>
<li>Generate a running sales total per day and include days in which you have no sales such as weekends. </li>
<li>Calculate the number of workdays between two dates. </li>
<li>Calculate projected ship dates for products based upon lead times. </li>
<li>Aggregate data by non-standard Fiscal Years or Periods. Perhaps your company uses the Fiscal Year of the US Government which runs from October 1st until September 30th. My current employer uses <a href="http://en.wikipedia.org/wiki/4-4-5_Calendar">the 4-4-5 Calendar</a> methodology, which is what originally led me to create this Date Table script. </li>
<li>Need to track data by seasons, whatever that means to your company. For example, if you manufactured bicycles you&#8217;d certainly want to compare sales figures in the spring versus fall.</li>
<li>Need to compare data using rolling months/periods. Suppose your company wants to know how March sales for this year compared to March for the past 5 years. Or you want to compare sales which occurred on Fridays to each other to look for trends. </li>
<li>Need to create <a href="http://en.wikipedia.org/wiki/Gantt_chart">Gantt Charts</a>.</li>
</ul>
<p>While many of these issues can be handled by writing T-SQL code, they&#8217;re much easier with a dedicated date table. Also, the Date Table can be customized to track anything about a particular day you want. </p>
<h3>What about Space Considerations?</h3>
<p>One concern that people have when creating a Date Table is the &#8220;wasted&#8221; space since each day is its own record. The space used is negligible, especially when compared to the benefits of having a dedicated date table. For example, when I execute my script for the years 2000 through 20035 the space used is less than 250MB. </p>
<h3>How do I create a Date Table?</h3>
<p><strong>Kimball Date Dimensions</strong><br />
In the <a href="http://www.amazon.com/gp/product/0470640383?ie=UTF8&#038;tag=made2-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0470640383">The Microsoft Data Warehouse Toolkit: With SQL Server 2008 R2</a><img src="http://www.assoc-amazon.com/e/ir?t=made2-20&#038;l=as2&#038;o=1&#038;a=0470640383" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />and the previous 2005 version, the Kimball Group recommends that you create a Date Dimension using Excel. In the latest book, they provide a Date Dimension Spreadsheet which is <a href="http://www.kimballgroup.com/html/booksMDWTtools.html">available for Download</a> and utilizes VBA to generate the table. They recommend you create a SSIS package to load that data into SQL Server. </p>
<p>While this will work, and some non-technical people might prefer this method, I prefer to use SQL Script, because I may not always have access to SSIS.</p>
<p><strong>Using a SQL Script</strong><br />
When I searched for a Date Table Script a few years ago, I couldn&#8217;t find something suitable for my needs. Most of them include only one set of fields (regular Calendar) and I to report via custom fiscal calendars. </p>
<p>I created this script from a function called <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519">F_TABLE_DATE by Michael Valentine Jones</a> from <a href="http://www.sqlteam.com/">SQLTeam</a> and have since gotten his permission to distribute it. I&#8217;ve modified the script so that it generates a Kimball style date dimension and it&#8217;s very comprehensive, yet simple to use and customize. </p>
<ol>
<li>Create a database called DateDatabase or whatever you&#8217;d like to call it. We&#8217;ll install F_TABLE_DATE in it and presumably the Date Table as well. </li>
<li><a href='http://www.made2mentor.com/wp-content/uploads/2011/04/Master-Date-Dimension-Script-Base-Ver-5.1-.txt'>Open the following script</a> and replace &#8220;DateDatabase&#8221; (Find and Replace) with whatever database name you chose in step 1. </li>
<li>Run the Script to create the function. </li>
</ol>
<p>Your database should now contain the function used to create Date Tables. As a test, run the following script:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">F_TABLE_DATE</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'20000101'</span>, <span style="color: #FF0000;">'20101231'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #000;">1</span></pre></div></div>

<p>You should receive 4019 Rows, 11 years worth of days plus a record for unknown dates. Notice the extensive list of fields built into the function. All of them are documented in the script. </p>
<p>Use a Select Into statement to create the Date Table like so. Replace dbo.DimDate with whatever you&#8217;d like to call your Date Table.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">IF</span> <span style="color: #808080;">EXISTS</span>
     <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>
        <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">IN</span>F<span style="color: #808080;">OR</span>MATION_SCHEMA.<span style="color: #202020;">TABLES</span>
       <span style="color: #0000FF;">WHERE</span> TABLE_TYPE <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BASE TABLE'</span> <span style="color: #808080;">AND</span> TABLE_NAME <span style="color: #808080;">=</span> <span style="color: #FF0000;">'DimDate'</span><span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">BEGIN</span>
        <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">DimDate</span>;
  <span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">Select</span>
  DateId
 ,<span style="color: #0000FF;">Date</span>
 ,NextDayDate
 ,CalendarYear
 ,CalendarYearQuarter
 ,CalendarYearMonth
 ,CalendarYearDayOfYear
 ,CalendarQuarter
 ,CalendarMonth
 ,CalendarDayOfYear
 ,CalendarDayOfMonth
 ,CalendarDayOfWeek
 ,CalendarYearName
 ,CalendarYearQuarterName
 ,CalendarYearMonthName
 ,CalendarYearMonthNameLong
 ,CalendarQuarterName
 ,CalendarMonthName
 ,CalendarMonthNameLong
 ,WeekdayName
 ,WeekdayNameLong
 ,CalendarStartOfYearDate
 ,CalendarEndOfYearDate
 ,CalendarStartOfQuarterDate
 ,CalendarEndOfQuarterDate
 ,CalendarStartOfMonthDate
 ,CalendarEndOfMonthDate
 ,QuarterSeqNo
 ,MonthSeqNo
 ,FiscalYearName
 ,FiscalYearPeriod
 ,FiscalYearDayOfYear
 ,FiscalYearWeekName
 ,FiscalSemester
 ,FiscalQuarter
 ,FiscalPeriod
 ,FiscalDayOfYear
 ,FiscalDayOfPeriod
 ,FiscalWeekName
 ,FiscalStartOfYearDate
 ,FiscalEndOfYearDate
 ,FiscalStartOfPeriodDate
 ,FiscalEndOfPeriodDate
 ,ISODate
 ,ISOYearWeekNo
 ,ISOWeekNo
 ,ISODayOfWeek
 ,ISOYearWeekName
 ,ISOYearWeekDayOfWeekName
 ,DateFormatYYYYMMDD
 ,DateFormatYYYYMD
 ,DateFormatMMDDYEAR
 ,DateFormatMDYEAR
 ,DateFormatMMMDYYYY
 ,DateFormatMMMMMMMMMDYYYY
 ,DateFormatMMDDYY
 ,DateFormatMDYY
 ,WorkDay
 ,IsWorkDay
<span style="color: #0000FF;">into</span>
  dbo.<span style="color: #202020;">DimDate</span> <span style="color: #008080;">-- Choose whatever name you like for your table.</span>
<span style="color: #0000FF;">From</span>
  dbo.<span style="color: #202020;">F_TABLE_DATE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'20000101'</span>,<span style="color: #FF0000;">'20251231'</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- Edit the parameters for a custom date range.</span>
  <span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> <span style="color: #000;">1</span></pre></div></div>

<p>Don&#8217;t panic, you don&#8217;t need to use all of the fields which is why this script is so highly customizable. Any fields you don&#8217;t want can be commented out before running the Select Into statement, which allows you to create your own custom Date Table. </p>
<p>The final step is to add a Primary Key to the DateID and an index for the Date and NextDayDate fields. As I can&#8217;t predict the usage patterns for your date table, you&#8217;ll have to implement further indexing if necessary.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>DimDate<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">ADD</span>  <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>DateId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">FILLFACTOR</span> <span style="color: #808080;">=</span> <span style="color: #000;">100</span>
 <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span>
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">INDEX</span> id_Dates
<span style="color: #0000FF;">ON</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> 
	<span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Date</span>, 
	 NextDayDate<span style="color: #808080;">&#41;</span></pre></div></div>

<h3>How do I use the Date Table? </h3>
<p>Using the date table is simple, you simply link to it from the query from your source system. Use the following template.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
  MY.<span style="color: #0000FF;">date</span>
 ,DTE.<span style="color: #202020;">CalendarYear</span>
 ,DTE.<span style="color: #202020;">CalendarMonthName</span>
<span style="color: #0000FF;">FROM</span>
    MyTable MY
  <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
    DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> DTE
  <span style="color: #0000FF;">ON</span> MY.<span style="color: #0000FF;">date</span> <span style="color: #808080;">&gt;=</span> DTE.<span style="color: #0000FF;">date</span> <span style="color: #808080;">AND</span>
     MY.<span style="color: #0000FF;">date</span> <span style="color: #808080;">&lt;</span> DTE.<span style="color: #202020;">NextDayDate</span></pre></div></div>

<p>Both DTE.Date and DTE.NextDayDate represent midnight of the two consecutive days. Therefore the time portion of a Datetime field can be ignored. </p>
<h3> What about the fiscal fields?</h3>
<p>After running these scripts, and examining the Date Table, you&#8217;ll notice that the fiscal fields mostly mirror the calendar fields. This is by design. <a href="http://www.made2mentor.com/2011/04/using-a-date-table-to-track-custom-fiscal-yearsperiods/">In the next article</a> I&#8217;ll explain how to customize the fiscal fields and a script to extract the fiscal information out of Made2Manage. Even if you don&#8217;t use M2M, you can still use the fiscal script as an example of how to do it. </p>
<p>Any questions? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>I Don’t Know! Deal With It! (Nulls)</title>
		<link>http://www.made2mentor.com/2011/03/i-don%e2%80%99t-know-deal-with-it-nulls/</link>
		<comments>http://www.made2mentor.com/2011/03/i-don%e2%80%99t-know-deal-with-it-nulls/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 15:59:40 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSRS]]></category>
		<category><![CDATA[Reporting]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=5398</guid>
		<description><![CDATA[
			
				
			
		
<p>This topic has been done to death in many places, but I still get frequent questions about Null field issues in T-SQL. My purpose in this article is not to debate the definition of Null. Smarter men than I have already done so. </p>
<p>SQL Server Books Online indicates &#8220;A value of NULL indicates that the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F03%2Fi-don%25e2%2580%2599t-know-deal-with-it-nulls%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F03%2Fi-don%25e2%2580%2599t-know-deal-with-it-nulls%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This topic has been done to death in many places, but I still get frequent questions about Null field issues in T-SQL. My purpose in this article is <strong>not to</strong> debate the definition of Null. Smarter men than I have already done so. </p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms191504.aspx">SQL Server Books Online</a> indicates &#8220;A value of NULL indicates that the value is unknown.&#8221; However, some disagree, most recently <a href="http://webbtechsolutions.com/2011/02/11/a-null-is-not-unknown/#respond">Joe Webb &#8211; Null is not Unknown</a>. Some argue that there should be <a href="http://sqlblog.com/blogs/hugo_kornelis/archive/2007/07/06/null-ndash-the-database-rsquo-s-black-hole.aspx">several different types of Null.</a></p>
<h3>Why Ask Why? </h3>
<p>As I said before, I don&#8217;t really care about the various interpretations of the Null value. I just know that they&#8217;re a pain to deal with. I have never received questions like this:</p>
<ul>
<li>What exactly is a Null? </li>
<li>What&#8217;s the definition of Null?</li>
</ul>
<p>The questions I get follow along these lines. </p>
<ul>
<li>Why am I losing records in this join? </li>
<li>Why is my total (or average) off in this query?</li>
<li>Why does my SSRS parameter list look funny? </li>
<li>Why won&#8217;t my parameters default like they should? </li>
</ul>
<p>So dear reader, as part of the <a href="http://www.made2mentor.com/t-sql-basics-series/">T-SQL Basics Series</a>, I am going to share what you need to know about dealing with Nulls rather than defining them. If you&#8217;d like to follow along, please download the <a href='http://www.made2mentor.com/wp-content/uploads/2011/03/Kung-Fu-Database-Script.zip'>Kung Fu Database Script</a>. </p>
<h3>Where Clause</h3>
<p>To start with, while people disagree about the definition of Null, everyone agrees what Null is not. It is not any of the following:</p>
<ul>
<li>0</li>
<li>blank</li>
<li>&#8221; (Empty String)</li>
<li>1/1/1900</li>
</ul>
<p>When most of us look at a Where clause, we only consider two options &#8211; True and False. However, SQL Server uses three value logic, True, False, and Unknown. Since Null represents an Unknown value any comparison to it results in Unknown, not True or False. Consider the following pseudo-code:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Select</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">from</span> Employees <span style="color: #0000FF;">where</span> Salary <span style="color: #808080;">&gt;</span> <span style="color: #000;">500</span></pre></div></div>

<p>If the Salary field doesn&#8217;t allow nulls then it&#8217;s very easy to return the list of Employees which earn more than $500 per some time period. However, if Salary is a Nullable field, then any Employee without a Salary specification will not be returned. How could it be? You simply do not know whether they make more than $500 or not. </p>
<p>I find the easiest way for me to learn these concepts is to look at examples. Let&#8217;s start with the following base query from the Kung Fu Database.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> OD.<span style="color: #202020;">OrderID</span>, OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>, OD.<span style="color: #202020;">ProductID</span>, OD.<span style="color: #202020;">ItemDescription</span>, OD.<span style="color: #202020;">OrderQty</span>, OD.<span style="color: #202020;">Discount</span>, OD.<span style="color: #202020;">ListPrice</span>
<span style="color: #0000FF;">FROM</span>   dbo.<span style="color: #202020;">OrderDetails</span> OD</pre></div></div>

<p><div id="attachment_5423" class="wp-caption aligncenter" style="width: 502px"><a href="http://www.made2mentor.com/wp-content/uploads/2011/03/null1.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/03/null1.png" alt="" title="null1" width="492" height="149" class="size-full wp-image-5423" /></a><p class="wp-caption-text">Null values are hightlighted. </p></div><br />
Returns: 6 Records. This is the total data set.</p>
<p>Now consider the following:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> OD.<span style="color: #202020;">OrderID</span>, OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>, OD.<span style="color: #202020;">ProductID</span>, OD.<span style="color: #202020;">ItemDescription</span>, OD.<span style="color: #202020;">OrderQty</span>, OD.<span style="color: #202020;">Discount</span>, OD.<span style="color: #202020;">ListPrice</span>
<span style="color: #0000FF;">FROM</span>   dbo.<span style="color: #202020;">OrderDetails</span> OD
&nbsp;
<span style="color: #0000FF;">WHERE</span>  OD.<span style="color: #202020;">OrderQty</span> <span style="color: #808080;">&gt;=</span> <span style="color: #000;">1</span></pre></div></div>

<p>Returns: 4 Records. Those with Null Values for OrderQty are ignored.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> OD.<span style="color: #202020;">OrderID</span>, OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>, OD.<span style="color: #202020;">ProductID</span>, OD.<span style="color: #202020;">ItemDescription</span>, OD.<span style="color: #202020;">OrderQty</span>, OD.<span style="color: #202020;">Discount</span>, OD.<span style="color: #202020;">ListPrice</span>
<span style="color: #0000FF;">FROM</span>   dbo.<span style="color: #202020;">OrderDetails</span> OD
&nbsp;
<span style="color: #0000FF;">WHERE</span>  OD.<span style="color: #202020;">OrderQty</span> <span style="color: #808080;">&lt;</span> <span style="color: #000;">1</span></pre></div></div>

<p>Returns: 0 Records. Comparing any value to Null results in Unknown and those those records are not returned.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> OD.<span style="color: #202020;">OrderID</span>, OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>, OD.<span style="color: #202020;">ProductID</span>, OD.<span style="color: #202020;">ItemDescription</span>, OD.<span style="color: #202020;">OrderQty</span>, OD.<span style="color: #202020;">Discount</span>, OD.<span style="color: #202020;">ListPrice</span>
<span style="color: #0000FF;">FROM</span>   dbo.<span style="color: #202020;">OrderDetails</span> OD
&nbsp;
<span style="color: #0000FF;">WHERE</span>  OD.<span style="color: #202020;">OrderQty</span> <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span></pre></div></div>

<p>Well, logically <strong>this should return 2 records, right</strong>? After all, you are asking for those records with Null values for OrderQty. However, the correct answer is <strong>0 records</strong>. The reason is simple.<strong> Comparing any value to Null (even another Null) returns Unknown</strong>. This may seem confusing, but logically it makes sense. Consider my Salary example above. If Bob and Dave both have Null Salary values, do they earn the same amount? Obviously, there&#8217;s no way to know. Therefore, the query evaluates to Unknown for those records and they aren&#8217;t returned. So, how can we return the Null records?</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> OD.<span style="color: #202020;">OrderID</span>, OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>, OD.<span style="color: #202020;">ProductID</span>, OD.<span style="color: #202020;">ItemDescription</span>, OD.<span style="color: #202020;">OrderQty</span>, OD.<span style="color: #202020;">Discount</span>, OD.<span style="color: #202020;">ListPrice</span>
<span style="color: #0000FF;">FROM</span>   dbo.<span style="color: #202020;">OrderDetails</span> OD
&nbsp;
<span style="color: #0000FF;">WHERE</span>  OD.<span style="color: #202020;">OrderQty</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span></pre></div></div>

<p>Returns: 2 Records. For the same reason, we could have used &#8220;IS NOT NULL&#8221; and 4 records would return. </p>
<p>So, what if we want to return any records with a quantity less than 1 and include Null Values? Well, you could write that query like this:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> OD.<span style="color: #202020;">OrderID</span>, OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>, OD.<span style="color: #202020;">ProductID</span>, OD.<span style="color: #202020;">ItemDescription</span>, OD.<span style="color: #202020;">OrderQty</span>, OD.<span style="color: #202020;">Discount</span>, OD.<span style="color: #202020;">ListPrice</span>
<span style="color: #0000FF;">FROM</span>   dbo.<span style="color: #202020;">OrderDetails</span> OD
&nbsp;
<span style="color: #0000FF;">WHERE</span>  OD.<span style="color: #202020;">OrderQty</span> <span style="color: #808080;">&lt;</span> <span style="color: #000;">1</span> 
<span style="color: #808080;">OR</span> OD.<span style="color: #202020;">OrderQty</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span></pre></div></div>

<p>Returns: 2 Records. </p>
<h3>Replacing Null Values with ISNULL or COALESCE</H3><br />
So that works, but it&#8217;s wordy. How can we more easily deal with nulls? Well, we can replace them with an actual value by using <a href="http://msdn.microsoft.com/en-us/library/ms190349.aspx">COALESCE</a> or <a href="http://msdn.microsoft.com/en-us/library/ms184325.aspx">ISNULL</a>. Each function returns the first Non-Null value found in the list of arguments. For example, SELECT ISNULL(NULL, 25) returns 25. However, SELECT ISNULL(NULL, NULL) returns Null. </p>
<p>Some people prefer IsNUll because the name of the function is more understable to them and it has been shown to have a very slight performance advantage. However, I use Coalesce because IsNull is limited to 2 arguments, whereas COALESE doesn&#8217;t have any realistic limit. SELECT COALESCE(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 7) still returns 7. Remember that it returns the first non-null value. Therefore, SELECT COALESCE(NULL, 3, NULL, NULL, NULL, 7) only returns the first value which is 3. </p>
<p>So, you may use Coalesce to simply these comparisons as well as for aesthetic reasons. Consider the previous Kung Fu Database query re-written with Coalesce. I&#8217;ve formatted it vertically to emphasize the difference.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
  OD.<span style="color: #202020;">OrderID</span>
 ,OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>
 ,OD.<span style="color: #202020;">ProductID</span>
 ,OD.<span style="color: #202020;">ItemDescription</span>
 ,<span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> OrderQty <span style="color: #008080;">-- COALESCE AND ALIASED</span>
 ,OD.<span style="color: #202020;">Discount</span>
 ,OD.<span style="color: #202020;">ListPrice</span>
<span style="color: #0000FF;">FROM</span>
  dbo.<span style="color: #202020;">OrderDetails</span> OD
<span style="color: #0000FF;">WHERE</span>
  <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&lt;</span> <span style="color: #000;">1</span></pre></div></div>

<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/03/null2.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/03/null2.png" alt="" title="null2" width="479" height="66" class="aligncenter size-full wp-image-5444" /></a><br />
Who determines whether or not you should replace the field like this? For the most part, the business user. So, if the business user provides the proper substitution values, the query may be written like this:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
  OD.<span style="color: #202020;">OrderID</span>
 ,OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>
 ,<span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">ProductID</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> ProductID
 ,<span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">ItemDescription</span>, <span style="color: #FF0000;">'None'</span><span style="color: #808080;">&#41;</span> ItemDescription
 ,<span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> OrderQty
 ,<span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">Discount</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> Discount
 ,<span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">ListPrice</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> ListPrice
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">OrderDetails</span> OD</pre></div></div>

<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/03/null3.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/03/null3.png" alt="" title="null3" width="477" height="148" class="aligncenter size-full wp-image-5449" /></a></p>
<h3>Joining on NULL-able Columns</h3>
<p>So, I&#8217;ve demonstrated how Null values can cause problems in the Where clause. For the same reasons, <strong>joining</strong> on Null-able columns is problematic as well. Consider my example Order Details table which allows a Null value for ProductID. Consider the following script:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
  OD.<span style="color: #202020;">OrderID</span>
 ,OD.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">LineNo</span><span style="color: #808080;">&#93;</span>
 ,OD.<span style="color: #202020;">ProductID</span>
 ,P.<span style="color: #202020;">ProductID</span>
 ,P.<span style="color: #202020;">ProdName</span>
 ,OD.<span style="color: #202020;">ItemDescription</span>
 ,OD.<span style="color: #202020;">OrderQty</span>
 ,OD.<span style="color: #202020;">Discount</span>
 ,OD.<span style="color: #202020;">ListPrice</span>
<span style="color: #0000FF;">FROM</span>
    dbo.<span style="color: #202020;">OrderDetails</span> OD
  <span style="color: #0000FF;">FULL</span> <span style="color: #808080;">OUTER</span> <span style="color: #808080;">JOIN</span>
    dbo.<span style="color: #202020;">Products</span> P
  <span style="color: #0000FF;">ON</span> P.<span style="color: #202020;">ProductID</span> <span style="color: #808080;">=</span> OD.<span style="color: #202020;">ProductID</span></pre></div></div>

<p>If that were written as your standard inner join, the recordset would lose the indicated record.<br />
<a href="http://www.made2mentor.com/wp-content/uploads/2011/03/null7.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/03/null7.png" alt="" title="null7" width="660" height="149" class="aligncenter size-full wp-image-5483" /></a></p>
<h3>Grouping, Sorting, and Aggregates</h3>
<p>In regards to Group By and Order By, Null values are considered equal. Meaning that all of the Null values are treated as one entity and are grouped and ordered as such.</p>
<p>Consider the following queries and result sets:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> OD.<span style="color: #202020;">OrderQty</span>
<span style="color: #0000FF;">FROM</span>   dbo.<span style="color: #202020;">OrderDetails</span> OD
&nbsp;
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> OD.<span style="color: #202020;">OrderQty</span></pre></div></div>

<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/03/NULL4.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/03/NULL4.png" alt="" title="NULL4" width="76" height="149" class="aligncenter size-full wp-image-5452" /></a></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>   OD.<span style="color: #202020;">OrderQty</span>
<span style="color: #0000FF;">FROM</span>     dbo.<span style="color: #202020;">OrderDetails</span> OD
&nbsp;
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> OD.<span style="color: #202020;">OrderQty</span></pre></div></div>

<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/03/null5.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/03/null5.png" alt="" title="null5" width="69" height="85" class="aligncenter size-full wp-image-5453" /></a></p>
<p><strong>Aggregate Functions</strong><br />
Aggregate functions such as COUNT, SUM, and AVG ignore Null values. However, one can use COUNT(*) which will count all records. Consider the following query:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
  <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span> QtySum
  ,<span style="color: #FF00FF;">Avg</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">cast</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">decimal</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> NullAvg
  ,<span style="color: #FF00FF;">Avg</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">cast</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">decimal</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> ZeroAvg 
 ,<span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span> QtyCount
 ,<span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> TotalCount
<span style="color: #0000FF;">FROM</span>
  dbo.<span style="color: #202020;">OrderDetails</span> OD</pre></div></div>

<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/03/null62.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/03/null62.png" alt="" title="null6" width="315" height="49" class="aligncenter size-full wp-image-5462" /></a><br />
I had to Cast the quantities as decimals, because otherwise the values would have been rounded to 1. If you do the math, you&#8217;ll see that aggregate functions only considered the four records with non-null values. This is particularly noticeable when you compare the NullAvg and the ZeroAvg. The ZeroAvg actually divides the total by 7 instead of 4. </p>
<p>In this article, I&#8217;ve attempted through the use of many examples to describe the issues when working with Null values and how to deal with them. In the next article, I&#8217;ll discuss how to deal with blank values in Joins, Where clauses, and when creating parameter lists for SQL Server Reporting Services. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/03/i-don%e2%80%99t-know-deal-with-it-nulls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Database Views &#8211; Using PowerPivot with Problem Databases Part 2</title>
		<link>http://www.made2mentor.com/2010/12/database-views-using-powerpivot-with-problem-databases-part-2/</link>
		<comments>http://www.made2mentor.com/2010/12/database-views-using-powerpivot-with-problem-databases-part-2/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 15:19:20 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[PowerPivot]]></category>
		<category><![CDATA[Reporting]]></category>
		<category><![CDATA[SSRS]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=4069</guid>
		<description><![CDATA[
			
				
			
		
<p>In the previous post I mentioned the use of views to simplify querying your database in general and PowerPivot in particular. </p>
What are Database Views?
<p>Views are virtual tables, which you can create with a SQL statement. Some of the advantages of using views are: </p>

Simplified Data Access &#8211; The view hides the data structure from [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2010%2F12%2Fdatabase-views-using-powerpivot-with-problem-databases-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2010%2F12%2Fdatabase-views-using-powerpivot-with-problem-databases-part-2%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In the <a href="http://www.made2mentor.com/2010/10/using-powerpivot-with-problem-databases-like-m2m-part-1">previous post</a> I mentioned the use of views to simplify querying your database in general and PowerPivot in particular. </p>
<h3>What are Database Views?</h3>
<p>Views are virtual tables, which you can create with a SQL statement. Some of the advantages of using views are: </p>
<ol>
<li>Simplified Data Access &#8211; The view hides the data structure from the user making reporting less complex. Also, calculations can be performed in the view for them making their lives easier as well.</li>
<li>Security &#8211; Through the use of views you can easily limit your users to certain types of data in your database. You can restrict them from looking at specific tables, or even certain fields or rows in a database. </li>
<li>Standardization/Maintainability &#8211; The table logic for views is stored in the view itself. Therefore, if a change in that logic is required or the database structure changes, which happens occasionally when you upgrade M2M, the view need only be updated once. Otherwise every report, spreadsheet, etc. based on the changed areas must be edited. </li>
</ol>
<h3>View Security</h3>
<p>As I&#8217;ve mentioned in the past, you should use a report login for your reporting with read only access. The simplest way to do so is to add a SQL Login and assign it the db_datareader role for that database. There are clear directions with screenshots <a href="http://articles.techrepublic.com.com/5100-10878_11-1061781.html">covering this here</a>. I create report logins which I creatively call ReportsXX, where XX represents which Made2Manage database that user will primarily report from. I use separate accounts per M2M company so different companies cannot see others data. </p>
<p>When creating these views, you should always create them with the same owner or in the same schema. <a href="http://www.sqlteam.com/article/understanding-the-difference-between-owners-and-schemas-in-sql-server">In SQL 2000</a> objects were owned by a database user. When you look at the M2M database you&#8217;ll notice that every object is owned by dbo (database owner). Objects created in SQL 2000 tend to default to the owner who created them and if an account is designated as a database owner the default is dbo. In my case, all objects owned by Reports01 would be Reports01.ObjectName and would need to be referred to with their fully qualified name by any user other than Reports01. </p>
<p>The concept of database schemas was introduced in SQL 2005. Schemas serve as &#8220;containers&#8221; for objects and are useful for enforcing security as well as organization in the database. In this case you would create a schema called Reports01 (or Reports or whatever you like), and since your report login has the db_datareader role, it can also read from your views. </p>
<h3>How do I Create a View? </h3>
<p>As always, please read my <a href="http://www.made2mentor.com/2010/03/standard-disclaimer/">standard disclaimer</a> before implementing any code from my site. Creating a view is very simple. Let&#8217;s take the following SQL statement.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> ARM.<span style="color: #202020;">fcinvoice</span> <span style="color: #0000FF;">AS</span> InvoiceNo,
       ARM.<span style="color: #202020;">fcustno</span> <span style="color: #0000FF;">AS</span> CustNo,
       ARM.<span style="color: #202020;">fbcompany</span> <span style="color: #0000FF;">AS</span> CompanyName,
       ARM.<span style="color: #202020;">fdgldate</span> <span style="color: #0000FF;">AS</span> PostDate,
       ARM.<span style="color: #202020;">fsalespn</span> <span style="color: #0000FF;">AS</span> SalesPerson,
       <span style="color: #FF00FF;">Rtrim</span> <span style="color: #808080;">&#40;</span>ARI.<span style="color: #202020;">fpartno</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> PartNo,
       ARI.<span style="color: #202020;">ftotprice</span> <span style="color: #0000FF;">AS</span> NetInvoiced,
       ARI.<span style="color: #202020;">fprodcl</span> <span style="color: #0000FF;">AS</span> ProdClass,
       <span style="color: #808080;">IN</span>M.<span style="color: #202020;">fgroup</span> <span style="color: #0000FF;">AS</span> GroupCode
  <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">armast</span> ARM
       <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">aritem</span> ARI
          <span style="color: #0000FF;">ON</span> ARM.<span style="color: #202020;">fcinvoice</span> <span style="color: #808080;">=</span> ARI.<span style="color: #202020;">fcinvoice</span>
       <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">inmast</span> <span style="color: #808080;">IN</span>M
          <span style="color: #0000FF;">ON</span> <span style="color: #808080;">IN</span>M.<span style="color: #202020;">fpartno</span> <span style="color: #808080;">=</span> ARI.<span style="color: #202020;">fpartno</span> <span style="color: #808080;">AND</span> <span style="color: #808080;">IN</span>M.<span style="color: #202020;">frev</span> <span style="color: #808080;">=</span> ARI.<span style="color: #202020;">frev</span>
 <span style="color: #0000FF;">WHERE</span> ARM.<span style="color: #202020;">fcstatus</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #FF0000;">'V'</span></pre></div></div>

<p>Please keep in mind that this is a simplified invoicing script for demonstration purposes. The templates I use are much more comprehensive and bring in many other related tables such as FastForms extension tables, the Sales Person table which contains their full name, portions of the Item Master table for product details, etc. </p>
<p>Further, it&#8217;s important to note that if you run that script on your database it will retrieve every applicable record since you started using M2M. With my current company that&#8217;s more than a decade of records. With input from the business leaders, we agreed to limit these views to the prior two fiscal years. While you could hard code these dates in with a statement like this, you shouldn&#8217;t do so.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"> <span style="color: #808080;">AND</span> ARM.<span style="color: #202020;">fdgldate</span> <span style="color: #808080;">&gt;=</span> <span style="color: #FF0000;">'1/1/2008'</span></pre></div></div>

<p>Instead, construct your view so that it automatically selects the previous two years.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"> <span style="color: #808080;">AND</span> ARM.<span style="color: #202020;">fdgldate</span> <span style="color: #808080;">&gt;=</span>  <span style="color: #FF00FF;">DATEADD</span><span style="color: #808080;">&#40;</span>yy, <span style="color: #FF00FF;">DATEDIFF</span><span style="color: #808080;">&#40;</span>yy,<span style="color: #000;">0</span>,<span style="color: #FF00FF;">getdate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">-</span><span style="color: #000;">2</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>  <span style="color: #008080;">-- In this case 1/1/2008</span></pre></div></div>

<p> This lessens the performance impact on your server. So, how do you create a view from this? You adapt the code as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> M2MDataXX <span style="color: #008080;">-- Replace XX with your M2M Company </span>
Go
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> ReportsXX.<span style="color: #202020;">vw_Invoices</span>
<span style="color: #0000FF;">AS</span>
   <span style="color: #0000FF;">SELECT</span> ARM.<span style="color: #202020;">fcinvoice</span> <span style="color: #0000FF;">AS</span> InvoiceNo,
          ARM.<span style="color: #202020;">fcustno</span> <span style="color: #0000FF;">AS</span> CustNo,
          ARM.<span style="color: #202020;">fbcompany</span> <span style="color: #0000FF;">AS</span> CompanyName,
          ARM.<span style="color: #202020;">fdgldate</span> <span style="color: #0000FF;">AS</span> PostDate,
          <span style="color: #0000FF;">YEAR</span> <span style="color: #808080;">&#40;</span>ARM.<span style="color: #202020;">fdgldate</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> PostYear,
          <span style="color: #0000FF;">MONTH</span> <span style="color: #808080;">&#40;</span>ARM.<span style="color: #202020;">fdgldate</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> PostMonth,
          <span style="color: #0000FF;">CONVERT</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span>, ARM.<span style="color: #202020;">fdgldate</span>, <span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> OrderMonthName,
          ARM.<span style="color: #202020;">fsalespn</span> <span style="color: #0000FF;">AS</span> SalesPerson,
          <span style="color: #FF00FF;">Rtrim</span> <span style="color: #808080;">&#40;</span>ARI.<span style="color: #202020;">fpartno</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> PartNo,
          ARI.<span style="color: #202020;">ftotprice</span> <span style="color: #0000FF;">AS</span> NetInvoiced,
          ARI.<span style="color: #202020;">fprodcl</span> <span style="color: #0000FF;">AS</span> ProdClass,
          <span style="color: #808080;">IN</span>M.<span style="color: #202020;">fgroup</span> <span style="color: #0000FF;">AS</span> GroupCode
     <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">armast</span> ARM
          <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">aritem</span> ARI
             <span style="color: #0000FF;">ON</span> ARM.<span style="color: #202020;">fcinvoice</span> <span style="color: #808080;">=</span> ARI.<span style="color: #202020;">fcinvoice</span>
          <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">inmast</span> <span style="color: #808080;">IN</span>M
             <span style="color: #0000FF;">ON</span> <span style="color: #808080;">IN</span>M.<span style="color: #202020;">fpartno</span> <span style="color: #808080;">=</span> ARI.<span style="color: #202020;">fpartno</span> <span style="color: #808080;">AND</span> <span style="color: #808080;">IN</span>M.<span style="color: #202020;">frev</span> <span style="color: #808080;">=</span> ARI.<span style="color: #202020;">frev</span>
    <span style="color: #0000FF;">WHERE</span> ARM.<span style="color: #202020;">fcstatus</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #FF0000;">'V'</span>
 <span style="color: #808080;">AND</span> ARM.<span style="color: #202020;">fdgldate</span> <span style="color: #808080;">&gt;=</span> <span style="color: #FF00FF;">DATEADD</span><span style="color: #808080;">&#40;</span>yy, <span style="color: #FF00FF;">DATEDIFF</span><span style="color: #808080;">&#40;</span>yy,<span style="color: #000;">0</span>,<span style="color: #FF00FF;">getdate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">-</span><span style="color: #000;">2</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> 
Go</pre></div></div>

<p>The view can be queried as if it were a SQL table, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">select</span> CompanyName, InvoiceNo, NetInvoiced, PartNo <span style="color: #0000FF;">FROM</span> ReportsXX.<span style="color: #202020;">vw_Invoices</span>
<span style="color: #0000FF;">where</span> NetInvoiced <span style="color: #808080;">&gt;</span> <span style="color: #000;">1000</span> <span style="color: #008080;">-- 1000 dollars</span></pre></div></div>

<p>When run against the educational database the data returns like so:<br />
<a href="http://www.made2mentor.com/wp-content/uploads/2010/10/view_results1.png"><img src="http://www.made2mentor.com/wp-content/uploads/2010/10/view_results1.png" alt="" title="view_results" width="415" height="126" class="aligncenter size-full wp-image-4086" /></a></p>
<p>You&#8217;ll notice that I&#8217;ve aliased the field names and replaced them with English. In programs such as SQL Reporting Services the alias names will be automatically formatted for you so NetInvoiced becomes Net Invoiced. Another advantage is that the view hides the table joins from the user as well and it looks like one object. </p>
<p>Now, keep in mind that what I&#8217;ve just shown you <strong>most likely violates your Consona User Agreement. </strong> You&#8217;re not really allowed to make changes to their database. The reason I say &#8220;most likely&#8221; is because I cannot get a definitive answer on this point from Consona. </p>
<h3>Why do I need to bother with all that owner/schema stuff?</h3>
<p>As I said before you can use them for security purposes and restrict user access only to the views you create. However, the most important reason is to make migration easier. When you migrate your M2M version, one of the first steps is a check of database objects and the installer may error out and quit if it finds objects that aren&#8217;t supposed to be there. Therefore, I document every view I create, run a script to drop all of the objects in the specific owner/schema, and migrate. After migration, I script all of my views back in, and begin testing them. </p>
<p>I wrote this simple script to drop all views belonging to a user in SQL 2000. Make sure to replace &#8220;ReportsXX&#8221; with the name of your report login and &#8220;M2MDataXX&#8221; with your database name.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">--  SQL 2000</span>
<span style="color: #0000FF;">USE</span> M2MDataXX
Go
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @UserName   <span style="color: #0000FF;">VARCHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">25</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">DECLARE</span> @ViewName   <span style="color: #0000FF;">NVARCHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">25</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">SQL</span>   <span style="color: #0000FF;">VARCHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SET</span> @UserName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'ReportsXX'</span>
<span style="color: #008080;">-------------------------- </span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> ViewName
  <span style="color: #0000FF;">INTO</span> #temp
  <span style="color: #0000FF;">FROM</span> sysobjects
 <span style="color: #0000FF;">WHERE</span> type <span style="color: #808080;">=</span> <span style="color: #FF0000;">'V'</span> <span style="color: #808080;">AND</span> <span style="color: #FF00FF;">user_name</span> <span style="color: #808080;">&#40;</span>uid<span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> @UserName
<span style="color: #008080;">--------------------------</span>
<span style="color: #0000FF;">DECLARE</span>
   DropViews <span style="color: #0000FF;">CURSOR</span> <span style="color: #0000FF;">FOR</span>
      <span style="color: #0000FF;">SELECT</span> ViewName <span style="color: #0000FF;">FROM</span> #temp
&nbsp;
&nbsp;
<span style="color: #0000FF;">OPEN</span> DropViews
&nbsp;
<span style="color: #0000FF;">FETCH</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">FROM</span> DropViews <span style="color: #0000FF;">INTO</span> @ViewName
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #FF00FF;">@@FETCH_STATUS</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
<span style="color: #0000FF;">BEGIN</span>
   <span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'Drop View '</span> <span style="color: #808080;">+</span> @UserName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span> <span style="color: #808080;">+</span> @ViewName
   <span style="color: #0000FF;">PRINT</span> @<span style="color: #0000FF;">sql</span>
   <span style="color: #008080;">--EXEC( @SQL)</span>
   <span style="color: #0000FF;">FETCH</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">FROM</span> DropViews <span style="color: #0000FF;">INTO</span> @ViewName
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">CLOSE</span> DropViews
<span style="color: #0000FF;">DEALLOCATE</span> DropViews
&nbsp;
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #temp</pre></div></div>

<p>For SQL 2005 and after, simply replace the code between the horizontal lines with:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> ViewName
  <span style="color: #0000FF;">INTO</span> #temp
  <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">objects</span>
 <span style="color: #0000FF;">WHERE</span> type <span style="color: #808080;">=</span> <span style="color: #FF0000;">'V'</span> <span style="color: #808080;">AND</span> schema_name <span style="color: #808080;">&#40;</span>schema_id<span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> @UserName</pre></div></div>

<h3>Stay Tuned</h3>
<p>In the near future, I will be posting more information about <a href="http://www.made2mentor.com/2010/10/announcing-project-m-data-analytics/">M-Data Analytics</a> which has an entirely separate database, easily understood field names, and will have a multitude of views built in, etc. That doesn&#8217;t help you now, but I assure you, it is coming. </p>
<p>In the meantime you can simplify your life by using Database Views. In the next article in this series I&#8217;ll put it all together and show you the steps you&#8217;ll need to follow to successfully use technology like PowerPivot with problem databases. </p>
<p>Also, make sure to come back Monday when I share my entry in the <a href="http://www.brentozar.com/archive/2010/12/twelve-days-of-sql-series/">Twelve Days of SQL Series</a>. Not only will I share my choice for one of the best blog posts (and bloggers) this year, but I will reveal a picture of the tackiest Christmas Ornament you have ever seen. And before I show it, let me assure you that I really do own it, it really is on my Christmas Tree, and you&#8217;ll understand why my girlfriend suggested I need psychiatric help simply for owning it. </p>
<p>More to come&#8230;. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2010/12/database-views-using-powerpivot-with-problem-databases-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Report Guidelines: Exporting Reports</title>
		<link>http://www.made2mentor.com/2009/10/report-guidelines-exporting-reports/</link>
		<comments>http://www.made2mentor.com/2009/10/report-guidelines-exporting-reports/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 14:00:46 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSRS]]></category>
		<category><![CDATA[Crystal Reports]]></category>
		<category><![CDATA[Reports]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=2162</guid>
		<description><![CDATA[
			
				
			
		
<p>This article is day four in a week of reporting articles.</p>
<p>Most users want to export reports to Excel so they can manipulate the data. If possible, set up the report for the cleanest exporting possible. This process starts when writing T-SQL. For example, one can extract only the date instead of a datetime value when [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Freport-guidelines-exporting-reports%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Freport-guidelines-exporting-reports%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This article is day four in a <a href="http://www.made2mentor.com/2009/10/basic-reporting-guidelines-week-of-articles/">week of reporting articles</a>.</p>
<p>Most users want to export reports to Excel so they can manipulate the data. If possible, set up the report for the cleanest exporting possible. This process starts when writing T-SQL. For example, one can extract only the date instead of a datetime value when time is not relevant.  </p>
<p>As I mentioned in a <a href="http://www.made2mentor.com/2009/08/exporting-memo-fields-to-excel-received-goods-rprego/"> previous post</a>, if you are creating a VFP report for M2M only the fields called out in your prg file will export to Excel. Therefore, do not pull them in via the data environment or your export will be missing items.  </p>
<p>SQL Server Reporting Services and Crystal Reports will export many formats including PDF, Excel, Word, HTML web pages, and several text based formats. Excel, in my experience is the most common export format followed by PDF. In Crystal, there are two basic options for exporting to Excel, standard and data only. Most often I use standard, but if default options are used the results will often be lacking. The problem is that Crystal will merge columns and rows in illogical ways and this makes data analysis impossible.  </p>
<p>The answer is to select a different column width option. Depending on your report, you’ll most often use a Group Header, Footer, or the Details section to determine your column widths.  </p>
<p><div id="attachment_2165" class="wp-caption alignleft" style="width: 545px"><img src="http://www.made2mentor.com/wp-content/uploads/2009/10/standard_excel.png" alt="Set the Export Properties.  " title="standard_excel" width="535" height="260" class="size-full wp-image-2165" /><p class="wp-caption-text">Set the Export Properties.  </p></div>Keep in mind that your users will not remember to change this setting when exporting, so there is an option to save these settings with your report. Choose File –<br />
Export – Report Export Options to change these options. These options are saved with the report and it will default to those settings for the users.  </p>
<p>I always create these settings even if the user says they will never export it. Invariably, someone will come to me in the future and complain that the export doesn’t work.  </p>
<p>As an aside, I have talked in the past about <a href="http://www.made2mentor.com/2009/09/pete-people-for-the-ethical-treatment-of-excel/">Excel Hell</a> and often that hell is caused by spreadsheet exports from M2M or other ERP systems. For example, some companies want to keep track of their Sales Order Backlog or Inventory over time, but these figures are a snapshot. Therefore, an employee is assigned the task of running the corresponding report, exporting it to Excel, and keeping track of it on the network. This behavior becomes so ingrained in some users that they have difficulty abandoning it.  </p>
<p>For example, I posted code to facilitate <a href="http://www.made2mentor.com/2009/04/cycle-counting-revisted/">Cycle Counting</a>. The code automatically exports cycle count information to a separate table to be kept forever. After I had demonstrated the capability for the user who requested the project, he actually said to me, “Great, so I can export this to excel and….”</p>
<p>Oh, how they love to export. Tomorrow I will discuss <a href="http://www.made2mentor.com/2009/10/report-guidelines-report-approval-and-deployment/">deployment</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2009/10/report-guidelines-exporting-reports/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Report Guidelines:  Know What Your User Wants and Your Target Number(s)</title>
		<link>http://www.made2mentor.com/2009/10/report-guidelines-know-what-your-user-wants-and-your-target-numbers/</link>
		<comments>http://www.made2mentor.com/2009/10/report-guidelines-know-what-your-user-wants-and-your-target-numbers/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 17:28:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSRS]]></category>
		<category><![CDATA[Crystal Reports]]></category>
		<category><![CDATA[Reports]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=2143</guid>
		<description><![CDATA[
			
				
			
		
<p>This article is day three in a week of reporting articles.</p>
<p>This seems like a no-brainer, but it is not as easy as you would think. Often your users will not know what they want and certainly not where to get it.  </p>
<p class="wp-caption-text">Don’t take direction from this guy. </p>
<p>Let’s say Milton, a purchasing agent, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Freport-guidelines-know-what-your-user-wants-and-your-target-numbers%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Freport-guidelines-know-what-your-user-wants-and-your-target-numbers%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This article is day three in a <a href="http://www.made2mentor.com/2009/10/basic-reporting-guidelines-week-of-articles/">week of reporting articles</a>.</p>
<p>This seems like a no-brainer, but it is not as easy as you would think. Often your users will not know what they want and certainly not where to get it.  </p>
<div id="attachment_2146" class="wp-caption aligncenter" style="width: 410px"><img src="http://www.made2mentor.com/wp-content/uploads/2009/10/milton_reports.jpg" alt="Don’t take direction from this guy. " title="milton_reports" width="400" height="313" class="size-full wp-image-2146" /><p class="wp-caption-text">Don’t take direction from this guy. </p></div>
<p>Let’s say Milton, a purchasing agent, requests a Vendor Performance Report. After you ask him to fill out your <a href="http://www.made2mentor.com/2009/10/reporting-guidelines-requesting-reports/">Report Request Form</a> (you are using one right?), you read through it and have a few questions. The first should be, “Who needs this data? Who is requesting the report?”<br />
<div id="attachment_2150" class="wp-caption aligncenter" style="width: 453px"><img src="http://www.made2mentor.com/wp-content/uploads/2009/10/bill_reports.jpg" alt="This is the guy to ask. (Unfortunately)" title="bill_reports" width="443" height="240" class="size-full wp-image-2150" /><p class="wp-caption-text">This is the guy to ask. (Unfortunately)</p></div></p>
<p>He indicates in a halting, barely audible tone, “Umm… Bill… the Production Manager… He needs it to rate our vendors. He says if I don’t get it… he’ll take my stapler.” If possible, you should go ask your questions of Bill. Bill knows the answers because he is the person requesting the data. However, I often find that Bill has delegated the task to Milton because Bill is swamped with work himself and can save some time. If you are a Bill, listen to me, no you won’t. We will both waste time because Milton will either ask you all of the questions I ask him, or will likely answer some of them incorrectly. Therefore, Bill, when you check the report and find it incorrect, you and I will be meeting anyway. After telling Milton that you will talk to Bill, he mumbles, “…but I could set your desk on fire…”  </p>
<p>With some personalities, a different problem arises. Suppose I am speaking with Bill in this case (the responsible party) and ask something like, “Why do you need this report?”  </p>
<p>I want to know the reason Bill wants it so I can lead him through the process and recommend options along the way. This speeds up the process and results in a better report. However, some people will misunderstand you and Bill may think you are questioning his <strong>right</strong> to the report. If Bill becomes defensive, the process is more difficult. Bill will often respond with something like, “Umm… yeah…. Well corporate says they want it… Maybe you could come in Saturday to work on it….”<br />
All joking aside, to avoid a misunderstanding I usually ask this in a different way such as, “Bill, so that I can create the best report as quickly as possible, may I ask the purpose of it? What are you really looking for?” Bill will often explain a little of his job to you and it’s a win-win situation.  </p>
<p>In our scenario, when you phrase the question properly, Bill opens up and tells you that he is thinking of dropping Vendor XYZ for poor performance. He wants a report to document that performance. At this point your question should be, “How? What constitutes poor performance?”  </p>
<p>Another reason it is difficult to determine what a user wants is they don’t know what you can actually do with the data. Many users have never heard of “drill down,” especially if the only reports they have used are M2M Visual FoxPro reports, as VFP does not support it. They may also not know that SQL Server Reporting Services and Crystal Reports have full charting and graphing abilities or that with conditional formatting you could show any problem vendors in red. Since the user does not know what you can do, they do not know what they can request.  </p>
<p>Also, if possible, know what your target number should be. Some users like to play a form of a digital Easter Egg hunt. They won’t tell you that they already know the answer; they want to see if you can come up with it on your own. I cannot tell you how many times I have created a report, only to find out that I didn’t match the user’s number and my number must therefore be “wrong.” A good example of this is in financial reporting. Most M2M financial reports pull from the general ledger rather than the accounts receivable tables for financial reporting. Trying to hit the same number, especially when you have no idea how they are calculating theirs, is practically impossible due to general ledger journal entries.  </p>
<p>Therefore, if you are trying to match a report in M2M (or any ERP system), derive the SQL directly from that report. I will follow up with articles and videos as to how I do this in the near future.</p>
<p>Finally, keep in mind that <strong>some users do not want your help</strong> in automating their reports. Management has ordered them to ask for your help, but they do not really want you to succeed. Often they perceive a loss of power or job security when portions of their job are automated. This is yet another reason why you should be talking to Bill, not Milton.  </p>
<p>Tomorrow I will discuss <a href="http://www.made2mentor.com/2009/10/report-guidelines-exporting-reports/">exporting issues</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2009/10/report-guidelines-know-what-your-user-wants-and-your-target-numbers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Reporting Guidelines: Standardize Your Layouts</title>
		<link>http://www.made2mentor.com/2009/10/reporting-guidelines-standardize-your-layouts/</link>
		<comments>http://www.made2mentor.com/2009/10/reporting-guidelines-standardize-your-layouts/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 15:00:27 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSRS]]></category>
		<category><![CDATA[Crystal Reports]]></category>
		<category><![CDATA[Reports]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=2131</guid>
		<description><![CDATA[
			
				
			
		
<p>This article is day two in a week of reporting articles.</p>
<p>Create standard layouts for both landscape and portrait reports and have them approved by management. Many larger companies have Corporate Identity Guidelines created by marketing experts with standards already defined for marketing and sales documents.  Adhere to the standards religiously.  If creating your [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Freporting-guidelines-standardize-your-layouts%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Freporting-guidelines-standardize-your-layouts%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This article is day two in a <a href="http://www.made2mentor.com/2009/10/basic-reporting-guidelines-week-of-articles/">week of reporting articles</a>.</p>
<p>Create standard layouts for both landscape and portrait reports and have them approved by management. Many larger companies have Corporate Identity Guidelines created by marketing experts with standards already defined for marketing and sales documents.  Adhere to the standards religiously.  If creating your own, you should adopt standard fonts, logos, colors, and style as well for all parts of the report.  This <strong>standardization makes reports easy for the users to scan</strong> and makes them look professional.  I&#8217;ll share some of the specific report guidelines that I use in Crystal Reports and SSRS in a later article.  As I <a href="http://www.made2mentor.com/2008/11/i-for-one-welcome-our-new-net-overlords-reporting/">mentioned previously</a>, I wish M2M had a way to customize the style of all of their reports in one place.    </p>
<p>Remember that you don’t have to have a degree in design to create a good standard layout, but almost all of your reports should conform to that standard.  Be sure to get your standard layout approved by management, and be prepared to change reports over time.  Executives seem to enjoy changing these standards, and companies sometimes change their logo and branding. Depending on which system you are using, you may be able to change much of that automatically but often not all of it.  </p>
<p>Since I’m discussing standardization, I’ll mention T-SQL queries here. Develop standard SQL templates for the basic departments in your database. For example, since incoming sales reports are popular, create a robust select statement, alias it properly, and use it as the base for all of your sales reports. Doing so will save you incalculable time down the road. As time goes on, I will post more and more of mine to this blog.  </p>
<p>Tomorrow I’ll discuss <a href="http://www.made2mentor.com/2009/10/report-guidelines-know-what-your-user-wants-and-your-target-numbers/">hitting your target</a>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2009/10/reporting-guidelines-standardize-your-layouts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reporting Guidelines: Requesting Reports</title>
		<link>http://www.made2mentor.com/2009/10/reporting-guidelines-requesting-reports/</link>
		<comments>http://www.made2mentor.com/2009/10/reporting-guidelines-requesting-reports/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 17:27:45 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SSRS]]></category>
		<category><![CDATA[Crystal Reports]]></category>
		<category><![CDATA[Reports]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=2119</guid>
		<description><![CDATA[
			
				
			
		
<p>This article is day one in a week of reporting articles.</p>
<p>If you are going to become a business intelligence ninja, start at the beginning.   You need to develop standard procedures for report creation.  They include:</p>

Who can request reports?  
Who approves their creation?  
Who assigns priority of your work?  
How [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Freporting-guidelines-requesting-reports%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Freporting-guidelines-requesting-reports%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This article is day one in a <a href="http://www.made2mentor.com/2009/10/basic-reporting-guidelines-week-of-articles/">week of reporting articles</a>.</p>
<p>If you are going to become a business intelligence ninja, start at the beginning.   You need to develop standard procedures for report creation.  They include:</p>
<ul>
<li>Who can request reports?  </li>
<li>Who approves their creation?  </li>
<li>Who assigns priority of your work?  </li>
<li>How are reports requested?  </li>
</ul>
<p>These questions are entirely determined by your corporate culture, however I would suggest writing up clearly defined procedures and sticking to them.  As to how my users request reports, I use a standardized form for doing so.  Mine is accessed from our intranet site.  I can’t provide the page itself as it is owned by my employer, but it includes the following questions: </p>
<blockquote><p>
Please specify:</p>
<p>Report Title<br />
Priority  &#8211; 1 to 5 with 5 being most urgent<br />
Please state purpose of report and give a brief description.<br />
Can you provide an example? If so, please specify.<br />
What file format does your data need to be? Printed Report, Excel, PDF, Web Page?<br />
Report Layout: Portrait or Layout<br />
Should the report have a chart or graph? Pie Chart, Bar Chart, Line Graph, etc?<br />
Should the report be scheduled to run automatically? If so, please specify.<br />
List the data fields, in the column order, for your report. Eg. Customer Name, Customer Number…<br />
Does this report need to be sorted or grouped? If so what is the criteria?<br />
What calculated fields do you wish to include in your report. Please be specific and include the formula for performing the calculation. Totals, subtotals?<br />
Do you have any other comments or special instructions for your report?
</p></blockquote>
<p>Feel free to steal these and modify them in any way that suits you. The list of questions may seem exhaustive, but if you really think about it, this is the bare minimum of information one needs to know to create a basic report.  Even with all of this information, all but the most trivial of reports will require further questions. </p>
<p>If at all possible, go directly to the source with those questions.  If the VP of Finance wants the report, but asks the Accounting Manager to task you, try to ask your questions of the VP. I’ll expound on this Wednesday.  </p>
<p>Be absolutely specific with your questions and assume very little.  For example, when asking for the difference between two dates always ask whether they want calendar days or working days.  </p>
<p>Users will often find this process laborious. Be polite and remind them that a thorough interview process will result in a better report in less time. </p>
<p>Do your report request procedures differ from mine?  Any suggestions? </p>
<p>Tomorrow, I’ll discuss <a href="http://www.made2mentor.com/2009/10/reporting-guidelines-standardize-your-layouts/">report layouts</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2009/10/reporting-guidelines-requesting-reports/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Basic Reporting Guidelines  &#8211;  Week of Articles</title>
		<link>http://www.made2mentor.com/2009/10/basic-reporting-guidelines-week-of-articles/</link>
		<comments>http://www.made2mentor.com/2009/10/basic-reporting-guidelines-week-of-articles/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 17:27:12 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SSRS]]></category>
		<category><![CDATA[Crystal Reports]]></category>
		<category><![CDATA[Reports]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=2116</guid>
		<description><![CDATA[
			
				
			
		
<p>A good percentage of my day is spent writing reports. The report formats could be SQL Server Reporting Services (SSRS), Crystal Reports, M2M VFP reports, or ODBC Excel Spreadsheets. The latter three methods usually involve T-SQL, which I write by hand using TOAD for SQL Server or the SQL Server Management Studio (SSMS). In the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Fbasic-reporting-guidelines-week-of-articles%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F10%2Fbasic-reporting-guidelines-week-of-articles%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>A good percentage of my day is spent writing reports. The report formats could be SQL Server Reporting Services (SSRS), Crystal Reports, M2M VFP reports, or ODBC Excel Spreadsheets. The latter three methods usually involve T-SQL, which I write by hand using <a href="http://www.toadsoft.com/toadsqlserver/toad_sqlserver.htm">TOAD for SQL Server</a> or the SQL Server Management Studio (SSMS). In the past, I have searched the web for report design information, and didn’t find much. Anyway, each day this week, I will offer guidelines and suggestions I&#8217;ve learned over the years because I know many of you write reports as I do. For the most part, these will be general and not pertain to any specific report medium. Also, keep in mind that these are simply my guidelines for report creation. Your mileage may vary.</p>
<p><a href="http://www.made2mentor.com/2009/10/reporting-guidelines-requesting-reports/">Day One &#8211; Requesting Reports</a><br />
<a href="http://www.made2mentor.com/2009/10/reporting-guidelines-standardize-your-layouts/">Day Two &#8211; Standardize Your Layouts<br />
<a href="http://www.made2mentor.com/2009/10/report-guidelines-know-what-your-user-wants-and-your-target-numbers/">Day Three &#8211; Know What Your User Wants and Your Target Number(s) </a><br />
<a href="http://www.made2mentor.com/2009/10/report-guidelines-exporting-reports/">Day Four &#8211; Exporting Reports</a><br />
<a href="http://www.made2mentor.com/2009/10/report-guidelines-report-approval-and-deployment/">Day Five &#8211; Report Approval and Deployment</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2009/10/basic-reporting-guidelines-week-of-articles/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Anybody Using Qlickview?</title>
		<link>http://www.made2mentor.com/2009/07/anybody-using-qlickview/</link>
		<comments>http://www.made2mentor.com/2009/07/anybody-using-qlickview/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 02:17:05 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[Reports]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=1722</guid>
		<description><![CDATA[
			
				
			
		
<p>Dashboards are a hot topic these days and I need to investigate the available options.  I had a Qlickview demonstration in person, but I&#8217;d like to talk to any of you who are using it.</p>
<p>So, are any of you folks using it?  Would you mind telling me your opinions of the product? </p>
]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F07%2Fanybody-using-qlickview%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2009%2F07%2Fanybody-using-qlickview%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Dashboards are a hot topic these days and I need to investigate the available options.  I had a Qlickview demonstration in person, but I&#8217;d like to talk to any of you who are using it.</p>
<p>So, are any of you folks using it?  Would you mind telling me your opinions of the product? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2009/07/anybody-using-qlickview/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

