简体   繁体   中英

Filter a field with concatenated values against a multi-value paramter in SSRS

I have a field called "ContactType" in my SQL dataset. This field has more than one value joined with a "," separator.

Lets say I have two records

Contact Type | RecordId
-------------|----------
A,B,C        | Record1
B,C,E        | Record2

Now in SSRS I have a multi-value parameter to use as a Contact Type filter. The options will be

  • A
  • B
  • C
  • D
  • E
  • F

When I select A and B from the filter I want to check if A or (OR) B present in the above records. The problem here is that the ContactType field is a single string of text but contains more than one value.

How can I filter the values in the ContactType field based on the array of values in the multi-value parameter?

I think the best way to do this would be to write a simple function using custom code. The custom code editor can be accessed from the Report menu.

Then use a function like this:

Function CheckContactType(ContactTypeList AS String, ContactTypeFilter AS Parameter) As Boolean
    Dim contactTypes As String() = ContactTypeList.Split(",")

    For i As Integer = 0 To (ContactTypeFilter.Count-1)
        If contactTypes.Contains(ContactTypeFilter.Value(i)) Then Return True
    Next i

    Return False
End Function

Then your filtering expression would be this:

=Code.CheckContactType(Fields!ContactType.Value, Parameters!ContactTypeFilterParameter)

and you would use the = operator against the value expression:

=True

The custom code function is making use of the .NET String.Split() and Array.Contains() methods. These MSDN pages are quite good for learning more about custom code in SSRS reports:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM