Median in Report

J

Jason

Hi,

I'm trying to use the example code from Microsoft's knowledge base to
calculate the median in a report (code below). The code works fine for me in
calculating the the mean of the entire dataset of my table/query. The
problem I run into is when I I try to calculate the same median function in a
report on a grouping, instead of returning the median for that specific
grouping, it returns the median for the entire dataset repeatedly under each
grouping.

As an example, my report is grouped based on Industry, and I would like to
return the median calculation of revenues as outlined below

Industry 1

Company1 $100
Company2 $180
Company3 $300
Company4 $450
Company5 $500
Median $300

Industry 2

Company1 $180
Company2 $250
Company3 $375
Median $250

In the current form, instead of returning the median for the grouping ($300
for Industry1 and $250 for Industry2), I instead am getting the median for
the entire set of 8 records ($275 in this case) and it is displaying under
both the median for Industry1 and Industry2. Can someone suggest how I might
alter the code below or possibly provide other coding that would handle this?
Thanks.

Function Median(tName As String, fldName As String) As Single
Dim MedianDB As DAO.Database
Dim ssMedian As DAO.Recordset
Dim RCount As Integer, i As Integer, x As Double, y As Double, _
OffSet As Integer
Set MedianDB = CurrentDb()
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
"] FROM [" & tName & "] WHERE [" & fldName & _
"] IS NOT NULL ORDER BY [" & fldName & "];")
'NOTE: To include nulls when calculating the median value, omit
'WHERE [" & fldName & "] IS NOT NULL from the example.
ssMedian.MoveLast
RCount% = ssMedian.RecordCount
x = RCount Mod 2
If x <> 0 Then
OffSet = ((RCount + 1) / 2) - 2
For i% = 0 To OffSet
ssMedian.MovePrevious
Next i
Median = ssMedian(fldName)
Else
OffSet = (RCount / 2) - 2
For i = 0 To OffSet
ssMedian.MovePrevious
Next i
x = ssMedian(fldName)
ssMedian.MovePrevious
y = ssMedian(fldName)
Median = (x + y) / 2
End If
If Not ssMedian Is Nothing Then
ssMedian.Close
Set ssMedian = Nothing
End If
Set MedianDB = Nothing
End Function
 
C

Charles Wang [MSFT]

Hi Jason,
From your description, I think that your code sample should work for this
requirement. Could you please elaborate what the issue of your current
code?

Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
============================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Median in Report 1
Calculation for Median 3
median 31
Calculating median in a group by query 4
YES 0
Calculating the Median for specified groups 1
Please help 6
Statistical Median Code 3

Top