Search code examples
reporting-servicessubtotal

ssrs GetTotal() Puplic Function


I browsed for solution of total for ReportItem! + ReportItem! and found Puplic Function

Public Shared Value as Decimal =0
  Public Shared Function GetValue(Item as Decimal ) as Decimal 
     value= value + Item
     return Item
  End Function

Public Shared Function GetTotal()
     return value
  End Function

I use it on my column, and in report view all number count is right (last column is total)

last column is total

But. When I export it to Excel I get in that total field different numbers, sometimes it gets * by 4, sometimes by 2, It is random.

excel total

Can someone explain please, why that happening and if there some solution for that. Thanks!

I have a groups (there no detail for this field, only part and project grouping)

And the top grouping using GetValue(ReportItems!Value1 - ReportItems!Value2) enter image description here


Solution

  • To avoid the same expression running multiple times, you can use shared variables combined with a shared list.

    In the list you store keys (group combinations) for which you have already run the expression.

    If the key does not exist you add the value to the variable and add the key to the list.

    Add the following custom code

    Public shared value as Decimal =0
    
    Public shared group1 As New System.Collections.Generic.List(Of String)
    
    Public  Function GetValue(Group as String, Item as Decimal ) as Decimal 
         If group1.Contains(Group) = False
         group1.add(Group)
         value= value + Item
         End If
         return Item
      End Function
    
    Public Function GetTotal()
         return value
      End Function
    

    Your expression should look like that below

    = Code.GetValue(Fields!group1.Value & " " & Fields!group2.Value, ReportItems!value1.value - ReportItems!value2.value)