Calculating median using two columns - excel

I have the following columns
12 A
11 M
12 B
12 C
11 A
9 M
13 N
11 M
12 C
11 B
15 M
I want to calculate median based on only Ms. I have done by selecting ranges.
I want to use columns rather than ranges. Is there any solution? Thanks for your help

If you have 2010 or later use this formula:
=AGGREGATE(17,6,(A:A/(B:B="M")),2)
If you have 2007 or earlier, try this array formula:
=MEDIAN(IF(B:B="M",A:A))
This is an array formula and must be confirmed with Ctrl-Shift-Enter.
Though the use of full column references will slow the calculation times because it will iterate through all the rows, all 1.04 million. It is good practice to limit the references to the max data range to help offset this drain. Change the C:C to something like $C$1:$C$10000 then it will loop only 10,000 times.

Well as a VBA user I love to use VBA, other can use your skill using formulas, and is completly right if you are good on formulas. Here is my answer and I hope help you.
Imagine you have this:
+----+------------+--------------------+---------------+
| 1 | Values (A) | Criteria Range (B) | Criteria ( C) |
+----+------------+--------------------+---------------+
| 2 | 14 | B | A |
| 3 | 1 | M | |
| 4 | 15 | A | |
| 5 | 15 | E | |
| 6 | 10 | A | |
| 7 | 3 | M | |
| 8 | 11 | A | |
| 9 | 8 | M | |
| 10 | 14 | A | |
| 11 | 5 | M | |
| 12 | 9 | M | |
| 13 | 10 | M | |
| 14 | 11 | N | |
| 15 | 9 | A | |
| 16 | 2 | M | |
| 17 | 15 | M | |
| 18 | 11 | A | |
| 19 | 12 | S | |
| 20 | 9 | M | |
| 21 | 11 | A | |
| 22 | 15 | V | |
+----+------------+--------------------+---------------+
And inside the cell E2 you have this =MedianIf($A$2:$A$22,$B$2:$B$22,C2) and the result must be 11
Because in a regular module of VBA you put this function.
Function MEDIANIF(RangeIf As Range, Criteria_Range1 As Range, Criteria1 As String) As Double
Dim i
Dim Counter
Dim tmp()
Dim subCouter
subCouter = 0
For Each i In Criteria_Range1
Counter = Counter + 1
If i.Value = Criteria1 Then
subCouter = subCouter + 1
ReDim Preserve tmp(1 To subCouter)
tmp(subCouter) = RangeIf(Counter)
End If
Next i
Dim a
MEDIANIF = Application.WorksheetFunction.Median(tmp())
End Function
And the function works like this:
RangeIF = is the range with the values that you want to get the MEDIAN
Criteria_Range1 = Is the range where you have all the criteria, is the same size as RangeIF
Criteria1 = Is the criteria you use to filter the data.

How about this low-tech solution:
In Column C, formulate: =IF(B2="M",A2,"") (in rows 2-12, based on your data, and assuming row 1 contains headers)
In cell D2 (or anywhere else), formulate: =MEDIAN(C:C)

Related

Count adyacent non-blank cells in Excel/Google Sheets

I have an Excel/Google Sheets table in which some rows contain blank cells in between non-blank cells. I would like to count from left to right. I have used the formula COUNTA(A2:F2) but it cannot achieve what I want. This is a sample of the outcome I would like to get, with the Personalized count that I am seeking to achieve and the classic COUNTA:
I think that the task is slightly different from what you describe in the question, you want to count until blank and not between not blank cells.
So I made this working example
+---+------+------+------+------+------+------+-------+
| | A | B | C | D | E | F | G |
+---+------+------+------+------+------+------+-------+
| 1 | 2021 | 2020 | 2019 | 2018 | 2017 | 2016 | Count |
| 2 | 1 | 1 | 1 | 1 | 1 | 1 | 6 |
| 3 | 1 | 1 | 1 | 1 | | | 4 |
| 4 | 1 | | 1 | 1 | 1 | 1 | 1 |
| 5 | 1 | 1 | 1 | | | 1 | 3 |
+---+------+------+------+------+------+------+-------+
where cell G2 contains the following:
=IFERROR(MATCH(1;--(A2:F2="");0)-1;COUNTA(A2:F2))
is it right for you?

Looking up values in third column by two criteria

I have a list similar to this one:
NO | Cat1 | Cat2 | | Crit1 | Crit2 |
---|------|------| | A | O |
5 | A | O |
3 | K | Y |
6 | K | Y |
7 | F | K |
8 | A | O |
9 | J | H |
10 | K | Y |
5 | F | T |
50 | A | O |
8 | L | E |
1 | R | D |
Based on two criteria I want a dynamic list which changes everytime the content are changed or the criteria are changed.
If criteria is A O then the list should be as below,
|List|
|----|
| 5 |
| 8 |
| 50 |
If any other criteria is selected the list will be longer or shorter and if nothing is present it is shown as a blank cell.
I have tried some MATCH and INDEX formulas but I cannot make it work correctly.
=IFERROR(INDEX(LookUpList;MATCH(0;COUNTIF(NewList;LookUpList)+IF(Cat1<>Crit1;1;0)+IF(Cat2<>Crit2;1;0);0));"")
Sorted ascending:
=IFERROR(AGGREGATE(15,7,A$2:A$12/((B$2:B$12=G$1)*(C$2:C$12=G$2)),ROW(1:1)), "")
Ordered by row:
=IFERROR(INDEX(A:A, AGGREGATE(15, 7, ROW(A:A)/((B$1:B$12=G$1)*(C$1:C$12=G$2)), ROW(1:1))), "")
Pick one formula then fill down for subsequent matches.

Excel: Give scores based on range, where max = 1 and min = 10

I have following problem:
I want to give scores to a range of numbers from 1-10 for example:
| | A | B |
|---|------|----|
| 1 | 1209 | 1 |
| 2 | 401 | 7 |
| 3 | 123 | 9 |
| 4 | 49 | 10 |
| 5 | 30 | 10 |
(Not sure if B is 100% correct but roughly)
I got the B values with
=ABS(CEILING(A1;MAX($A$1:$A$32)/10)*10/MAX($A$1:$A$32)-11)
It seems to work but if I for example take numbers like
| | A | B |
|---|------|----|
| 1 | 100 | 1 |
| 2 | 90 | 2 |
| 3 | 80 | 3 |
| 4 | 70 | 4 |
| 5 | 50 | 6 |
But I want 50 to be 10.
I would like to have it scalable so I can do it with a 1-10 or 1-100 or 5-27 or whatever scale and with however many numbers in the list and whatever numbers to score from.
Thanks!
Use this formula:
=$E$1 + ROUND((MIN($A:$A)-A1)/((MAX($A:$A)-MIN($A:$A))/($E$1-$E$2)),0)
It is scalable. You put the max and min in E1 and E2.

excel formula series fill in custom order

i have two table of excel
1st/
1 | blawa1 |blawb1 |blawc1 |blawd1 |
2 | blawa2 |blawb2 |blawc2 |blawd2 |
3 | blawa3 |blawb3 |blawc3 |blawd3 |
...
2nd/ that should be linked to 1st table in the folowing form
1 | blawa1 |
2 | blawb1 |
3 | blawc1 |
4 | blawd1 |
5 | |
6 | blawa2 |
7 | blawb2 |
8 | blawc2 |
9 | blawd2 |
10 | |
11 | blawa3 |
12 | blawb3 |
13 | blawc3 |
14 | blawd3 |
15 | |
16 | blawa4 |
17 | blawb4 |
18 | blawc4 |
19 | blawd4 |
20 | |
...
when i write the formula in table 2 and drag it down, the formula did not fill in series that i want.
e.g: it jump from "blawa1" to "blawa6" , while it should be "blawa2"
With data in columns A through D, pick some cell, say F4, and enter:
=OFFSET($A$1,ROUNDUP(ROWS($1:1)/5,0)-1,MOD(ROWS($1:1)-1,5))
and copy down:
Using similar formulas, any two dimensional table can be mapped into a single column or a single row.
If you see zeros in the output single column rather than the spaces, fill column E with blanks.
You can use the below formula
=IF(INDIRECT(CHAR(64+MOD(ROW()+4,5)+1) & (INT((ROW()-1)/5)+1))=0,"",INDIRECT(CHAR(64+MOD(ROW()+4,5)+1) & (INT((ROW()-1)/5)+1)))
copy the this cell to required number of times.

Excel Creating a List from Beginning and End number AND tags

I am trying to create a list from an index of grouped values.
This is very similar to this, however my groups also have "tags" on then that complicate the listings.
Here is an example of my INDEX tab:
| A | B | C | D |
-------------------------
1 | 1 | 1 | 1 | CV |
2 | 1 | 2 | 2 | IS |
3 | 1 | 3 | 3 | IS |
4 | 2 | 4 | 5 | GN |
5 | 2 | 6 | 7 | PS |
6 | 4 | 8 | 11 | SQ |
7 | 2 | 12 | 13 | SS |
8 | 1 | 14 | 14 | AT |
9 | 15 | 15 | 29 | AT |
10| 4 | 30 | 33 | TYP |
Where A is the number of pages, B is the first page, C is the last page and D is the tag. I would also like to add columns such that I can keep a running tally of the tags.
| A | B | C | D | E | F |
---------------------------------------
1 | 1 | 1 | 1 | CV | CV1 | CV1 |
2 | 1 | 2 | 2 | IS | IS1 | IS1 |
3 | 1 | 3 | 3 | IS | IS2 | IS2 |
4 | 2 | 4 | 5 | GN | GN1 | GN2 |
5 | 2 | 6 | 7 | PS | PS1 | PS2 |
6 | 4 | 8 | 11 | SQ | SQ1 | SQ4 |
7 | 2 | 12 | 13 | SS | SS1 | SS2 |
8 | 1 | 14 | 14 | AT | AT1 | AT1 |
9 | 15 | 15 | 29 | AT | AT2 | AT16 |
10| 4 | 30 | 33 | TYP | TYP1 | TYP4 |
Note that the tag could occur multiple times and it may not be in sequential rows.
Here is what I want this to look like for my LIST tab:
| A |
---------
1 | CV1 |
2 | IS1 |
3 | IS2 |
4 | GN1 |
5 | GN2 |
6 | PS1 |
7 | PS2 |
8 | SQ1 |
9 | SQ2 |
10| SQ3 |
11| SQ4 |
and so on...
How do I add the additional columns to the INDEX tab via formulas?
How do I create the LIST via formulas? (...is this even possible?)
The formulas should be pretty simple to write. Just consider what you're trying to accomplish.
Your first formula (in column E) is just taking a running count of the tags (in column D). So you want to count all cells from the first tag up to the corresponding tag where the tag names are the same. That count is to be appended to the tag name.
=$D1 & COUNTIF($D$1:$D1, $D1)
The second formula (in column F) is just taking a running sum of the page counts (in column A). So you want to take the sum of all corresponding page counts from the first tag up to the corresponding tag where the tag names are the same. The sum is to be appended to the tag name.
=$D1 & SUMIF($D$1:$D1, $D1, $A$1:$A1)
Note that the column doesn't change nor does the starting rows of the ranges (hence the need to use absolute ranges). The only thing that changes are the rows of the tag and the row of the end range.
I don't think it would be possible to generate that list through simple formulas. As far as I know, formulas need to have a 1-to-1 correspondence with another range. A single range can yield multiple values so a formula just won't cut it. You'll need to write a VBA script to generate that.
Sub GenerateList()
Dim usedRange As Range
Dim count As Dictionary
Set usedRange = Worksheets("Index").usedRange
Set count = CountValues(usedRange)
Dim output As Range
Dim row As Integer
Dim key As Variant
Set output = Worksheets("List").Columns("A").Rows
output.ClearContents
row = 1
For Each key In count.Keys()
Dim i As Integer
For i = 1 To count(key)
output(row) = key & i
row = row + 1
Next i
Next key
End Sub
Function CountValues( _
usedRange As Range, _
Optional tagsColumn As String = "D", _
Optional valuesColumn As String = "A") As Dictionary
Dim tags As Range
Dim values As Range
Set tags = usedRange.Columns(tagsColumn).Rows
Set values = usedRange.Columns(valuesColumn).Rows
Dim map As New Dictionary
Dim tag As Range
For Each tag In tags
map(tag.Value) = map(tag.Value) + values(tag.row)
Next tag
Set CountValues = map
End Function
This uses a Dictionary so you'll have to reference the scripting runtime.
It sounds like you're just trying to get a list of "Unique Values" on a separate sheet that you can use as your list. Try these pages, there are multiple VBA methods to paste unique items in a range.
Also, Advanced Filter has an option to paste unique values to another location. So none of your repeat tags would appear in this list, only unique ones for your "LIST" tab.
Anyway, not sure if that's what you're wanting, but the question was a smidge vague.
Links here:
Create Unique list
Create Unique list 2

Resources