Pages

Men

rh

4/20/2011

Reporting Service Formulas

String Functions

1) Combine more than one field by using concatenation operators and Visual Basic constants. The following expression returns two fields, each on a separate line in the same text box:

=Fields!FirstName.Value & vbCrLf & Fields!LastName.Value

2) Format dates and numbers in a string with the Format function. The following expression displays values of theStartDate and EndDate parameters in long date format:

=Format(Parameters!StartDate.Value, "D") & " through " &  Format(Parameters!EndDate.Value, "D")

3) If the text box contains only a date or number, you should use the Format property of the text box to apply formatting instead of the Format function within the text box.

The Right, Len, and InStr functions are useful for returning a substring, for example, trimmingDOMAIN\username to just the user name. The following expression returns the part of the string to the right of a backslash (\) character from a parameter named User:

=Right(Parameters!User.Value, Len(Parameters!User.Value) - InStr(Parameters!User.Value, "\"))

4) The following expression results in the same value as the previous one, using members of the .NET FrameworkSystem.String class instead of Visual Basic functions:

=Parameters!User.Value.Substring(Parameters!User.Value.IndexOf("\")+1, Parameters!User.Value.Length-Parameters!User.Value.IndexOf("\")-1)

5)Display the selected values from a multivalue parameter. The following example uses the Join function to concatenate the selected values of the parameter MySelection into a single string that can be set as an expression for the value of a text box in a report item:

= Join(Parameters!MySelection.Value)

6)The Regex functions from the .NET Framework System.Text.RegularExpressions are useful for changing the format of existing strings, for example, formatting a telephone number. The following expression uses theReplace function to change the format of a ten-digit telephone number in a field from "nnn-nnn-nnnn" to "(nnn)nnn-nnnn":

=System.Text.RegularExpressions.Regex.Replace(Fields!Phone.Value, "(\d{3})[ -.]*(\d{3})[ -.]*(\d{4})", "($1) $2-$3")
Note:
Verify that the value for Fields!Phone.Value has no extra spaces and is of type System.String.

Conversion Function:

7) You can use Visual Basic functions to convert a field from the one data type to a different data type. Conversion functions can be used to convert a the default data type for a field to the data type needed for calculations or to combine text. 


The following expression converts the constant 500 to type Decimal in order to compare it to a Transact-SQL money data type in the Value field for a filter expression.  

=CDec(500) 


8)The following expression displays the number of values selected for the multivalue parameter MySelection.
=CStr(Parameters!MySelection.Coun

9) The Iif function returns one of two values depending on whether the expression is true or not. The following expression uses the Iif function to return a Boolean value of True if the value of LineTotal exceeds 100. Otherwise it returns False:

=IIF(Fields!LineTotal.Value > 100, True, False)
  
10)Use multiple IIF functions (also known as "nested IIFs") to return one of three values depending on the value of PctComplete. The following expression can be placed in the fill color of a text box to change the background color depending on the value in the text box. 

=IIF(Fields!PctComplete.Value >= 10, "Green", IIF(Fields!PctComplete.Value >= 1, "Blue", "Red"))

11)Values greater than or equal to 10 display with a green background, between 1 and 9 display with a blue background, and less than 1 display with a red background. 

A different way to get the same functionality uses the Switch function. The Switch function is useful when you have three or more conditions to test. The Switch function returns the value associated with the first expression in a series that evaluates to true:

=Switch(Fields!PctComplete.Value >= 10, "Green", Fields!PctComplete.Value >= 1, "Blue", Fields!PctComplete.Value = 1, "Yellow", Fields!PctComplete.Value <= 0, "Red",)

12)Values greater than or equal to 10 display with a green background, between 1 and 9 display with a blue background, equal to 1 display with a yellow background, and 0 or less display with a red background.  

A third way to get the same functionality uses the Choose function. The Choose function uses the first parameter as an index to one of the remaining function parameters. The first parameter must be an integer. If the background color of a text box in a table is set to this expression, the value of MyIndex controls the color.

=Choose(Fields!MyIndex.Value,"Red","Green","Yellow")

For example, when MyIndex is 1, the background color is "Red". In the query result set, verify that the data forMyIndex does not exceed the range of the parameter list.  

13)Test the value of the ImportantDate field and return "Red" if it is more than a week old, and "Blue" otherwise. This expression can be used to control the Color property of a text box in a report item: 

 =IIF(DateDiff("d",Fields!ImportantDate.Value, Now())>7,"Red","Blue")

14) Test the value of the PhoneNumber field and return "No Value" if it is null (Nothing in Visual Basic); otherwise return the phone number value. This expression can be used to control the value of a text box in a report item. 


=IIF(Fields!PhoneNumber.Value Is Nothing,"No Value",Fields!PhoneNumber.Value)
15) Test the value of the Department field and return either a subreport name or a null (Nothing in Visual Basic). This expression can be used for conditional drillthrough subreports. 

=IIF(Fields!Department.Value = "Development", "EmployeeReport", Nothing)

 16) Test if a field value is null. This expression can be used to control the Hidden property of an image report item. In the following example, the image specified by the field [LargePhoto] is displayed only if the value of the field is not null. 

=IIF(IsNothing(Fields!LargePhoto.Value),True,False)

Report Functions

Reporting Services provides additional report functions that you can use to manipulate data in a report. This section provides examples for two of these functions. 
Sum 



17) The Sum function can total the values in a group or data region. This function can be useful in the header or footer of a group. The following expression displays the sum of data in the Order group or data region:   

=Sum(Fields!LineTotal.Value, "Order") 


18) You can also use the Sum function for conditional aggregate calculations. For example, if a dataset has a field that is named State with possible values Not Started, Started, Finished, the following expression, when placed in a group header, calculates the aggregate sum for only the value Finished:

=Sum(IIF(Fields!State.Value = "Finished", 1, 0))


RowNumber 


19) The RowNumber function, when used in a text box within a data region, displays the row number for each instance of the text box in which the expression appears. This function can be useful to number rows in a table. It can also be useful for more complex tasks, such as providing page breaks based on number of rows.

The scope you specify for RowNumber controls when renumbering begins. The Nothing keyword indicates that the function will start counting at the first row in the outermost data region. To start counting within nested data regions, use the name of the data region. To start counting within a group, use the name of the group.

=RowNumber(Nothing)



Appearance of Report Data
You can use expressions to manipulate how data appears on a report. For example, you can display the values of two fields in a single text box, display information about the report, or affect how page breaks are inserted in the report.
Page Headers and Footers

20) When designing a report, you may want to display the name of the report and page number in the report footer. To do this, you can use the following expressions: 
 
The following expression provides the name of the report and the time it was run. It can be placed in a text box in the report footer or in the body of the report. The time is formatted with the .NET Framework formatting string for short date: 

=Globals.ReportName & ", dated " & Format(Globals.ExecutionTime, "d")



21) The following expression, placed in a text box in the footer of a report, provides page number and total pages in the report: 
=Globals.PageNumber & " of " & Globals.TotalPages

22) The following examples describe how to display the first and last values from a page in the page header, similar to what you might find in a directory listing. The example assumes a data region that contains a text box named LastName. 

The following expression, placed in a text box on the left side of the page header, provides the first value of theLastName text box on the page: 

=First(ReportItems("LastName").Value)

23) The following expression, placed in a text box on the right side of the page header, provides the last value of the LastName text box on the page: 

=Last(ReportItems("LastName").Value)

24) The following example describes how to display a page total. The example assumes a data region that contains a text box named Cost. 

The following expression, placed in the page header or footer, provides the sum of the values in the Cost text box for the page: 

=Sum(ReportItems("Cost").Value)


Page Breaks


25) In some reports, you may want to place a page break at the end of a specified number of rows instead of, or in addition to, on groups or report items. To do this, create a group that contains the groups or detail records you want, add a page break to the group, and then add a group expression to group by a specified number of rows. 

The following expression, when placed in the group expression, assigns a number to each set of 25 rows. When a page break is defined for the group, this expression results in a page break every 25 rows. 

=CInt(Ceiling(RowNumber(Nothing)/25)) 


26) To allow the user to set a value for the number of rows per page, create a parameter named RowsPerPage and base the group expression on the parameter, as shown in the following expression:

=CInt(Ceiling(RowNumber(Nothing)/Parameters!RowsPerPage.Value))


Properties
Expressions are not only used to display data in text boxes. They can also be used to change how properties are applied to report items. You can change style information for a report item, or change its visibility.
Formatting 


27) The following expression, when used in the Color property of a text box, changes the color of the text depending on the value of the Profit field: 

=Iif(Fields!Profit.Value < 0, "Red", "Black")
28) The following expression, when used in the BackgroundColor property of a report item in a data region, alternates the background color of each row between pale green and white: 

=Iif(RowNumber(Nothing) Mod 2, "PaleGreen", "White")
 
29) If you are using an expression for a specified scope, you may have to indicate the dataset for the aggregate function: 

=Iif(RowNumber("Employees") Mod 2, "PaleGreen", "White")

Visibility

30) You can show and hide items in a report using the visibility properties for the report item. In a data region such as a table, you can initially hide detail rows based on the value in an expression. 

The following expression, when used for initial visibility of detail rows in a group, shows the detail rows for all sales exceeding 90 percent in the PctQuota field: 

=Iif(Fields!PctQuota.Value>.9, False, True)



31) The following expression, when set in the Hidden property of a table, shows the table only if it has more than 12 rows: 

 =IIF(CountRows()>12,true,false)


32) You can customize URLs by using report data and also conditionally control whether URLs are added as an action for a text box. 

The following expression, when used as an action on a text box, generates a customized URL that specifies the dataset field EmployeeID as a URL parameter. 

="http://adventure-works/MyInfo?ID=" & Fields!EmployeeID.Value
33) The following expression conditionally controls whether to add a URL in a text box. This expression depends on a parameter named IncludeURLs that allows a user to decide whether to include active URLs in a report. This expression is set as an action on a text box. By setting the parameter to False and then viewing the report, you can export the report Microsoft Excel without hyperlinks. 

=IIF(Parameters!IncludeURLs.Value,"http://Unioneworks.com/productcatalog",Nothing)


Report Data

Expressions can be used to manipulate the data that is used in the report. You can refer to parameters and other report information. You can even change the query that is used to retrieve data for the report.
Parameters

34) You can use expressions in a parameter to vary the default value for the parameter. For example, you can use a parameter to filter data to a particular user based on the user ID that is used to run the report. 

The following expression, when used as the default value for a parameter, collects the user ID of the person running the report: 

=User!UserID 


35) To refer to a parameter in a query parameter, filter expression, text box, or other area of the report, use theParameters global collection. This example assumes that the parameter is named Department

=Parameters!Department.Value

36) Parameters can be created in a report but set to hidden. When the report runs on the report server, the parameter does not appear in the toolbar and the report reader cannot change the default value. You can use a hidden parameter set to a default value as custom constant. You can use this value in any expression, including a field expression. The following expression identifies the field specified by the default parameter value for the parameter named ParameterField

=Fields(Parameters!ParameterField.Value).Value
  
Custom Code


37) You can use custom code in a report. Custom code is either embedded in a report or stored in a custom     assembly which is used in the report.  


The following example calls an embedded code method called ToUSD, which converts the StandardCost field value to a dollar value: 

=Code.ToUSD(Fields!StandardCost.Value)

  =IIf(Fields!StateCount.Value < 25, "Red", "Black")

            =IIf(Fields!StateCount.Value < 25, "Red", IIf(Fields!StateCount.Value > 1000, "Blue", "Black"))



 =IIF(Fields!LastBackUpTaken.Value = "Not Yet Taken","Red", 
         IIF( IsDate(Fields!LastBackUpTaken.Value) = true,
IIF( CDate(Fields!LastBackUpTaken.Value).AddDays(Parameters!DaysTillExpiry.Value)
.      CompareTo(NOW()) = 1,"GreenYellow",
          IIF( CDate(Fields!LastBackUpTaken.Value).AddDays(7)
          .CompareTo(NOW()) = 1, "Yellow", "Red")),"Red"))
  
 =iif(Switch(......) < 0, 0, Switch(....))=Ceiling(rowNumber(Nothing)/10)


Alternate Row color  expression:
     

=iif(Fields!Session_Shared.Value.tostring().Contains("No"),"Brown","DarkGreen")
38) Displaying User Name in the report level:
 ="Report generated by " + User!UserID + " on " + Globals!ExecutionTime + "
       "PST " ---
       
39) Date Display Format(2/23/2011 11:59:59 PM)

=dateadd("d",-(day(Today())-1),Today())--
       
40) Date Display Format : (From 1/1/2011 To 1/31/2011) 

="From " + Parameters!Start_Date.Value + " To " +   

       Parameters!End_Date.Value---From 1/1/2011 To 1/31/2011
=dateadd("s", -1, dateadd("d", 1, today()))

No comments :

Post a Comment