Having a heck of a time figuring out how to get my report to look like the following. Any advice would be very helpful regarding the Median function. In the totals the Median and Average is calculated in the underlying structure not from the calculated values from above. I should note I have no problem getting the Averages it's just the Median part.
I'm grouping by Store then by Item.
Store 1
Item QTY Median Average
Apples 17 31 33
Oranges 182 17 21
Totals 199 25 24
Store 2
Etc.
I found a really good article from this blog but I cannot get it to work with this grouping. Blog Link
The Code I'd like to use looks like this.
The question is now how do I use it properly?
I have tried adding a variable to the Item Group. =Code.AddValue(Fields!myValue.Value) Then in the field of the cell I added =Code.GetMedian()
It returns a garbage result thou.
Dim values As New System.Collections.Generic.List(Of Integer)
Dim valueCounts As New System.Collections.Generic.Dictionary(Of Integer, Integer)
Function AddValue(newValue As Integer) As Integer
values.Add(newValue)
AddValue = newValue
If Not valueCounts.ContainsKey(newValue) Then
valueCounts.item(newValue) = 1
Else
valueCounts.item(newValue) += 1
End If
End Function
Function GetMedian() As Double
Dim count As Integer = values.Count
If count = 0 Then
Return 0
Else
values.Sort()
If count Mod 2 = 1 Then
Return values(CInt((count / 2) - 0.5))
Else
Dim index1 As Integer = count \ 2
Dim index2 As Integer = index1 - 1
Dim value1, value2 As Integer
value1 = values(index1)
value2 = values(index2)
Return (value1 + value2) / 2
End If
End If
End Function