How do i represent an unknown number of columns in SSRS?
Asked Answered
S

5

10

I'm working on a rather complex report in Sql Server Reporting Services. My SP returns a dynamic number of columns each of which are dynamically named.

Basically think of a time keeping application. Each column that is dynamic represents a time bucket that time was charged to for that team. If no time was charged to that bucket for the period of time the report covers it doesn't show. Each bucket has its own identifier which i need to be the column headers.

I have an SP that returns this all. It does it by doing a bit of dynamic SQL with an exec statement (ugly i know but I'm on SQL 2000 so a PIVOT option wouldn't work)

I can have an indefinite number of buckets and any or all might show.

I found this - http://www.codeproject.com/KB/reporting-services/DynamicReport.aspx - which is helpful but in the example he has a finite number of columns and he just hides or shows them according to which ones have values. In my case i have a variable number of columns so somehow i need the report to add columns.

Any thoughts?

Seismism answered 20/5, 2009 at 15:34 Comment(2)
could we see an example or two of your data? chances are that what you're doing with the dynamic columns could either become permanent columns, or into rows of data.Neritic
I'm not at work at the moment but I had two versions of this drawn up. One uses dynamic columns the other is the more "relational" version where i pull each back in its own data set and they relate to another. That is very possible but i can't seem to find a way in SSRS to represent those rows of data as columns either (essentially pivot them)Seismism
S
11

As long as you know a maximum number of columns, it's possible to do this after a fashion.

First, name the columns with a result from your query, so you can either pass it in to the query or derive it there. Second, just build out the report as if it had the maximum number of columns, and hide them if they are empty.

For example, I had to build a report that would report monthly sales numbers for up to a year, but the months weren't necessarily starting in January. I passed back the month name in one column, followed by the numbers for my report. On the .rdl, I built out 12 sets of columns, one for each possible month, and just used an expression to hide the column if it were empty. The result is the report appears to expand out to the number of columns needed.

Of course, it's not really dynamic in the sense that it can expand out as far as you need without knowing the upper bound.

Swindell answered 12/8, 2010 at 14:12 Comment(1)
this is exactly what i ended up doing. Unfortunately the project got scrapped so i never updated this answer and forgot what i did. Thanks.Seismism
M
9

This can be done. I did this and it works fine. You don't have to know the maximum number of columns or show and hide columns in my approach. Use a matrix and modify your sp to return dynamic data to the structure mentioned in this blog post http://sonalimendis.blogspot.com/2011/07/dynamic-column-rdls.html

Mohandas answered 16/7, 2011 at 2:57 Comment(0)
M
4

Build 2 related Datasets, first one for the report content, and the second one for the list of its column labels.

The Dataset of the report content must have a fixed number of columns and name. You can allocate some maximum number of columns.

In this example I have the first 2 columns as fixed, or always visible, and a maximum of 4 columns to be displayed by choice through a multivalued parameter, or depends on the query conditions. And as usual, we may have a total as well. So, it may look like this:

Fixed01, Fixed02, Dyna01, Dyna02, Dyna03, Dyna04, Total

The second Dataset with its values will look like this:

Name    Label
----    -----
Dyna01  Label01
Dyna02  Label02
Dyna03  Label03

I have omitted the 4th Label to demonstrate that not all columns are being used by a certain query condition. Remember that both Datasets are meant to be related to the same query.

Now create a parameter named, say, @columns; populate its Available Values and Default Values with the second Dataset.

For each of those 4 dynamic columns, set the column visibility with the following expression:

=IIf(InStr(join(Parameters!columns.Value,","),"Dyna01"),false,true)

And for each of their column header Text Boxes, use the following expression:

=Lookup("Dyna01", Fields!Name.Value, Fields!Label.Value, "dsColumns")

As for the Total, here is the expression for its visibility:

=       IIf(InStr(join(Parameters!columns.Value, ","), "Dyna01"), false, true)
AndAlso IIf(InStr(join(Parameters!columns.Value, ","), "Dyna02"), false, true)
AndAlso IIf(InStr(join(Parameters!columns.Value, ","), "Dyna03"), false, true)
AndAlso IIf(InStr(join(Parameters!columns.Value, ","), "Dyna04"), false, true)

And here is for its values:

= IIf(InStr(join(Parameters!columns.Value, ","), "Dyna01"), Fields!C01.Value, 0)
+ IIf(InStr(join(Parameters!columns.Value, ","), "Dyna02"), Fields!C02.Value, 0)
+ IIf(InStr(join(Parameters!columns.Value, ","), "Dyna03"), Fields!C03.Value, 0)
+ IIf(InStr(join(Parameters!columns.Value, ","), "Dyna04"), Fields!C04.Value, 0)

That's all, hope it helps.

Bonus, that second Dataset, dsColumns, can also hold other column attributes, such as: color, width, fonts, etc.

Massa answered 10/5, 2012 at 3:47 Comment(0)
H
4

I think the best way to do it is add all the columns in your table and edit the visibility property of it with the help of arguments that you get from your SP..this will solve the purpose of dynamic column but when viewing the report you will get a lot of white-space which you can solve with SSRS - Keep a table the same width when hiding columns dynamically? and your report will be ready

Helvetii answered 18/10, 2012 at 10:11 Comment(0)
U
0

I've had the need to do this in the past and the conclusion I came to is "you can't", however I'm not positive about that. If you find a solution, I'd love to hear about it.

An issue that comes to mind is that you need to define the report using the names of the columns that you're going to get back from the stored proc, and if you don't know those names or how many there are, how can you define the report?

The only idea I had on how to do this is to dynamically create the report definition (.rdl file) via C#, but at the time, I wasn't able to find an MS API for doing so, and I doubt one exists now. I found an open source one, but I didn't pursue that route.

Ululant answered 20/5, 2009 at 15:44 Comment(1)
Flagging as the correct answer because, well, we never ended up solving this one given the platform we were on and constraints. We ended up changing the requirement.Seismism

© 2022 - 2024 — McMap. All rights reserved.