Finding the median

pumpmerchant

Board Regular
Joined
Mar 27, 2007
Messages
117
Hi, I am trying to find the median on a column of data but can't find the operator for this - all I can see are sum, max, etc. Is there any easy way round this?

Many thanks
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Access doesn't have a Median function (as you found). There are some workarounds. For example:

1. Sort Descending on the field whose median you want
2. Create a Rank calculation to generate the rank for each record
3. Filter the Rank calculation for the record with the middle ranking (as this is a sorted query, you will have the middle value in the field, ie the median).

The following SQL does the above, for a field called NetRevenue from tblProjectData:

Code:
SELECT tblProjectData.NetRevenue, (SELECT Count(*) FROM tblProjectData As P WHERE [NetRevenue] > [tblProjectData].[NetRevenue])+1 AS Rank
FROM tblProjectData
WHERE ((((SELECT Count(*) FROM tblProjectData As P WHERE [NetRevenue] > [tblProjectData].[NetRevenue])+1)=Int((SELECT COUNT(*) FROM tblProjectData)/2)))
ORDER BY tblProjectData.NetRevenue DESC;

Dump this into NotePad, replace the field and table names with your own, and place the result into a new query.

Note: If your field or table names have any spaces, use square brackets around the name -- eg,
YourTable is OK, as is [Your Table]. Your Table (without the brackets) will generate an error.

Denis
 
Upvote 0
Adding an extra field to the median formula

Hello SydneyGeek. I used your SQL code to get the median, and it works fine, but I'm having trouble adding in an extra field to the filter. I want to get the median value of some time values [TimeDiff] by month.

Adding in the month field simply gives me the month that the overall median is for, but what I want is the median for each month. I have the data sorted by month, and then by [TimeDiff], so I think it is ready, I'm just not sure how to add it to the code.

Thanx

Sade
 
Upvote 0
Finding the median by category threw a whole new layer into the equation. I needed 2 queries to get it to go, because doing it in 1 generated syntax errors.

Setup:
2 tables.

First, UK_CarSales
SalesID
ManufacturerID
Month
Year
Sales

Second, Manufacturers
ManufacturerID
Manufacturer

The first query has the 2 tables joined on ManufacturerID, with
Manufacturer -- sort Ascending
Sales -- sort Descending
Year
Month
in the grid.

Then, 3 calculated fields --
Code:
Rank: (SELECT COUNT(*) FROM UK_CarSales As U INNER JOIN Manufacturers As M ON U.ManufacturerID = M.ManufacturerID WHERE (U.Sales>UK_CarSales.Sales AND Manufacturer=Manufacturers.Manufacturer))+1

MidValue: (SELECT COUNT(*) FROM UK_CarSales As U INNER JOIN Manufacturers As M ON U.ManufacturerID = M.ManufacturerID WHERE ( Manufacturer=Manufacturers.Manufacturer))\2

MatchUp: [Rank]=[MidValue]

I saved this as qrySales_Rank1 and built a new query based on the first.

Fields in grid:
Manufacturer
Sales
Year
Month
Matchup

Criteria for Matchup is -1
No sorting in the second query

Denis
 
Upvote 0

Forum statistics

Threads
1,215,265
Messages
6,123,961
Members
449,135
Latest member
jcschafer209

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top