Below I’m going to show you a few quick examples of how to use the Switch function within SSRS expressions.
In the first example we will change the font color based on the quantity of units sold.
Green will be a quantity of 20,000 +, Black will be a quantity of 10,000 +, and everything less than 10,000 will be displayed as Red.
To do something like this go to the properties of the selected cell in the table and then go to Font/Color. On the drop down list for color, choose expression and enter the following expression.
=Switch(Fields!TotalQuantity.Value <= 10000, "Red" ,
Fields!TotalQuantity.Value >= 20000, "Green",
Fields!TotalQuantity.Value > 10000, "Black")
After the above expression has been entered run the report and you should see something like the image below.
For the next example let’s suppose you have three columns in your report LastMonth, ThisMonth, and NextMonth. That’s pretty easy but what if you had a business rule that said if it’s less than the 3rd day of the month then show last month. You can handle this situation as well by using the Switch expression. For my example today I will be using day 26 so you can see how it works.
First create an expression as we did before and use the following code.
=Switch(Day(Now()) < 26,format(dateadd("m",-1,dateadd("d",1-datepart("d",today()),today())),"MMM yyyy"),
Day(Now()) >= 26,format(dateadd("m", 0,dateadd("d",1-datepart("d",today()),today())),"MMM yyyy"))
From the image below in yellow you can see that the day was set to day 27 or higher because it’s showing the correct Month, January 2011. If you look at the blue area below you can see that the months are now 1 month behind beucase I set the day to 26 and the date of writing this blog is January 25th, 2011.
As with most things in SSRS you can do something in more than 1 way but today I wanted to introduce you to the Switch Function.