Distributing students across classes based on marks - excel

Name
Marks
Rank
Class
Eddie
20
6
C
Tom
10
10
A
Jenny
30
4
A
Riva
40
3
C
Andy
50
2
B
Mark
5
11
B
Sally
78
1
A
Jack
15
8
B
Dick
15
8
C
Harry
20
6
A
Dom
30
4
B
The students are expected to be distributed across classes A, B and C, based on their marks in the above picture.
The student with the highest marks goes in A. The one with the next highest goes in B. The next highest goes in C. The next goes again to A and so on.
What should be the formula to be used in Excel 2013 and above for calculating the Class?

Sort the table by either Marks descending or Rank ascending.
D2: =CHOOSE(MOD(ROWS($1:1)-1,3)+1,"A","B","C")

If you are using Excel 365, you can use the SORTBY function to solve the question.
Assume the Name column is in a named range called List_Name, the Marks column is in a named range called List_Marks, your example dataset is in range A1:D12, and you want to return the class code in column D.
In cell D2, enter any one of the following formulas and drag it down:
=CHOOSE(MOD(MATCH(A2,SORTBY(List_Name,List_Marks,-1),0),3)+1,"C","A","B")
Alternatively, you can use the following in cell D2 instead:
=INDEX({"C";"A";"B"},MOD(MATCH(A2,SORTBY(List_Name,List_Marks,-1),0),3)+1)
If you cannot use the SORTBY function, then the answer provided by Ron Rosenfeld should do the job quite well.
Let me know if you have any questions.

Assuming that the chart you provided is in cells A1-D11
Try making a 2x3 chart on the side (I’m using F2-G4 with 1...A 2...B 0...C
and then put the formula in D1 as follows: =vlookup(mod(C2,3),F2:G4, false)
You could even skip out the whole C column if you wanted, writing =vlookup(mod(rank(A2,B:B),3),F2:G4, false)
But then you might have an issue of 2 people going to the same class if they rank the same.

Related

Counting unique values based on partial text

I've got many items in boxes on a pallet. I would like to know how I can count the unique number of boxes in a pallet.
A
B
C
D
E
1
Item
Pallet-box ID
Pallet No.
No. of boxes
2
abc
P01-B01
P01
5
3
def
P01-B01
P02
2
4
ghi
P01-B02
5
jkl
P01-B02
6
mno
P01-B02
7
pqr
P01-B03
8
stu
P01-B03
9
vwx
P01-B04
10
yz
P01-B05
11
123
P02-B01
12
456
P02-B02
12
789
P02-B02
So, based on the above example, the above pallet (P01) has 5 unique boxes (B01-B05) and pallet (P02) has 2 unique boxes (B01-B02). What kind of formula should I use to achieve the result of 5 for P01 and 2 for P02 in column E? I was thinking of using COUNTIF but it doesn't seem to be applicable here. Any advice/help is greatly appreciated.
If you have Excel 365 you can use the following formulas.
I added some helper columns to achieve the result.
=UNIQUE(tblData[Pallet-box ID]) retrieves the unique Pallet boxes (column E)
Based on that =UNIQUE(LEFT(UNIQUE(tblData[Pallet-box ID]),3)) retrieves the unique pallet no. (column G)
And now we can count the pallet no within the pallet boxes: =COUNTIF(E4#,G4# & "*") (column H)
I'm not a specialist myself, but I have just created following example:
A B C D E
7 a
8 a
9 a
10 b
11 b
In a cell, I created the formula =UNIQUE(E7:E11) and next to it the formula =COUNTIF(E7:E11,UNIQUE(E7:E11)), these are the results:
a 3
b 2
So, combining COUNTIF() and UNIQUE() basic Excel functions might help you.
Edit: you might use a helper column, based on =LEFT(B1,LEN(A1)): I had put P01 in cell "A1" and P02-B01 in cell "B1" and the result was P02, the left side of P02-B01, based on the length of P01.
Well, if you are using either Excel 2021 or O365, then you may try in this way as well,
Unique Pallet No.
• Formula used in cell D3
=UNIQUE(LEFT(Pallet_box_ID,3))
No. Of Boxes
• Formula used in cell E3
=SUMPRODUCT(--(LEFT(UNIQUE(Pallet_box_ID),3)=D3))
Where Pallet_box_ID refers to the range
=$B$3:$B$14
Created using defined name manager --> select the range from B2:B14 & Press CTRL + SHIFT + F3 --> Check Top Row from Create Names From Selection Dialog Box and Press Ok, now you can refer the Name Box showing the range B3:B14 as Pallet_box_ID
Also I will suggest you to use this way because it may happen the Pallet Box ID can be PO10 or PO100 then LEFT(Pallet_box_ID,3) shall not give you proper output as shown in image below
• Formula used in cell D3
=UNIQUE(LEFT(Pallet_box_ID,FIND("-",Pallet_box_ID)-1))
• Formula used in cell E3
=SUMPRODUCT(--(LEFT(UNIQUE(Pallet_box_ID),FIND("-",UNIQUE(Pallet_box_ID))-1)=D3))

Sum values using lookup table, where lookup values are a list of values with comma delimiter

I am trying to sum values based that equal a lookup value. However, that value is actually a list of values delimited by a comma. Below is an example of what I mean.
Suppose I have raw data in the form of sheet1 below:
Sheet1:
A
B
1
ID
VALUE
2
A
30
3
A
50
4
A
20
5
B
10
6
B
20
7
C
70
8
C
40
9
D
30
10
E
50
11
F
20
12
F
30
13
G
10
And I have a look table that groups all IDs by their respective teams, as per sheet2 below.
Sheet2:
A
B
1
TEAM
IDS
2
Red
A, B
3
Blue
C, D
4
Green
E, F, G
And I want to create a report where the user can select the team name, and the sum of the values in sheet1 will aggregate based on the selection, as per the following example. So the user would select "Green" in cell B1 and it would return the sum of values that correspond to E, F, and G in sheet1.
Report:
A
B
1
Select Team:
Green
2
Sum:
110
I have searched all over for a solution to this and was able to find something similar. I tried to repurpose the formula for my data but couldn't get it to work because I think that solution dealt with numbers rather than text.
Excel: Perform a SUMIF where the criteria is a comma-delimited list
Any suggestions would be greatly appreciated!
Edit: Just want to add that I realize I could first parse out the IDs in sheet2, however I'm looking for a formula that can bypass that as my real dataset is quite large and parsing out the IDs under each team would explode the number of rows.
A variation of #JvdV solution on the linked question:
=SUMPRODUCT(SUMIFS(Sheet1!B:B,Sheet1!A:A,FILTERXML("<t><s>"&SUBSTITUTE(VLOOKUP(Sheet2!F1,Sheet2!A:B,2,FALSE),",","</s><s>")&"</s></t>","//s")))
Note, this only works with Excel 2013 or later and only on PC. FILTERXML is not available on Mac or prior to 2013.
If Mac or prior to 2013:
=SUMPRODUCT(SUMIFS(Sheet1!B:B,Sheet1!A:A,TRIM(MID(SUBSTITUTE(VLOOKUP(F1,Sheet2!A:B,2,FALSE),",",REPT(" ",999)),(ROW($ZY1:INDEX($ZY:$ZY,LEN(VLOOKUP(F1,Sheet2!A:B,2,FALSE))-LEN(SUBSTITUTE(VLOOKUP(F1,Sheet2!A:B,2,FALSE),",",""))+1))-1)*999+1,999))))

How to SELECT N values ABOVE and BELOW from specific value

If I have a table:
Column A
Column B
Column C
1
Jane
10
2
Stewe
9
3
John
8
4
Mike
7
5
Luke
6
6
Andrew
5
7
Carl
4
8
Sasha
3
9
Ariel
2
10
Carol
1
I would like to SELECT 3 rows above and below WHERE Column B = someValue .
IF query SELECT * WHERE Column B = "Andrew" result should look like:
Column A
Column B
Column C
3
John
8
4
Mike
7
5
Luke
6
6
Andrew
5
7
Carl
4
8
Sasha
3
9
Ariel
2
I know how to select one row, but cant understand how to select such range.
Thanks for ideas!
You can limit and offset inside your QUERY():
=QUERY(A1:C,"limit "&2+MIN(5,MATCH(D1,B:B,0))&" offset "&MAX(0,MATCH(D1,B:B,0)-5))
Well, this was fun...
If 3 above or below are not available then blank... rolling data around is a different proposition.
Below the image is the list of formulae used.
So, per cell not including the data validation that is based on cells B2:B11
A14 and dragged down:
=IFERROR(INDEX($A$2:$A$11,MATCH(B14,$B$2:$B$11,0)),"")
C14 and dragged down:
=IFERROR(INDEX($C$2:$C$11,MATCH(B14,$B$2:$B$11,0)),"")
Cells B14 through B20:
=IFERROR(IF(MATCH(B$17,$B$2:$B$11,0)=3,NA(),INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)-3)),"")
=IFERROR(IF(MATCH(B$17,$B$2:$B$11,0)=2,NA(),INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)-2)),"")
=IFERROR(IF(MATCH(B$17,$B$2:$B$11,0)=1,NA(),INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)-1)),"")
=E2
=IFERROR(INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)+1),"")
=IFERROR(INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)+2),"")
=IFERROR(INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)+3),"")
In Excel 365, you could try:
=INDEX(A:C,MAX(2,MATCH(D2,B:B,0)-3),0):INDEX(A:C,MIN(COUNTA(B:B),MATCH(D2,B:B,0)+3),0)
In Google sheets, on the other hand, the formula would be:
=INDEX(A:C,MAX(2,MATCH(D2,B:B,0)-3),0):INDEX(A:C,MIN(COUNTA(B:B),MATCH(D2,B:B,0)+3),0)
(spot the difference).
Excel
Google Sheets
This should produce what you want in all cases:
=IFERROR(FILTER(A2:C,B2:B<>"",ROW(A2:A)>=VLOOKUP("Andrew",{B2:B,ROW(B2:B)},2,FALSE)-3,ROW(A2:A)<=VLOOKUP("Andrew",{B2:B,ROW(B2:B)},2,FALSE)+3))
Of course, you can replace the two instances of "Andrew" with a cell reference (one where you type a changeable name).
This just looks up the row in a curly-bracket array formed from the names and row numbers and uses FILTER to keep results to rows between +/-3 rows of where the target name is found. If you choose the first name (or any other name), you won't get an error; because even if the target name were on Row 1 and the formula goes looking for anything "greater than or equal to 1 minus 3, all rows will be greater than a negative number. Same on the high end. You just won't get a full seven names if there aren't at least three other rows prior to or after the target row.
this not the best solution but it will work , you can use a helper column 'D' that contains the following formula =if(countif(INDIRECT("B"&ROW()+3&":"&"B"&ROW()-3),"Andrew")>0,TRUE,FASLE)
and u can query from here like this SELECT * WHERE Column D = TRUE

Assign one value at the time from a column i Excel if the value is the same

I got some help with a problem finding column values in Excel: Find column value Excel
The table looks like this:
A 17 8,5 5,666666667 4,25 3,4 2,833333333 2,428571429
B 5 2,5 1,666666667 1,25 1 0,833333333 0,714285714
C 5 2,5 1,666666667 1,25 1 0,833333333 0,714285714
G 4 2 1,333333333 1 0,8 0,666666667 0,571428571
The code that I got help with basically selects A, B, C and G based of the order of the largest numeric value. The largest value is 17, that is one point for A, then comes 8,5 which gives another point for A. That works fine.
However, then comes 5. I now get B two times instead of first B and the C (the order does not matter, but I do not want to get two points for B when B and C have the same value). How can this be fixed?
(The real problem that this example illustrates is the number of seats on local boards in Swedish multiplicitets, where the number of seats for the different parties is based on the number of seats in the city council.)
Edit: Formula: =INDEX($J$16:$J$25,AGGREGATE(14,6,ROW($K$16:$W$25)/(K28=$K$16:$W$25),1)-ROW($J$16)+1)
Edit2: Wanted result:
Mandate Party
1 A
2 A
3 A
4 B
5 C
To do this by formulas, I think a simple way is to
unpivot the original data using formulas
Apply a unique rank formula to that data
Then use VLOOKUP to return ranks 1-n
The following formulas and defined names will be useful
origTbl =Sheet2!$A$1:$H$4
unpivotTbl =Sheet2!$A$8:$C$35
A8 =RANK(C8,$C$8:$C$35,0)+COUNTIF($C$8:C8,C8)-1
B8 =INDEX(origTbl,INT((ROWS($1:1)-1)/7)+1,1)
C8 =INDEX(origTbl,INT((ROWS($1:1)-1)/7)+1,MOD(7+ROWS($1:1)-1,7)+2)
Fill down A8:C8 to row 35
In the formulas in B8 and C8, the 7 is the number of columns in origTbl minus 1 (eg: COLUMNS(origTbl)-1)
Any changes you make in origTbl will be reflected in the final table
For appearances, you can certainly hide the UnPivot table someplace.
Note: in my locale, decimal separator is the dot

SUMIF for first 5 cells meeting criteria

Simple Excel Table such as
A B
1 John 5
2 John 7
3 John 9
4 Jill 25
5 John 21
6 John 22
7 Jill 50
8 John 100
9 John 2000
10 Jack 4
Using SUMIF, we can return the total assigned to John.
=SUMIF(A:A,"John",B:B)
Is there a way to return only the first 5 values that match the criteria? Or is there a way to return the 5 smallest values for John? Either would work.
Oh well. I'll go ahead and presume that you have Excel 2010 or later.
With e.g. "John" in D1, enter this formula in E1:
=SUMIFS($B$1:$B$10,$A$1:$A$10,D1,$B$1:$B$10,"<="&AGGREGATE(15,6,$B$1:$B$10/($A$1:$A$10=D1),5))
Copy down to give similar results for names in D2, D3, etc.
Regards
Formula:
=IF(COUNTIF($A$1:A1,A1)<=5,SUMIF($A$1:A1,A1,$B$1:B1),"")
The last value shown for each person will be the sum of the first (up to)5 values for that person. Just copy and paste values then sort.
Your sample data would show the same result for either the first 5 or lowest 5 as John's numbers are in ascending order. If that is not always the case or if you need to provide compatibility to versions of Excel earlier than 2010 I would offer the following. Note that in my sample image, I've resorted the numerical values in descending order to illustrate the difference.
For John's first 5 values (E2 in the sample image):
=SUM(INDEX(($B$2:$B$11)*($A$2:$A$11=D2)*(ROW($1:$10)<=SMALL(INDEX(ROW($1:$10)+($A$2:$A$11<>D2)*1E+99,,), 5)),,))
For John's lowest 5 values (F2 in the sample image):
=SUMPRODUCT(SMALL(INDEX(($B$2:$B$11)+($A$2:$A$11<>D2)*1E+99,,),ROW($1:$5)))
These are standard formulas. Any array processing is supplied by INDEX and/or SUMPRODUCT. Ctrl+Shift+Enter is not required. Some form of error control may be necessary when there are less than 5 matching values; a simple IF(COUNTIF(), <formula>) would suffice. When transcribing these type of formulas it is important to note that ROW(1:10) is the position within B2:B11 or A2:A11, not the actual row on the worksheet.
 
                  
In C1 enter:
=IF(A1="John",1,0)
In C2 enter:
=IF(A2="John",1+MAX($C$1:C1),0)
and copy down. Then use:
=SUMPRODUCT((A:A="John")*(B:B)*(C:C<6))
.
Assuming John in D1 you can get the sum of the 5 smallest values for John with this array formula
=SUM(SMALL(IF(A$1:A$100=D1,B$1:B$100),{1,2,3,4,5}))
confirm with CTRL+SHIFT+ENTER and copy down for to work for all names in the list

Resources