Search code examples
vb.netvisual-studio-2012reporting-servicescrystal-reportsssrs-2012

AVG multiple Lookup expression in SSRS (Visual Studio)


If I have 6 people and each person has 2 Survey Responses. Each Survey Responses has 10 questions. On my dataset, I have one column that has all the questions (1, 2, 3, 4.... )and the second column has all the answers correspond to questions. See image below.

I'm trying Average the Scores of Questions 1, 2, 5 & 8 for each person.

I've added a VB code in the Report Properties and use the expression below and able to get the AVG for question 1. Is there a way to incorporate the AVG of Question 2, 5 & 8?

=Code.AvgLookup(
  LookupSet(
    Fields!Instructorname.Value & "1. Course achieved?",
    Fields!Instructorname.Value & Fields!Questions.Value,
    Fields!Scores.Value, "DataSet1")
 )

Question & Answer Layout

enter image description here


Solution

  • It seems your function takes only one question for the calculation. Note LookupSet() returns a Object[] and despite there is not any explicit method to concatenate/union arrays in SSRS, there is a trick to do so using JOIN and SPLIT functions:

    =Code.AvgLookup(split(
      join(
        LookupSet(Fields!Instructorname.Value & "1. Course achieved?",
          Fields!Instructorname.Value & Fields!Questions.Value,
          Fields!Key.Value,Fields!Code.Value,"DataSet1"),","
      )
      & "," &
      join(
        LookupSet(Fields!Instructorname.Value & "2. Content was easy to understand",
          Fields!Instructorname.Value & Fields!Questions.Value,
          Fields!Key.Value,Fields!Code.Value,"DataSet1"),","
      )
      & "," &
      join(
        LookupSet(Fields!Instructorname.Value & "5. Material was easy to understand",
          Fields!Instructorname.Value & Fields!Questions.Value,
          Fields!Key.Value,Fields!Code.Value,"DataSet1"),","
      )
      & "," &
      ... -> join(lookupset(...) for question 8 and so on.
    ,","))
    

    Despite this works, it could take more time to process your report. The correct way to handle this should be creating the proper table structures and relationships that lets you get the data you need in your report directly from your source.