How to use the MEDIAN function
What is the MEDIAN function?
The MEDIAN function calculates the median based on a set of numbers in Excel. The median value is the middle number of a group of numbers when arranged from smallest to largest.
It returns a good measure of the typical value in a data set that is not affected by outliers or skewness . For example, the median income may be a better measure of the average income than the mean. The mean can be distorted by a few very high incomes which is the case, for instance, in the US. The very high incomes represent the outliers.
Table of contents
1. MEDIAN Function Syntax
MEDIAN(number1, [number2], ...)
2. MEDIAN Function Arguments
number1 | Required. A constant or a reference to a cell or cell range which contains numbers. |
[number2] | Optional. Up to 254 additional arguments. |
3. MEDIAN Function Example
The picture above demonstrates the MEDIAN function in cell D3. The data is in cell range B2:B8, here it is:
Numbers |
1 |
2 |
2 |
6 |
1 |
2 |
The following formula in cell D3 calculates the median value based on the values in cell range B3:B8:
If the group contains an even number of numbers the MEDIAN function calculates the average of the two numbers in the middle.
There is no middle number in this group: 1, 1, 2, 2, 2, 6. To calculate the median in this case you need to calculate the average of the two middle numbers. It is calculated like this: (2+2)/2 = 2 This value matches the number calculated in cell D3.
To illustrate using another data set, the middle number of 1, 1, 2, 3, 4, 6 is not possible, we need to calculate the average of the two middle numbers: 2.5. (2+3)/2 = 2.5
This data set has a middle number which is the median: 1, 1, 2, 3, 4, 6, and 7. The middle number is 3 in this specific data set.
4. How the average and the median can differ
The image above shows the income of two different groups in cells B3:B9 and D3:D9 respectively, the data is made-up. The average incomes of these two groups are calculated in cells G3:G4, the calculations show that group 1 has a higher average income than group 2.
However, group 2 has a higher median income than group 1. How is this possible? Group 1 has an outlier that distorts the average (mean) but not the median.
Formula in cell H3:
The chart displays the two groups, group 1 is blue and group 2 is yellow. Both groups are sorted from small to large in order to easily spot the middle number which is the median. Group 1 has an outlier that is drastically larger than the others.
5. Sort rows based on the median values
This image demonstrates a formula in cell B13 that calculates the median for each row then sorts the data in cells B5:N8 by row from small to large. All this is done in one single formula.
To show that the calculation is correct column P contains the median numbers, the formula in cell B13 is an Excel 365 dynamic array formula meaning it spills values to cells below and to the right as far as needed. Here are the values in cell range B4:N8:
Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | |
North | 28 | 14 | 722 | 577 | 427 | 722 | 309 | 875 | 449 | 860 | 976 | 485 |
West | 429 | 976 | 156 | 576 | 688 | 509 | 137 | 433 | 961 | 137 | 293 | 520 |
South | 313 | 811 | 184 | 601 | 356 | 184 | 927 | 829 | 512 | 445 | 467 | 871 |
East | 443 | 888 | 27 | 288 | 981 | 100 | 166 | 673 | 590 | 90 | 169 | 926 |
Excel 365 LAMBDA function in cell B13:
For example, the first row (North) contains these numbers: 28, 14, 722, 577, 427, 722, 309, 875, 449, 860, 976, and 485
The number of values are 12 which is an even number, median is therefore the average between 577 and 485. 485 + 577 = 1062 1062/2 = 531
531 is the calculated median for column "North" in the image above in cell P5 which matches our manually calculated value.
The formula in cell B13 returns the rows in sorted order from small to large based on their median numbers. Row "North" is at the top since the median value is the smallest in the group of rows.
This type of calculations using formulas were much harder in earlier Excel versions than Excel 365, the formula I created above is small and easy to understand.
Explaining formula
The Evaluate Formula tool is located on the Formulas tab in the Ribbon. It is a useful feature that allows you to step through and evaluate complex formulas to understand how the calculation is being performed and identify any errors or issues. The following steps shows these detailed evaluations for the formula above.
Step 1 - Calculate the median for variable a
The MEDIAN function calculates the median based on a group of numbers. The median is the middle number of a group of numbers.
Function syntax: MEDIAN(number1, [number2], ...)
MEDIAN(a)
Step 2 - Build the LAMBDA function
The LAMBDA function build custom functions without VBA, macros or javascript.
Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)
LAMBDA(a,MEDIAN(a))
Step 3 - Calculate the median by row
The BYROW function puts values from an array into a LAMBDA function row-wise.
Function syntax: BYROW(array, lambda(array, calculation))
BYROW(C5:N8,LAMBDA(a,MEDIAN(a)))
returns {531; 471; 489.5; 365.5}
Step 4 - Sort rows based on the median values
The SORTBY function sorts a cell range or array based on values in a corresponding range or array.
Function syntax: SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2],…)
SORTBY(B5:N8,BYROW(C5:N8,LAMBDA(a,MEDIAN(a))),1)
becomes
SORTBY(B5:N8,{531; 471; 489.5; 365.5}, 1)
and returns
6. Sort columns based on the median values
The following formula sorts columns based on the median values calculated column-wise from small to large. Row 20 shows the medians values, they are sorted from small to large. Here is the data table displayed in cell range B4:N8 in the image above:
Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | |
North | 28 | 14 | 722 | 577 | 427 | 722 | 309 | 875 | 449 | 860 | 976 | 485 |
West | 429 | 976 | 156 | 576 | 688 | 509 | 137 | 433 | 961 | 137 | 293 | 520 |
South | 313 | 811 | 184 | 601 | 356 | 184 | 927 | 829 | 512 | 445 | 467 | 871 |
East | 443 | 888 | 27 | 288 | 981 | 100 | 166 | 673 | 590 | 90 | 169 | 926 |
The median values calculated column-wise from cell range C5:N8 are: {371, 849.5, 170, 576.5, 557.5, 346.5, 237.5, 751, 551, 291, 380, 695.5} These values are shown in row 10 in the image above.
Excel 365 LAMBDA function in cell C14:
The formula in cell C14 returns the columns in sorted order from small to large based on their median numbers. Column "Mar" is the left-most column since the median value is the smallest in the group of rows, in this example the median value is 170.
Explaining formula
The Evaluate Formula tool is located on the Formulas tab in the Ribbon. It is a useful feature that allows you to step through and evaluate complex formulas to understand how the calculation is being performed and identify any errors or issues. The following steps shows these detailed evaluations for the formula above.
Step 1 - Calculate the median for variable a
The MEDIAN function calculates the median based on a group of numbers. The median is the middle number of a group of numbers.
Function syntax: MEDIAN(number1, [number2], ...)
MEDIAN(a)
Step 2 - Build the LAMBDA function
The LAMBDA function build custom functions without VBA, macros or javascript.
Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)
LAMBDA(a,MEDIAN(a))
Step 3 - Calculate the median by column
The BYCOL function passes all values in a column based on an array to a LAMBDA function, the LAMBDA function calculates new values based on a formula you specify.
Function syntax: BYCOL(array, lambda(array, calculation))
BYCOL(C5:N8,LAMBDA(a,MEDIAN(a)))
returns
{371, 849.5, 170, 576.5, 557.5, 346.5, 237.5, 751, 551, 291, 380, 695.5}
Step 4 - Sort columns based on the median values
The SORTBY function sorts a cell range or array based on values in a corresponding range or array.
Function syntax: SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2],…)
SORTBY(B5:N8,BYROW(C5:N8,LAMBDA(a,MEDIAN(a))),1)
becomes
SORTBY(C4:N8,, 1)
and returns
7. Units contained in a range that overlap another range
This section demonstrates how to calculate overlapping numerical ranges. What is interesting to know is the fact that Excel handles dates as numbers.
This makes it possible to use the methods shown in this section with Excel date and time values. An Excel date is really only an integer (whole number) formatted as a date. For example, 1/1/1900 is 1. 1/1/2000 is 36526.
There are 36525 days between 1/1/1900 and 1/1/2000, you can verify this yourself. Type 1/1/2000 in a cell, select the cell again. Press CTRL + 1 to open the "Format cells" dialog box. Press with left mouse button on "General"
Now we know how Excel handles dates. This makes it possible to use the MEDIAN function to calculate the number of overlapping days between two date ranges.
I have five examples below that demonstrate the formula calculations in great detail, one is added to the end number of both ranges and the reason is that date ranges count the end date contrary to numerical ranges.
For example, numerical range 5 to 10 is 10 - 5 equals 5. However, date range 1/5/2020 to 1/10/2020 contains 6 days. That is the reason the formulas below are adding 1 to the end dates of both date ranges.
Hi Oscar,
I need a formula that gives me the number of days contained in a range that overlaps another range.
I got this from Carol Weideman, thank you! I think it is really simple and clever.
Date range 1: A to B and date range 2: C to D
Formula:
MEDIAN(number1, [number2], ..)
Returns the median, or the number in the middle of the set of given numbers
The MEDIAN function returns the middle number based on a set of given numbers, the formula above uses two MEDIAN functions and each function contains three numbers.
The first MEDIAN function contains the start date, end date + 1 of the first date range and the end date + 1 of the second date range. The second MEDIAN function contains the start date, end date + 1 of the first date range and the start date of the second date range.
Example 1 - No overlapping ranges
This example has two ranges that don't overlap, this demonstrates the calculations behind the formula when it returns 0 (zero) overlapping units.
The first range begins at 10 and ends at 15, the second range begins 5 and ends at 8. The second range is located before the first range, example 5 demonstrates when the second range is located after the first range.
becomes
=MEDIAN(10,15+1,8+1)-MEDIAN(10,15+1,5)
becomes
=MEDIAN(10,16,9)-MEDIAN(10,16,5)
becomes
10 - 10 = 0
0 (zero) units are overlapping.
Example 2 - Partially overlapping ranges
This example has two numerical ranges that partially overlap, see image above. The first range begins at 10 and ends at 15.
The second range begins at 7 and ends at 10, the ranges overlap by one unit. Example 4 shows the same setup except that the second range overlaps the last unit of the first range.
becomes
=MEDIAN(10,15+1,10+1)-MEDIAN(10,15+1,7)
becomes
=MEDIAN(10,16,11)-MEDIAN(10,16,7)
becomes
11 - 10 = 1
1 unit is overlapping.
Example 3 - Overlapping ranges
This example demonstrates when two ranges completely overlap each other, the first range starts at 10 and ends at 15.
The second range starts at 11 and ends at 14, four units overlap in total which the image above shows.
becomes
=MEDIAN(10,15+1,14+1)-MEDIAN(10,15+1,11)
becomes
=MEDIAN(10,16,15)-MEDIAN(10,16,11)
becomes
15 - 11 = 4
4 units are overlapping.
Example 4 - Partially overlapping ranges
This example shows two ranges that partially overlap, the first range starts at 10 and ends at 15. The second range starts at 15 and ends at 18.
Both ranges overlap at 15 and only one unit is overlapping, see image above.
becomes
=MEDIAN(10,15+1,18+1)-MEDIAN(10,15+1,15)
becomes
=MEDIAN(10,16,19)-MEDIAN(10,16,15)
becomes
16 - 15 = 1
1 unit is overlapping
Example 5 - No overlapping ranges
The last example
becomes
=MEDIAN(10,15+1,20+1)-MEDIAN(10,15+1,17)
becomes
=MEDIAN(10,16,21)-MEDIAN(10,16,17)
becomes
16 - 16 = 0
0 units are overlapping.
Can these calculations be done with multiple date ranges?
Yes, they can. However, the MEDIAN function is not suitable for array calculations so the formulas are somewhat more complicated.
I have written an article that demonstrates a formula that tells you if a date range is overlapping any other date range specified in a list. It returns True if it is overlapping and False if not.
What if you want to know exactly which date range overlaps?
Check out this article: Identify rows of overlapping records The formula returns the row number or rows of all date ranges that overlap.
Can I use the MEDIAN function to calculate time ranges?
Yes, you can. I have written this article that shows the formula calculations in detail:
How to calculate overlapping time ranges
'MEDIAN' function examples
Question: I need to calculate how many hours a machine is utilized in a company with a night and day […]
Functions in 'Statistical' category
The MEDIAN function function is one of 73 functions in the 'Statistical' category.
Excel function categories
Excel categories
One Response to “How to use the MEDIAN function”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
Great article, Oscar! I really appreciated how you explained the difference between using the median and the mean, and how outliers can affect the latter. It was interesting to see in your examples how the mean can be distorted by a few very high incomes or an outlier, whereas the median gives a better representation of the typical value in a dataset. Keep up the good work!