reporting services tips

Upload: marcel-chis

Post on 03-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Reporting Services Tips

    1/3

    Reporting Services tips

    1

    Reporting Services tips

    Set a table's visibility to false when it has no rows

    So to set the (table) Visibilty / Hidden property to hide when there are no rows, it's simply

    =Iif (CountRows() > 0, false, true)

    Get record count or row count of rows in reporting service table object?

    Use this expression where ever you like

    =RowNumber("table1")

    if you add above expression into a new column in table 1 you'll see the exact number of the

    your filtered rows , and you can use the Last one as the count

    Date format in SSRS

    You can use the Format()function to achieve this. If you select the field you want to format

    and then go to Properties->Expressions you could do something like this:

    =Format(Fields!DateCreated.Value,"yyyy/MM/dd")

    Adding an Else to your Switch

    September 14, 2009 inReporting Services|6 comments

    In this short article I will be talking about two functions in the SQL Server Reporting Services

    (SSRS) function stack. Those functions are IIF() and Switch(). And Ill be showing you how

    easy it is to add an Else part to the Switch function.

    Two commonly-used functions in Reporting Services are theIIF()and the Switch(). These

    are two functions of the Program Flow type, or Decision Functions as they are called onthis

    MSDN page.

    In case youre wondering why its so difficult to find a function reference for the built-in

    functions of SSRS, its because these are actually Visual Basic functions and Microsoft refersto those for any detailed explanation. Clickthis link for the IIF() functionin the Visual Basic

    Language Reference, andthis one for the Switch().

    Anyone whos done some programming most likely already knows theif then

    else statement. If evaluates to true then

    gets executed, else gets executed.

    The IIF()works in the same way. According to its description it

    Returns one of two objects, depending on the evaluation of an expression.

    This is its definition:

    http://stackoverflow.com/questions/514043/set-a-tables-visibility-to-false-when-it-has-no-rows-in-reporting-serviceshttp://dba.stackexchange.com/questions/23668/date-format-in-ssrshttp://blog.hoegaerden.be/category/sqlserver/reporting-services-sqlserver/http://blog.hoegaerden.be/category/sqlserver/reporting-services-sqlserver/http://blog.hoegaerden.be/category/sqlserver/reporting-services-sqlserver/http://blog.hoegaerden.be/2009/09/14/adding-an-else-to-your-switch/#commentshttp://blog.hoegaerden.be/2009/09/14/adding-an-else-to-your-switch/#commentshttp://blog.hoegaerden.be/2009/09/14/adding-an-else-to-your-switch/#commentshttp://msdn.microsoft.com/en-us/library/ms157328.aspxhttp://msdn.microsoft.com/en-us/library/ms157328.aspxhttp://msdn.microsoft.com/en-us/library/ms157328.aspxhttp://msdn.microsoft.com/en-us/library/ms157328.aspxhttp://msdn.microsoft.com/en-us/library/27ydhh0d.aspxhttp://msdn.microsoft.com/en-us/library/27ydhh0d.aspxhttp://msdn.microsoft.com/en-us/library/27ydhh0d.aspxhttp://msdn.microsoft.com/en-us/library/dft2z9yf.aspxhttp://msdn.microsoft.com/en-us/library/dft2z9yf.aspxhttp://msdn.microsoft.com/en-us/library/dft2z9yf.aspxhttp://msdn.microsoft.com/en-us/library/dft2z9yf.aspxhttp://msdn.microsoft.com/en-us/library/27ydhh0d.aspxhttp://msdn.microsoft.com/en-us/library/ms157328.aspxhttp://msdn.microsoft.com/en-us/library/ms157328.aspxhttp://blog.hoegaerden.be/2009/09/14/adding-an-else-to-your-switch/#commentshttp://blog.hoegaerden.be/category/sqlserver/reporting-services-sqlserver/http://dba.stackexchange.com/questions/23668/date-format-in-ssrshttp://stackoverflow.com/questions/514043/set-a-tables-visibility-to-false-when-it-has-no-rows-in-reporting-services
  • 8/12/2019 Reporting Services Tips

    2/3

    Reporting Services tips

    2

    Public Function IIf( _ByVal Expression As Boolean, _ByVal TruePart As Object, _ByVal FalsePart As Object _) As Object

    Heres a simple example:

    =IIf(Fields!YearlyIncome.Value >= 60000,"High","Low")

    Using this expression, the "High" string is returned when the value of the YearlyIncome field

    is equal to or above 600, while the string "Low" is returned when the value is below 600.

    Now have a look at the following example. It has been nicely structured with indentation and

    line breaks to make reading easier.

    =IIF

    (Sum(Fields!LineTotal.Value) >= 100,"Violet",

    IIF()(

    Sum(Fields!LineTotal.Value) < 25,"Transparent","Cornsilk"

    ))

    As you see, it shows a nested IIF inside another one. Imagine that there were several more

    nestings and that line breaks were not used by the coder. Would be a nightmare to read,

    right?

    Thats why the Switch()was invented. The description for the Switch function reads:

    Evaluates a list of expressions and returns an Object value corresponding to the first

    expression in the list that is True.

    And this is the function definition:

    Public Function Switch( _ByVal ParamArray VarExpr() As Object _

    ) As Object

    In Reporting Services, the VarExpr parameter is simply an even list of expressions and/or

    object references separated by commas. Which comes down to something like this:

    Switch(, val1, , val2).

    Heres a simple example:

    =Switch(

    Fields!State.Value = "OR", "Oregon",

    Fields!State.Value = "WA", "Washington")

  • 8/12/2019 Reporting Services Tips

    3/3

    Reporting Services tips

    3

    This expression says that if the value for the State field is "OR" then the Switch function willreturn "Oregon", and so on

    Now, to get to the point of this article, the Switch function does not contain an ELSE part like

    the IIF does.

    But I wouldnt be writing this if there wasnt a workaround, would I? If you read the

    Switchs description closely, it says that it will return the first expression in the list that is

    true. So each expression is evaluated in the order that they are passed to the function. To get

    ELSE-like behavior we would need an expression that evaluates to True but only when all

    other expressions are False. So, why not use True as expression? Its the simplest expression

    that I can think of and it does the works!

    Have a look at the following, its a rewrite of the last IIF example mentioned earlier.

    =Switch

    (Sum(Fields!LineTotal.Value) >= 100, "Violet",Sum(Fields!LineTotal.Value) < 25, "Transparent",True, "Cornsilk"

    )

    So, which one do you think is the most readable? The IIF, or the Switch? These are only

    simple examples that Ive been using, imagine situations with ten or more possibilities. Well,

    I think youve got my point by now.

    Quick tip for users ofReport Builder 2.0:to be able to format your expression with line

    breaks and tabs, you need to use CTRL + ENTER or CTRL + TAB in the Expression

    Builder. Just hitting ENTER will close the popup window. Its quite annoying if youre used

    to the BIDS interface, but it works

    http://msdn.microsoft.com/en-us/library/dd220530.aspxhttp://msdn.microsoft.com/en-us/library/dd220530.aspxhttp://msdn.microsoft.com/en-us/library/dd220530.aspxhttp://msdn.microsoft.com/en-us/library/dd220530.aspx