Learn more about SQL Server tools

mssqltips logo
giveaway
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

Display column headers for missing data in SSRS matrix report


By:   |   Read Comments (1)   |   Related Tips: > Reporting Services Formatting

Problem

I need to create a matrix report using SQL Server Reporting Services (SSRS) that can show all months in the column header even for the months that have missing data.

Below is the yearly report and we see some months are missing data and therefore the column headers don't show.

Current Report - Description: Here is the report where you can see missing months.

The expected output is shown below:

Expected report - Description: here is the report output we are exoecting

This tip explains the steps to develop the matrix report to show column headers for all months even when there is no data for these columns.

Solution

For this solution, I am using the below T-SQL query to develop the SSRS report. As you can see, the below query is returning Sales Amount for all the employees for a given year. If we pass the year value we are going to get the data for the months within that year.

SELECT 
   b.FirstName+' '+b.LastName as EmployeeName
   ,[SalesAmountQuota]
   ,MONTH([Date]) as CalenderMonth
   ,YEAR([Date]) as CalenderYear
FROM [dbo].[FactSalesQuota] as a
INNER JOIN [dbo].[DimEmployee] as b on a.EmployeeKey=b.EmployeeKey
WHERE YEAR([Date])=@Year
   

However, in this scenario there are a few months that have no data and therefore those months do not show in the matrix report headers. The idea is to modify the T-SQL query to include the missing months, so they display in the matrix headers.

SSRS Report Development

For this solution, I am using a T-SQL query from the AdventureworksDW2016 database and SQL Server Data Tools 2015 version in order to develop this SSRS report.

The first step is to modify the above query in order to include the missing months into the dataset. I will first declare a table variable called @months, then I will write the below T-SQL and using a loop to load the @months table will all the month numbered from 1 to 12.

DECLARE @months table (MonthNum int)
DECLARE @i int =1

WHILE (@i<=12)
BEGIN
   INSERT INTO @months(MonthNum)
   SELECT @i
   SET @i=@i+1
END

SELECT * FROM @months

Once we execute the above, we can see the output results will all the month numbers as shown in the below image.

Tablevariable - Description: Results from table variable

Modifying the Existing T-SQL Query

The next step is to modify the existing report query to include the table variable @months. Using a LEFT JOIN to join the @months table to the FactSalesQuota table, in this case even if the main data table is missing data for few months we will still get all the months in the output, but the rest of the columns, other than MonthNumber, will have NULL values.

DECLARE @months table (MonthNum int)

DECLARE @i int = 1 
WHILE (@i < = 12) 
BEGIN 
   INSERT INTO @months(MonthNum) 
   SELECT @i 
   SET @i = @ i + 1 
END 

SELECT 
   b.FirstName + ' ' + b.LastName as EmployeeName 
   ,[SalesAmountQuota] 
   ,m.MonthNum  as CalenderMonth 
   ,MONTH([Date]) as CalenderMonth_old 
   ,YEAR([Date]) as CalenderYear 
FROM @months as m 
LEFT JOIN [dbo].[FactSalesQuota] as a on m.MonthNum=MONTH(a.[Date]) and YEAR([Date])=@Year 
LEFT JOIN [dbo].[DimEmployee] as b on a.EmployeeKey=b.EmployeeKey 

The next step is to update the report data set query with the above updated T-SQL code and then refresh the fields as shown in the below image.

Querydesigner - Description: Refresh the feilds by updating with new SQL Query

After running the report, we can now see the matrix columns for all the months as shown in the below image.

After fixing the Query - Description: Here is the report output right after fixing the code.

However, there are couple of things we need to fix in this report.

We want to remove the records where Employee Name has no data. To do this we can remove blank employee rows by writing a simple filter expression on the Employee row group section in the SSRS report as shown below.

ISNOTHING(Fields!EmployeeName.Value) = FALSE
   

Group filter - Description: here is the row filter expression for filtering the data.

We also want to display 0ís instead of blank values.  This can be done by writing an IIF expression on the value text box as shown in the below image.

=IIF(ISNOTHING(Sum(Fields!SalesAmountQuota.Value)),0,Sum(Fields!SalesAmountQuota.Value))
   
Value filter - Description: Value filter expression to display blank values to 0

Final Report

After fixing the above two formatting items, we can rerun the report and now the report displays as needed.

final report - Description: Here is the final report , after fixing these items
Next Steps
  • When you have the same or similar requirement you can follow these steps to develop a solution using T-SQL code and SSRS expressions.
  • As you can see, we can easily display column headers even when data is missing by doing a few workarounds in our T-SQL code and formatting.
  • Download the SSRS rdl file for this report


Last Update:






About the author
MSSQLTips author Amar Reddy Amarendra Reddy Thummeti is a Senior Business Intelligence Engineer who works in Silicon Valley and holds a Master's Degree in Computer Science.

View all my tips





Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Notify for updates 


SQL tips:

*Enter Code refresh code     



Thursday, May 18, 2017 - 2:45:35 PM - John Back To Top

 

 This is so helpful. thank you Amarendra Reddy Thummeti.


Learn more about SQL Server tools