SSRS: Checking for Divide By Zero Using Custom Code

I encountered a divide-by-zero error while working on an SSRS report and thought the issue could easily be resolved using IIF with code similar to the following:

=IIF(Fields!Denominator.Value = 0, 0, Fields!Numerator.Value/Fields!Denominator.Value)

I soon realized that this does not resolve the issue.  It appears that all parameters  in the IIF function are evaluated regardless if the first parameter evaluates to true or false.  Therefore, the divide-by-zero was still occurring.

After doing some research, I decided that the best option to avoid the divide-by-zero error is to implement custom code.

Note: The following screen shots are from Report Builder 3.0

The first step is to open the Report Properties window.  You can access the report properties by clicking anywhere outside of the report body.

If you still cannot see the Report Properties window, make sure you have the ‘Properties’ option checked in the ‘View’ tab.

image

The Report Properties window is displayed below.  In the Code text box, click the ellipse […].  You may need to click on the Code text box first to see the ellipse button.

image

Next, select ‘Code’ in the left hand menu if it is not already selected.  Paste the code (displayed below screen shot) in the Custom code field.

SNAGHTML9a4f0b7

Function Divide(Numerator as Double, Denominator as Double)
If Denominator = 0 Then
Return 0
Else
Return Numerator/Denominator
End If
End Function

Now that you’ve created the custom code, you can begin to use the code in your report.  The following is an example of how you can use the Divide function in a text box expression:

=Code.Divide(Fields!CurrentYearSales.Value-Fields!PriorYearSales.Value,Fields!PriorYearSales.Value)*100

Posted in Database and BI, Technology | Tagged | Leave a comment

SSIS: Implementing IsNumeric( ) Logic Using the Data Flow Script Component

SSIS does not include an ‘out-of-the-box’ isNumeric( ) function.  Fortunately, this functionality can be implemented using the script component in the data flow.

I am going to make the assumption that readers are familiar with the SSIS script component.  If not, please feel free to read my prior blog post on the subject: SSIS Dataflow Script Component

To implement isNumeric( ) behavior, I used the C# Int32.TryParse method.

TryParse(String, Int32):  Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.

Here’s a snippet of the code in C#:

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    int BatteryQuantity1Int;

    if (! int.TryParse(Row.BatteryQuantity1Input, out BatteryQuantity1Int))
       BatteryQuantity1Int = 0;   

    //Add an output row and set the column values
    Output0Buffer.AddRow();
    Output0Buffer.ModelNumber = Row.ModelNumberInput;
    Output0Buffer.ModelYear = Row.ModelYearInput;
    Output0Buffer.ModelName = Row.ModelNameInput;

I also came across a blog post by Dustin Ryan where he implements IsNumeric( ) functionality using the Derived Column Transform.  The post is found here.

Posted in Database and BI, Technology | Tagged | Leave a comment

SSIS: Implementing Package Logging

SSIS logging is configured on a package-by-package basis.  SSIS provides a flexible event logging environment that allows developers to configure: the container or scope to log (generally the entire package), the type of events to log, and the log provider type (SQL Server, Windows Event Log, Text Files, SQL Server Profiler or XML Files).

Whenever I set up logging in an SSIS package, I always log events at the package level using the SQL Server log provider.  The type of events I log are: On Error, On Post Execute, On Pre Execute, On Task Failed, and On Warning.

The following is a list of steps to configure package logging as I described above:

Right click on the control flow surface and select ‘Logging’ (or select ‘Logging’ in the SSIS menu)

image

In the ‘Containers’ section, you select the container or scope for logging.  I always select the package level (to log events in the entire package) by checking the box next to the package name. 

In the ‘Add a new log’ section, select ‘SSIS log provider for SQL Server’ then click ‘Add’.

In the ‘Configuration’ field, select the SQL Server database where you want to store logged events.

SNAGHTML1244667

Next, click the ‘Details’ tab.  On the details tab you specify the types of events you want to log.  I always select the following: On Error, On Post Execute, On Pre Execute, On Task Failed, and On Warning.

SNAGHTML12ceddd

  • On Error – Writes a log entry when an error occurs.
  • On Post Execute – Writes a log entry immediately after the executable has finished running.
  • On Pre Execute  – Writes a log entry immediately before the executable runs.
  • On Task Failed – Writes a log entry when a task fails.
  • On Warning – Writes a log entry when a warning occurs.

When finished, click ‘OK’. 

You should also create an event handler for each type of event you’re logging.  To create an event handler, click the ‘Event Handlers’ tab.

image

In the ‘Event Handler’ drop-down, select the appropriate event type.  Then click the link to create an event handler for that specific event.  You need to repeat this process for each type of event (On Error, On Post Execute, On Pre Execute, On Task Failed, and On Warning)

image

When you’re finished, the list in the ‘Event handler’ drop-down should look like this:

image

Logged events are stored in the dbo.sysssislog system table in the database you specified while configuring package logging.

Posted in Database and BI, Technology | Tagged | 1 Comment

SSIS: Issues with the Excel Data Source

I discovered some undesirable behavior with the SSIS  Excel Data Source.  Although this issue has been well-documented for a while, I thought it is worth mentioning again.

I had one column in my spreadsheet that was sparsely populated, nearly all blank values.  When I ran my SSIS package to extract the data from the spreadsheet, every row in that column was set to NULL including those that had a valid value in the spreadsheet.

Upon further investigation, I discovered the issue stems from the Jet driver.  Apparently, by default, only the first 8 rows of data are scanned to determine the data type for each column.  If the first 8 rows of data in a column are blank, then any valid data in the additional rows is ignored and treated as NULL.  It is possible to change the value in the registry so more than 8 rows are scanned.

In addition, I also discovered that if you use the Excel data source and want to run your package from BIDS, then ‘Run64BitRuntime’ in the solution properties must be set to False.  You can configure this as follows:

Right click on the solution in BIDS and select ‘Properties’.

image

Set Run64BitRuntime to False.

image

Posted in Database and BI, Technology | Tagged | 1 Comment

Book Review: “Expert Cube Development with Microsoft SQL Server 2008 Analysis Services”

A few months back I purchased the book “Expert Cube Development with Microsoft SQL Server 2008 Analysis Services” by Chris Webb, Alberto Ferrari and Marco Russo.  I recently finished reading this book and thought I’d share my impressions.

Expert Cube Development with Microsoft SQL Server 2008 Analysis Services

  • I recommend this book to anyone who has a solid understanding of SSAS, works with SSAS on a regular basis and wants to take their cube development and optimization to the next level.  This is not a beginner level book. 
  • The content of this book is very well-organized.  The first chapter provides an overview of basic dimensional modeling concepts: Ralph Kimball vs. Bill Inmon, dimensions and facts, star schema vs. snowflake schema, etc.  The remaining chapters are all specific to SSAS starting with building dimensions and cubes, creating measures and measure groups, adding transactional data to fact tables, enhancing the cube with MDX calculations, implementing currency conversions in SSAS, query performance tuning, implementing security, “productionization”, and monitoring cube performance and usage. 
  • This book does not focus on  menus or user interface (it is assumed that you work with SSAS regularly and already know this); nor does it provide an overview of every feature SSAS offers.  Instead, the authors focus on best practices (from their perspectives) gained from years of experience working with SSAS. 
  • The book describes common scenarios and challenges encountered during cube development and provides reasons why one approach may be better than another and when you’d want to select one option over another.  Many other books I’ve read explain the features and options but never delve into a detailed explanation of the pros/cons like this book does.
  • Links to additional sources of information such as blog posts, white papers and other reference materials are shared throughout the book.  I have not read all of the additional reference materials but the ones I did read were high-quality.

I personally have about 8 months of experience working with SSAS – this book provided me with lots of good tips and recommendations that are not ‘common knowledge’ and can only be gained through years of experience with the product.  I feel that this book is a great investment and a resource I can leverage well into the future.

Posted in Database and BI, Technology | Tagged | Leave a comment