Hiding and Showing Columns Based On a Parameter in Reporting Services

Hiding and showing columns is simple in Reporting Services - each column has a "Visibility" property that can be set using an expression. The expression could be driven by a parameter, calculation or data and only needs to resolve to "True" or "False".

One way I use to hide and show columns is allowing the user to select which columns to show using a multi-value parameter (to do this, you must be running at least Reporting Services 2005 which introduced multi-value parameters). Here's my method, step-by-step:

Step 1 is to set up the parameter. In the "Parameters" dialog, add a new parameter called "ColumnsToDisplay", type some values and labels in, and mark the parameter "Multi-value". Make sure your parameter does not accept Nulls or Blanks:

Step 2 is to create a table with columns that can be hidden or shown:

The query behind the dataset is shown below:

SELECT 'Sales' AS Department, 'Established 1/Jan/2006' AS Column1, 'Located in Building A' AS Column2, 'Managed by Fred' AS Column3, '10 Staff' AS Column4
UNION
SELECT 'Payroll', 'Established 4/Dec/2005', 'Located in Building S', 'Managed by Paul', '3 Staff (2 Part-Timers)'
UNION
SELECT 'IT', 'Established 9/Jun/2008', 'Located in Building S', 'Managed by Julie', '4 Staff'
UNION
SELECT 'Executive Office', 'Established 1/Sep/2006', 'Located in Building G', 'Managed by Colin', '2 Staff'

Step 3 is to add a function called "IsColumnSelected" that accepts the multi-value parameter and a column name and returns a boolean value:

''' <summary>
''' Return whether the passed column name has been selected
''' in the multi-value parameter, whether it should be visible
''' or not.
''' </summary>
Public Shared Function IsColumnSelected(ByRef objMultiValueParam() As Object, ByVal strColumnName As String) As Boolean

    ' return whether the passed column name is in the multi-value
    ' parameter array
    Return 0 < InStr(vbNullChar & Join(objMultiValueParam, vbNullChar) & _
        vbNullChar, vbNullChar & strColumnName & vbNullChar)

End Function

This function does the job of looking through the multi-value parameter (an object array) and returning whether the passed column name is in the array, without a loop. It's based on old VB tip at http://www.devx.com/vb2themax/Tip/18364 which uses the Visual Basic "Join" function to combine the array to a string, and then check if the passed column name is in the string. Simple, and it works for small amounts of data (like this sample).

Step 4 hooks all the pieces together using an expression that needs to be put in each column's "Visibility" property which calls "IsColumnSelected". Put the expression below in the "Visibility" property of the first column:

=Not Code.IsColumnSelected(Parameters!ColumnsToDisplay.Value, "Column1")

This expression needs to be put in each column that needs to be hidden or shown, passing the correct column name as the second parameter.

After doing this, you should have a basic method to hide or show columns based on a multi-value parameter in Reporting Services:

Notes: One limitation I should mention is that the PDF export format still allows space for the hidden columns, even if they're not shown. Other formats (Excel, HTML) don't do this.

Tags: , , , ,

OT: Star Wars Culture

Via Delicious Popular Bookmarks, I found a great link to Star Wars-inspired art from design blog Abduzeedo.

I loved Star Wars as a kid and my eldest son TJ has inherited the obsession (we even have to ask *not* to talk about Star Wars when we're playing games or discussing movies!)

Tags: , ,

Configurable Date Grouping (Day, Week or Month) using SQL

Recently I needed configurable grouping in an SQL statement - the exact requirement was that the results be grouped by day, week or month, depending on the user's selection.

This can be done without a table of dates or dynamic SQL by using a CASE statement in the GROUP BY clause.

Here's an example with CASE to achieve configurable date grouping using the "TransactionHistory" table in AdventureWorks:

--"group by" variable

DECLARE @GroupBy TINYINT

--******** CHANGE GROUPING HERE ********

--group by Day (0), Week (1) or Month (2)

SET @GroupBy = 2

--**************************************

 

--need to set the first day of week to be Monday so that

--the datepart calculations work

SET DATEFIRST 1

 

SELECT  --apply dynamic grouping based on "@GroupBy"

        --note that the "GROUP BY" clause needs to match this CASE statement

        [TheDate] = CASE

            --day: ignore time by converting to date-only string format then back to date

            WHEN @GroupBy = 0 THEN CONVERT(SMALLDATETIME, CONVERT(VARCHAR(25),

                                   TH.[TransactionDate], 106))

            --week: get next Sunday after the date by adding (7 - weekday)

            --e.g. to a Tuesday (day 2) we add 5 to get Sunday

            --     to a Sunday, we add 0

            WHEN @GroupBy = 1 THEN CONVERT(SMALLDATETIME, CONVERT(VARCHAR(25),

                                   DATEADD(day, 7 - DATEPART(weekday, TH.[TransactionDate]),

                                   TH.[TransactionDate]), 106))

            --month: get first day of month by calculating the current

            --day of month minus (the date minus 1)

            --e.g. on the 5th of the month, minus 4

            --     on the first of the month, minus 0

            WHEN @GroupBy = 2 THEN CONVERT(SMALLDATETIME, CONVERT(VARCHAR(25),

                                   DATEADD(day, -(DATEPART(day, TH.[TransactionDate]) - 1),

                                   TH.[TransactionDate]), 106))

        END, 

        --some value here

        COUNT(TH.[TransactionID]) AS [NumberOfTransactions]

FROM    AdventureWorks.Production.[TransactionHistory] TH WITH (NOLOCK)

GROUP BY

        CASE

            WHEN @GroupBy = 0 THEN CONVERT(SMALLDATETIME, CONVERT(VARCHAR(25),

                                   TH.[TransactionDate], 106))

            WHEN @GroupBy = 1 THEN CONVERT(SMALLDATETIME, CONVERT(VARCHAR(25),

                                   DATEADD(day, 7 - DATEPART(weekday, TH.[TransactionDate]),

                                   TH.[TransactionDate]), 106))

            WHEN @GroupBy = 2 THEN CONVERT(SMALLDATETIME, CONVERT(VARCHAR(25),

                                   DATEADD(day, -(DATEPART(day, TH.[TransactionDate]) - 1),

                                   TH.[TransactionDate]), 106))

        END

ORDER BY 1 ASC

I tried this in SQL Server 2005 but it should work in SQL Server 2000 as well (additionally, full instructions on getting the AdventureWorks sample database are here on MSDN).

Tags: , , ,

OT: Improving my T-SQL What?

Aussie SQL Server MVP Rob Farley has been busy lately, speaking at TechEd (and blogging his presentation here).

He's doing his TechEd talk at the Melbourne SQL Server User Group on Tuesday night and obviously means fair-dinkum business, as evidenced by the subject line of the invite e-mail :-)

(The full subject actually read "AUSSUG: Invitation: Improving Your T-SQL Arsenal (Rob Farley)", but in my e-mail client it was cut off...he he...)

Tags: ,

User Interface That Works - The Microwave With Only 4 Buttons

At work we have a very simple microwave with only 4 buttons (not counting the door open lever/button):

This microwave manages to be one of the most effective user interfaces I've ever come across. Pressing the plus ("+") button ups the time in 10 second increments, until you reach 90 seconds, and then it ups the time in 1 minute intervals. Once you pass 90 seconds, the display shows whole minutes. You can't set a time of 17 seconds, for example, or even 6 minutes and 30 seconds.

Pressing the minus button decreases the time by 1 minute if the current time is more more than 90 seconds, and then it decreases the time in 10 second intervals down to zero. Pressing the "Start" button increases the time in 1 minute intervals, but only up to 3 minutes.

All this I've figured out by experimentation. I reckon it's the lack of numeric precision and the associated traditional numeric keypad that makes this 4-button microwave so effective. The "+", "-", "Start" and "Cancel" buttons are self-explanatory, and experimenting with them instantly tells you what they do.

Contrast this with the horrible boiling water unit that I blogged about previously, where the buttons are unlabeled (and clicking them does nothing anyway), and the 4-button microwave comes out a long way in front.

Links: http://theruntime.com/blogs/thomasswilliams/archive/2008/04/23/a-short-rant-on-one-example-of-why-over-engineering-stuff.aspx

Tags: , ,

Flexible Audit Trigger for SQL Server 2005

I came across a really interesting audit trigger for SQL Server 2005 a little while back, and thought it was worth re-posting it here.

The trigger basically logs changes for only columns that are affected into a generic audit table. It's got some fancy bit manipulation coding (that I still don't fully understand) and uses dynamic SQL, but hey, since I normally stay away from triggers anyway, I can accept a little dynamic SQL :-)

The alternative is to have an audit table that closely resembles my real table and log changes to that. And then run queries to find what was changed, and when. And then do this for every table I want to audit. And then...you get the picture.

You can find this amazing trigger at Pop Rivett's SQL Server FAQ No.5: Pop on the Audit Trail. It met my needs perfectly, but it may not meet yours: it's worth testing the impact of dynamic SQL and the effect if you use @@IDENTITY after inserts.

Links: http://www.simple-talk.com/sql/database-administration/pop-rivetts-sql-server-faq-no.5-pop-on-the-audit-trail/

Tags: , , ,

The Perfect Storm of Source Control

I've been holding off upgrading my Subversion repository and tools, until last week or so when the "perfect storm" of source control arrived: VisualSVN Server with Subversion 1.5, TortoiseSVN 1.5, and the last piece of the puzzle, AnkhSVN 2.0.

If, like me, you've been waiting for the right time to upgrade your Subversion source control to the newly-released 1.5, then the free VisualSVN Server is one official (and dead-simple) way to do this as CodeBetter's James Kovacs recently noted. My older 1.3 repository was upgraded painlessly using VisualSVN Server, although I could have also called svnadmin upgrade from a command line as per this advice.

To Watch Out For: One point of confusion for me was the fact that I was running SvnService, installed with "Subversion 1 Click Setup" years ago, which still used the old Subversion binaries. I stopped the service, removed it and then renamed the old "Subversion" folder so that I could use the 1.5 binaries shipped with VisualSVN Server. VisualSVN Server doesn't add the path to the Subversion binaries to your environment variables though - this is one step you might have to do manually.

TortoiseSVN is a Subversion front-end for Windows Explorer. I've used it for years and have just upgraded to the latest 1.5 version, after upgrading my repository. The new version offers better icons...and of course much more (!), in addition to the existing integrated spell-checking, diff and merge tool, icon overlays, support for all Subversion protocols, etc. My "better icons" comment, while tongue-in-cheek, is actually serious: files not under version control or that are ignored are marked with different icon overlays, which is very handy.

Although I'm happy with TortoiseSVN, I wanted to include AnkhSVN 2.0 in my toolkit for its Visual Studio integration. I've been reading the Subversion blog and noted that AnkhSVN 2.0 offers full integration with Visual Studio 2005 and 2008. It certainly worked as advertised for me, installing simply enough and working immediately without any mucking around.

Good luck with your own upgrade - you'll have to decide for yourself whether this latest "perfect storm" of synchronised releases is worth upgrading to. And may your experience with the latest Subversion version and associated tools be as good as mine!

Links: VisualSVN Server http://www.visualsvn.com/server/, TortoiseSVN http://tortoisesvn.tigris.org/, AnkhSVN http://ankhsvn.open.collab.net/

Tags: , ,

OT: Logo for SQL Bits

SQL Bits is having a logo competition, and I entered the following:

My goal was to feature the familiar (often yellow) database cylinder, with simple text. I don't expect to win, but given the prize (an XBox 360) I thought it would be worth a try :-)

Links: http://www.sqlbits.com/LogoCompetition.aspx

Tags: , ,

Blogged with the Flock Browser

OT: Book Review: "Choosing and Using Your Home Computer: An Introductory Course"

My mother-in-law Marijke gave me a special surprise the other day: a hand-me-down copy of the 1984 tome "Choosing and Using Your Home Computer: An Introductory Course".

Marijke had bought it some time ago to learn about computers. The book is a classic, from the time when PC's were called "microcomputers". It covers the latest hardware available at the time, the BASIC language, accessories like printers, plotters and the new-fangled "mouse", sprite graphics, games and the business case for computers.

What makes this book more poignant is that I was into computers in 1984. I had a Commodore 64, and one of my best mates had its predecessor, the Vic 20. I remember peeking and poking!

I'll post some more pics later, but for now enjoy the front cover and coverage of the "joystick":

Tags: , ,

Hello TheRuntime!

I'm Thomas Williams, and I blog about SQL Server, Reporting Services, .NET, and development in general.

My old blog at DotNetJunkies was in desperate need of updating - thanks to Jay for setting me up here at my new home! Please subscribe to TheRuntime main feed or my personal feed to make sure you stay up-to-date with my posts.

More about me

I work in the health industry in Frankston, Victoria, Australia and live with my beautiful wife Olivia and 3 young kids. I am an MCP (going for MCAD.NET) and a MCTS in SQL Server 2005.

My full-time job involves writing software to process large amounts of data for enterprise data sources, using SQL Server 2005, SSIS, Reporting Services, and windows and web front ends (this is what I find myself blogging most about too, surprisingly).

I have a personal blog at Thomas Williams.blog, and I also enjoy movies, reading, board games, XBox, and some non-geeky things as well :-)

Tags: , ,

«October»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678