Interview Questions

Thursday, June 29, 2017

Hiding parameter groups or Dynamics filters on SSRS report dialog at runtime

Recently I had a requirement to hide report parameters from report dialog based on the caller menu item. So here is how to do it. I will show you the example of production picking list report. Production picking list report in AX 2012 feature pack version has the following report parameters:
Image1
There are three parameter groups on the report dialog above which are as follows:
  • Parameters
  • If connected to purchase order
  • View
And below is the classDeclaration of the contract class, as shown below:
Image2
The View group fields comes from InventDimViewContract class which is added as a parm method in ProdPickListContract class to show the options for all inventory dimensions.
Suppose, we want to hide all the parameter groups from report dialog and just want to display the dynamics filters on the report. You need to modify the UIBuilder class of your report. For production picking list report, the UI Builder class is ProdPickListUIBuilder. The build method needs to be modified and the code in Bold below can be used to hide the whole parameter group from SSRS report dialog:
public void build()
{
FormBuildGroupControl grp;
grp = this.dialog().curFormGroup();
grp.frameType();
grp.columns(2);
if (this.controller().parmArgs().menuitemName() == #yourMenuItemName)
     {
           grp.visible(false);
     }
super ();
}
Below is the output, after hiding the whole parameter group:
Image3
To hide the dynamic filters from SSRS report dialog based on caller menu item, you need to override the following method in your controller class and return false from this method based on your condition:
showQueryValues
Override this method in your controller class and write the following code:
public boolean showQueryValues(str parameterName)
{
If (this.parmArgs().menuItemName() == menuItemOutputStr(#YourMenuItemName)
{
return false;
}
else
{
return true;
}
}
You will see the following output when the report is run:
Image4
Only the Printer and Print destination options will be shown which is displayed by default on the reports.
Enjoy reporting.

How to pass the default value to parameter in SSRS report in AX 2012?

Issue: If you want to pass default value as parameter in SSRS report, then how we can get the solution?

Solution: 
modify the data contract by overriding the SysReportRunController.preRunModifyContract() method. Assign the default values to start date and end date parameters there.

Using Complex Expressions

Complex expressions can contain multiple built-in references, operators, and function calls, and appear on the design surface as <<Expr>>. To see or change the expression text, you must open the Expression dialog box or type directly in the Properties pane. The following table lists typical ways you can use a complex expression to display or organize data or change report appearance, including the property to set, the dialog box you typically use to set it, and the value for the property. You can type an expression directly into a dialog box, on the design surface, or in the Properties pane.
FunctionalityProperty, Context, and Dialog BoxProperty Value
Calculate aggregate values for a dataset.Value property for a placeholder inside of a text box. Use Placeholder Properties Dialog Box, General.=First(Fields!Sales.Value,"DataSet1")
Concatenate text and expressions in the same text box.Value for a placeholder inside of a text box that is placed in a page header or page footer. Use Placeholder Properties Dialog Box, General.="This report began processing at " & Globals!ExecutionTime
Calculate an aggregate value for a dataset in a different scope.Value for a placeholder inside of a text box that is placed in a tablix group. Use Placeholder Properties Dialog Box, General.=Max(Fields!Total.Value,"DataSet2)
Format data in a text box depending on value.Color for a placeholder inside of a text box in the details row for a tablix. Use Text Box Properties Dialog Box, Font.=IIF(Fields!TotalDue.Value < 10000,"Red","Black")
Calculate a value once to refer to throughout the report.Value for a report variable. Use Report Properties Dialog Box, Variables.=Variables!MyCalculation.Value
Include specific values for more than one field from a dataset.Filter equation for a group in a tablix. Use Tablix Properties Dialog Box, Filters.For data type, select Boolean.

=IIF(InStr(Fields!Subcat.Value,"Shorts")=0 AND (Fields!Size.Value="M" OR Fields!Size.Value="S"),TRUE, FALSE)

=

TRUE
Hide a text box on the design surface, that can be toggled by the user using a Boolean parameter named Show.Hiddenproperty on a text box. Use Text Box Properties Dialog Box, Visibility.=Not Parameters! Show<boolean parameter> .Value
Specify dynamic page header or footer content.Value for a placeholder inside of a text box that is placed in the page header or footer.="Page " & Globals!PageNumber & " of " & Globals!TotalPages
Specify a data source dynamically by using a parameter.Connection string on the Data source. Use Data Source Properties Dialog Box, General.="Data Source=" & Parameters!ServerName.Value & ";initial catalog=AdventureWorks2012"
Identify all the values for a multivalue parameter chosen by the user.Value for a placeholder inside of a text box. Use Tablix Properties Dialog Box, Filters.=Join(Parameters!MyMultivalueParameter.Value,", ")
Specify page breaks for every 20 rows in a tablix with no other groups.Group expression for a group in a tablix. Use Group Properties Dialog Box, Page Breaks. Select the option Between each instance of a group.=Ceiling(RowNumber(Nothing)/20)
Specify conditional visibility based on a parameter.Hidden property for a tablix. Use Tablix Properties Dialog Box, Visibility.=Not Parameters!< boolean parameter >.Value
Specify a date formatted for a specific culture.Value for a placeholder inside of a text box in a data region. Use Textbox Properties Dialog Box, General.=Fields!OrderDate.Value.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"))
Concatenate a string and a number formatted as a percentage to two decimal places.Value for a placeholder inside of a text box in a data region. Use Textbox Properties Dialog Box, General.="Growth Percent: " & Format(Fields!Growth.Value,"p2")

Using Simple Expressions in SSRS

Simple expressions appear on the design surface and in dialog boxes in brackets, for example, a dataset field appears as [ProductID]. Simple expressions are created for you automatically when you drag a field from a dataset onto a text box. A placeholder is created and the expression defines the underlying value. You can also type expressions directly into a data region cell or text box, both on the design surface or in a dialog box, (for example, [ProductID]).
The following table lists examples of the ways you can use simple expressions. The table describes the functionality, the property to set, the dialog box you typically use to set it, and the value for the property. You can type the simple expression directly on the design surface, in a dialog box, or in the Properties pane, or you can edit it in the Expression dialog box, just as you would with any expression.
FunctionalityProperty, Context, and Dialog BoxProperty Value
Specify a dataset field to display in a text box.Value property for a placeholder inside a text box. Use Placeholder Properties Dialog Box, General.[Sales]
Aggregate values for a group.Value property for a placeholder inside a row associated with a tablix group. Use Textbox Properties Dialog Box.[Sum(Sales)]
Include a page number.Value property for a placeholder inside a text box that is placed in a page header. Use Textbox Properties Dialog Box, General.[&PageNumber]
Display a selected parameter value.Value property for a placeholder inside a text box on the design surface. Use Textbox Properties Dialog Box, General.[@SalesThreshold]
Specify a group definition for a data region.Group expression on the tablix group. Use Tablix Group Properties Dialog Box, General.[Category]
Exclude a specific field value from a table.Filter equation on the tablix. Use Tablix Properties Dialog Box, Filters.For data type, select Integer.

[Quantity]

>

100
Include only a specific value for a group filter.Filter equation on the tablix group. Use Tablix Group Properties Dialog Box, Filters.[Category]

=

Clothing
Exclude specific values for more than one field from a dataset.Filter equation for a group in a tablix. Use Tablix Properties Dialog Box, Filters.=[Color]

<>

Red

=[Color]

<>

Blue
Specify sort order based on an existing field in a table.Sort expression on the tablix. Use Tablix Properties Dialog Box, Sorting.[SizeSortOrder]
Link a query parameter to a report parameter.Parameters collection on the dataset. Use Dataset Properties Dialog Box, Parameters.[@Category]

[@Category]
Pass a parameter from a main report to a subreport.Parameters collection on the subreport. Use Subreport Properties Dialog Box, Parameters.[@Category]

[@Category]

How do I Format Decimal Places to 0 or 2 Decimal Places

For this issue you can use following expressions:
1: =Replace(Round(Fields!Rate.Value,2),".00","")

or 

2: =Round(CDec(Replace(Replace(Fields!Rate.Value,".00",""),"00","")),2)


SQL Server Reporting Services Tips and Tricks to Improve the End User Experience

Problem
In this tip, we will look at a few SQL Server Reporting Services (SSRS) Tips and Tricks to improve the end-user experience.
Solution
Let's take a look at the following Tips and Tricks:
  • Display Total Number of Pages while Navigating
  • Display Everything in a Single Page
  • Display Report Parameter Selection
  • Display No Rows Error Message
  • Page Settings for Optimal Printing
NoteThis tip assumes you have experience in building simple to medium complexity SSRS Reports.
For the purpose of this demonstration, let's build a simple report.
Create a new project, add a new report, and create a data source pointing to AdventureWorksDW database. Then create a dataset with the following query.

Report Main Dataset - "dstSSRSTipsPart2"

SELECT
    DPC.EnglishProductCategoryName AS Category
    , DPS.EnglishProductSubcategoryName AS Subcategory
    , DP.EnglishProductName AS Product
    , FIS.OrderDate
    , FIS.SalesAmount
    , FIS.Freight AS FreightAmount
    , FIS.TaxAmt AS TaxAmount
FROM
    dbo.FactInternetSales AS FIS
    INNER JOIN dbo.DimProduct AS DP
        ON FIS.ProductKey = DP.ProductKey
    INNER JOIN dbo.DimProductSubcategory AS DPS
        ON DP.ProductSubcategoryKey = DPS.ProductSubcategoryKey
    INNER JOIN dbo.DimProductCategory AS DPC
        ON DPS.ProductCategoryKey = DPC.ProductCategoryKey
    INNER JOIN dbo.DimDate AS DD
        ON FIS.OrderDateKey = DD.DateKey
WHERE DD.CalendarYear = 2005
Next, add a Table report item to the designer with five columns as Category, Subcategory, Product, OrderDate, and SalesAmount in the same sequence. The completed report, with some basic formatting, should look as shown below.
Total Pages showing as ? in navigation bar

Display Total Number of Pages while Navigating a SSRS Report

With the release of SQL Server 2008, many new features were introduced in SSRS. One of those features was "On-Demand" report processing, which is a very nice performance improvement especially for large reports. With this feature, when the report is viewed in Report Manager, SSRS displays estimated number of pages with a question mark ("?") at the end, to indicate that there are more pages to be displayed, as shown below.
Total Pages showing as ? in navigation bar
Often times users mistake this to be a bug, but this is actually the result of a feature. And hence, there are requests to display the total number of pages and remove the question mark. Let's see how we can achieve this.
To address this issue, we need to use the "TotalPages" Built-in Field either in the report header or footer. Add a textbox either in the header or footer with the following expression and hide the textbox, so that it's not visible to the users.
=Globals!TotalPages
This will fix the issue and the total number of pages are displayed in the navigation bar and the question mark ("?") disappears as shown below.
Total Pages showing correctly in navigation bar

Display Everything in a Single Page for a SSRS Report

In certain scenarios, especially in cases like dashboards, users want all the contents to be displayed in a single page. However, when the contents of the page are more than what can actually fit on one page, the report spans across two pages.
To display all the contents of a report in one page when the report is rendered in Report Manager, set the Interactive Height property of the report to zero as shown below.
Display everything on one page
For the purpose of this demonstration, set this property for our report and notice that, all the contents are displayed in a single page as shown below. NoteKeeping all the contents on a single page for a long report can adversely impact the report rendering performance. Test, measure, and evaluate whether it's suitable for your scenario or not, before the report is released to users.
Display everything on one page

Display Report Parameter Selection for a SSRS Report

Parameters/Filters are a great way for the users to interact with the report. It is a good practice to display the parameter selection in the report as it offers various benefits including the following:
  • At a quick glance, it shows the list of values selected in each of the parameters.
  • When the report is exported and is shared with others, they get a picture of what selection of parameters resulted in the output report.
For the purpose of this demonstration, add two parameters to the report - Category & Subcategory. Configure the parameters using the below steps.
Create two datasets using the below two queries, one for each parameter. The first dataset with the first query is for the Category parameter and the second dataset with the second query is for the Subcategory parameter.

Dataset for Category Parameter - "dstCategory"

SELECT
    ProductCategoryKey AS CategoryKey
    , EnglishProductCategoryName AS CategoryName
FROM dbo.DimProductCategory

Dataset for Subcategory Parameter - "dstSubcategory"

SELECT
    ProductSubcategoryKey AS SubcategoryKey
    , EnglishProductSubcategoryName AS SubcategoryName
FROM dbo.DimProductSubcategory
ORDER BY ProductCategoryKey, EnglishProductSubcategoryName
Create and configure the two parameters using the following settings.
Parameter Property
Category Parameter
Subcategory Parameter
NamepCategorypSubcategory
PromptCategorySubcategory
Data TypeIntegerInteger
SelectionMulti-select
(Allow Multiple Values)
Multi-select
(Allow Multiple Values)
Available Values
    DatasetdstCategorydstSubcategory
    Value FieldCategoryKeySubcategoryKey
    Label FieldCategoryNameSubcategoryName
Default Values
    DatasetdstCategorydstSubcategory
    Value FieldCategoryKeySubcategoryKey
Note that, for simplicity, we are not cascading the parameters even though these two parameters are good candidates for cascading parameters.
Next update the main report dataset "dstSSRSTipsPart2" with the following query. This is to link the above two parameters with the report's main dataset.
SELECT
    DPC.EnglishProductCategoryName AS Category
    , DPS.EnglishProductSubcategoryName AS Subcategory
    , DP.EnglishProductName AS Product
    , FIS.OrderDate
    , FIS.SalesAmount
    , FIS.Freight AS FreightAmount
    , FIS.TaxAmt AS TaxAmount
FROM
    dbo.FactInternetSales AS FIS
    INNER JOIN dbo.DimProduct AS DP
        ON FIS.ProductKey = DP.ProductKey
    INNER JOIN dbo.DimProductSubcategory AS DPS
        ON DP.ProductSubcategoryKey = DPS.ProductSubcategoryKey
    INNER JOIN dbo.DimProductCategory AS DPC
        ON DPS.ProductCategoryKey = DPC.ProductCategoryKey
    INNER JOIN dbo.DimDate AS DD
        ON FIS.OrderDateKey = DD.DateKey
WHERE 
    DD.CalendarYear = 2005
    AND DPC.ProductCategoryKey IN (@pCategory)
    AND DPS.ProductSubcategoryKey IN (@pSubcategory)
Map the Parameters in the above dataset with the corresponding Report Parameters. Parameter Mapping should look as shown below.
Parameter Mapping for Main Report Dataset
Next add two textboxes to the report above the Table with the following expressions.

Expression for 1st Textbox for Category Parameter

="Selected Categories: " & Join(Parameters!pCategory.Label, ", ")

Expression for 2nd Textbox for Subcategory Parameter

="Selected Subcategories: " & Join(Parameters!pSubcategory.Label, ", ")
Your report design surface should look as shown below.
Report designer after adding the textboxes for Category and Subcategory Parameters
Now render the report with default values selected in both Category and Subcategory parameters and the rendered report looks as shown below.
Rendered report with default parameter selection
Next select "Bikes" under the Category Parameter and "Mountain Bikes, Road Bikes, Touring Bikes" under the Subcategory parameter and render the report and the rendered report looks as shown below.
Rendered report with default parameter selection
Notice that, based on the values selected in the parameters, the corresponding values/labels are displayed in the report. This can be really useful information specifically for users viewing the report offline.

Display No Rows Error Message for a SSRS Report

Sometimes when there is no data to display in the report, the rendered report is displayed as blank, which is not very intuitive to the end users. SSRS has a feature, which allows displaying a custom message when there are no rows to display in the table.
Select the Table and go Properties window and notice that there is a property called "NoRowsMessage" under "No Rows" category. Enter the following text as a value for that property. Notice that, you can either enter static text or it can be dynamic text based on an expression.

No data to display for the selected input parameter values!

Also, notice that, there are additional formatting options under "No Rows" category. Choose the font style, weight, color etc. and the properties windows should look as shown below.
Table Properties configuration for No Rows Error Message
"NoRowsMessage" property is applicable to Table, Matrix, and List data regions. The corresponding property for charts is "NoDataMessage" and for the color scale for a map is "NoDataText".
Now render the report by selecting "Components" in the Category filter and leave the default selection in the Subcategory filter. The output report should look as shown below.
Rendered report with No Rows Error Message

Page Settings for Optimal Printing for a SSRS Report

Often when the reports are exported or printed, we see different issues, which can be fixed by making appropriate page settings. Following are some of the commonly encountered issues:
  • Report items getting clipped across multiple pages. For instance, few columns appear on one page and next set of columns appear on a different page.
  • Blank pages appear in the PDF or Printed Versions.
To address this issue, we need to configure the page settings accordingly. Use the following rule of thumb while setting the width of a page.
Page Width >= Body Width + Left Margin + Right Margin
Make a note of the Width of the Report Body in the properties window. Make sure to select "Body" from the drop down while checking the size.
Total Pages showing correctly in navigation bar

Now configure the page width as shown below. Go to "Report" --> "Report Properties..." --> "Page Setup".
Total Pages showing correctly in navigation bar
As we can see in this tip, SSRS is a very powerful reporting tool, and we just need to explore it to cater to most of the end user requirements.