Scenario: I want to Group By/ Group By during run time/ Optional Group by with None option: the Report dynamically chosen by the User (by User Parameter)
Solution:
Resource: http://www.erikmonchen.com/2011_05_01_archive.html, Erik Monchen
- Make sure you include all the fields for which you want to add group possibility.
- Create a parameter (user input option) called GroupBy1 and settings for the parameter:
- Data type: text
- Multiple select: No
- Available values: Add the values that you want the end users to select. The label doesn't matter what you enter, the value must be the same name as the field name in your report data: data set.
- Default value: Whichever value you think the end user wants.

- Now in the Row Group, add Parent Group and select group by: expression and type the following:
=Fields(Parameters!GroupBy1.Value).Value

==================================================================
Optional Grouping with option None:
1. Add a parameter called "GroupBy" with following values:
- General Tab: Allow null value
- Available tab:
2. Add a parent group in the SSRS with expression:
- =iif (Parameters!GroupBy.Value is Nothing,1,Fields(iif(Parameters!GroupBy.Value is Nothing, "Division",Parameters!GroupBy.Value)).Value)
3. GroupBy Details Textbox expression:
- General Tab: =Fields(iif(Parameters!GroupBy.Value is Nothing, "Division", Parameters!GroupBy.Value)).Value
- Visibility tab: =Parameters!GroupBy.Value is nothing
4. GroupBy Header Textbox expression:
- Visibility tab:
=Parameters!GroupBy.Value is nothing
=================================================================
How Dynamic Grouping works:
Question:How can I give my users the ability to dynamically select fields on which to group within a report?
Answer:The key to dynamic grouping in a report is this: Practically everything in a report can be based on an expression. From grouping to column headers to column and row visibility... since all of it is expression-based, it can be based on parameters supplied by the user.
Step 1: Build your report with static grouping.
It will be easier to start with a standard report with non-dynamic grouping and modify it from there.
Step 2: Define the grouping parameters.
For each dynamic group, create a parameter for the field name on which to group. Add a valid values list containing the names of the fields on which you want to allow grouping. If you want grouping to be optional, also include null (with a label like "None").
Step 3: Change your group expressions to make them based on the parameters
The trick here is the indexer into the Fields collection. Normally, you have static group expressions like this: =Fields!Year.Value.
But you can refer to items in the Fields collection using an alternate string-based syntax like this:=Fields("Year").Value.
Since the field name is just a string, that means you can use any string subexpression instead. In particular, you can use the parameter:
=Fields(Parameters!FirstFieldName.Value).Value
If you want to allow for optional grouping, this is slightly more complicated:
=iif(Parameters!Group1.Value is Nothing,1,Fields(iif(Parameters!Group1.Value is Nothing, "Year",Parameters!Group1.Value)).Value)If the parameter value is Nothing, you can just group on a constant (like 1).
Notice the second iif embedded within the Fields collection indexer expression. This is needed because you'll get an error if you try to access the Fields collection with a null indexer [Remember: Visual Basic evaluates all arguments of all functions, so the outer iif wouldn't be enough to stop the Fields collection from erroring if the parameter value is null]. If the parameter value is Nothing, the third argument to the iif will return the value of the Year field, but it won't be used for anything.
Step 4: Change the column headings and report data to be based on the parameters
Before:
Customer Year
=Fields!Customer.Value =Fields!Year.Value
After:
=Parameters!Group1.Value =Parameters!Group2.Value
=Fields(Parameters!Group1.Value).Value =Fields(Parameters!Group2.Value).Value
Note: Again, if you're allowing for optional grouping, you'll need to make things slightly more complex:
=Fields(iif(Parameters!Group1.Value is Nothing, "Year", Parameters!Group1.Value)).Value
Step 5: Hide columns and rows for optional grouping
If you're allowing for optional grouping, you'll need to hide the unneeded columns and rows. You can do this by setting the Hidden property of the corresponding column and row to: =Parameters!Group1.Value is Nothing
No comments:
Post a Comment