Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Using SSRS - Matrix Control to Generate Columns Dynamically

3.67/5 (4 votes)
9 Apr 2013CPOL2 min read 95.4K  
This will help you generate columns dynamically in SSRS reports using Matrix control.

Scenario

In a support center there are agents who assist customers for solving their issues with company products. Company needs to track, amount of time spent by agents on various activities. Report is required in following format:

Image 1

Number and Name of Activities are not fixed in this report because:

  • New activities can be added
  • Existing activities can be modified

So, the columns “Time spent on Activity 1” to “Time spent on Activity N” need to be generated dynamically.

Solution

Table structure

Agent Table: Image 2

Activity Table:

Image 3
Agent Activity Mapping Table: Image 4

Query that gives me all Agent, Activity data:

SQL
SELECT A.AgentName, ACT.ActivityName, ASM.TimeInMinutes
 FROM   dbo.Agent_ActivityMapping ASM INNER JOIN dbo.Agent A  ON
  A.AgentID = ASM.AgentRef INNER JOIN dbo.Activity ACT  ON 
  ACT.ActivityID = ASM.ActivityRef

Image 5

Step 2: Right click on “Shared Data Source” folder in solution explorer and click on “Add New Data Source”:

Image 6

Step 3: Now right click on “Reports” folder in solution explorer and click on Add new item and select Report. I have named my report as “ActivityMatrix”

Step 4: Double click on report to open it. In Report Data pane, Right click on Dataset folder and Add Dataset. In this dataset, I have used the same query that we had created to fetch all the Agent-Activity data.

Image 7

Step 5: Now insert a Matrix onto the report.

Image 8

Step 6: Now drag Agent name into data section of first column. Drag ActivityName into header of second column. That is because result of Activity element will give us the column names. It will look something like this:

Image 9

Now, drag “TimeInMinutes” element from dataset to data of second column. This column will give us sum of time spent on an activity by an agent.

Image 10

Step 7: In order to add a column at the end of the report, right-click and select following:

Image 11

Notice that there are 4 options of inserting a column. First 2 options are for adding column inside the group i.e., the ActivityName group. Last two options are for adding column outside the group. So we will select last option in our case. This will be a computed column. This needs to be the sum of time spent on various activities by each agent i.e., [Sum(TimeInMinutes)]. So, our report looks like this now:

Image 12

Final preview of report is as follows:Image 13

Target achieved!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)