Excel Calculate Average and Median across multiple worksheets and columns - excel

Looking for some Excel help. I have the following worksheets:
WORKSHEET WEEK1
A-------|B------|C------|D------
NAME |GAME A |GAME B |GAME C
JIM 10 15 12
BILL 12 10 5
TED
PHIL 0 8 17
WORKSHEET WEEK2
A-------|B------|C------|D------
NAME |GAME D |GAME F |GAME B
JIM 15 10 9
BILL
TED 3 21 18
PHIL 12 2 30
Basically, I'll have 10 worksheets (one per week). We are tracking people's pinball scores over a series of weeks. The games/scores will NOT be in the same order on the sheet week after week (20 machines, only six played per week).
I need to calculate Average and Median for the machine across multiple worksheets, AND eliminate any missing or zeroed scores (so the average of 10,0,10 is 10, same for median, same for data missing/null).
I used this for the first week
=MEDIAN(IF('1B'!L:L>0,'1B'!L:L))
And seemed to be just ducky, but am struggling getting this to be cross sheet in varied columns excluding blanks.
I'm not opposed to having another sheet that looks through all of the worksheets and columns to find a match on the game title and then grab that column, calculate, done. But if I have to do this manual-ish, that's totally not a problem.
I'm a C# developer, so I get code, I'm just dreadfully bad at Excel and the syntax makes me the tiniest bit sad.
UPDATE:
The expected output is as follows:
GAME A: Average: X, Median Y
GAME B: Average: Z, Median N
Thanks much!

Assuming that:
1) You have Excel 2007 or later
2) The data in each of the sheets to be queried occupies the range A1:D5 (with headers in row 1 and names in column A)
3) You put your choice of game (e.g. "GAME B") for which you wish to obtain the desired results in cell A1 of a sheet other than those being queried
First go to Name Manager (Formulas tab) and make the following definition:
Name: SheetList
Refers to: ={"Sheet1","Sheet2","Sheet3"}
(Or whatever happen to be the relevant sheet names in question.)
The required array formula** for the average is then:
=AVERAGE(IFERROR(1/(1/N(INDIRECT(SheetList&TEXT(ROW(B2:D5)*10^5+TRANSPOSE(MODE.MULT(IF(T(INDIRECT(TRANSPOSE(SheetList)&TEXT(10^5+COLUMN(B2:D5),"!R0C00000"),0))=A1,COLUMN(B2:D5)),COLUMN(B2:D5))),"!R0C00000"),0))),""))
Replace AVERAGE with MEDIAN in the above to obtain the equivalent median for that game.
Regards
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

Related

Distributing students across classes based on marks

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.

Excel function to calculate pairwise date difference

I have a excel sheet with all employees of my company. This list also includes the sick days of each employee. I want to calculate the sick days of each year.
My excel sheet looks like this (shortened version):
A B C D E F G ...
1 Name From To From To From To ...
2 Max 2015/06/15 2015/06/16 2016/08/17 2016/08/17
3 Sarah 2016/01/20 2016/01/20
4 Phil
...
Explanation: Max was sick for two times. First from 2015/06/15 to 2015/06/16 and second from 2016/08/17 to 2016/08/17. Every time he gets sick the table is expanded to the right. Sarah was sick only on 2016/01/20 and Phil wasn't sick.
Now, I want to calculate the sick days of a year.
I could do something like
A B
10 2015 Sum of sick days in 2015
11 2016 Sum of sick days in 2016
So what needs to be done is:
check if date is in the right year
pairwise calculation of networkdays of all dates in a row, that means
=SUM(NETWORKDAYS(B2;C2)) for 2015 and
=SUM(NETWORKDAYS(D2;E2);NETWORKDAYS(B3;C3)) for 2016.
But it should work more dynamically. I only want to choose the matrix, e.g. A1 to I8 for each year and the rest should be calculated automatically. Does anybody know how I can do that?
You can use the below formula. (paste this in the cell B10
=SUMPRODUCT((YEAR($B$2:$N$8)=$A10)*(MOD(COLUMN($B$2:$N$8),2)=0)*(($C$2:$O$8-$B$2:$N$8)+1))
Copy the above cell into B11. It should work.
The following seems quite unruly:
=IF(YEAR(C2)=A10,NETWORKDAYS(B2,C2))+IF(YEAR(G2)=A10,NETWORKDAYS(F2,G2))+IF(YEAR(K2)=A10,NETWORKDAYS(J2,K2))+IF(YEAR(C3)=A10,NETWORKDAYS(B3,C3))+IF(YEAR(G3)=A10,NETWORKDAYS(F3,G3))+IF(YEAR(K3)=A10,NETWORKDAYS(J3,K3))+IF(YEAR(C4)=A10,NETWORKDAYS(B4,C4))+IF(YEAR(G4)=A10,NETWORKDAYS(F4,G4))+IF(YEAR(K4)=A10,NETWORKDAYS(J4,K4))
Alternatively you could either write a macro to loop through the data or add an additional two columns in between each From and To column. D2 would be
=IF(ISBLANK(C2),"",YEAR(C2))
and copy down. E2 would be
=IF(ISBLANK(C2),"",NETWORKDAYS(B2,C2))
and copy down. E11 would be
=SUMIF(D2:D4,$A11,E2:E4)
and copy across to I11 and M11 etc. You can then just
=SUM(C11:M11)
These new columns can be hidden if you needed.

Transfer data from one worksheet to the next based on cell values

I had trouble titling this, and I think it is better explained with examples. I am not an extremely experienced excel user, but was asked to figure this out.
Worksheet 1 (delivered by software) is formatted like this:
12/17/2013
Hour Delivered
00.00-00.59
Employee 1 18
Employee 2 17
Total For Hour 35
01.00-01.59
Employee 1 18
Employee 2 17
Employee 3 12
Total For Hour 47
... etc until hours 24.00-24.59
The number of employees in the group per hour is different each day, so i don't think that I can just simply reference the cells.
The worksheets that I want to transfer the data from worksheet 1 to are based on date, so there is one for each day. (12/17 worksheet, 12/18 worksheet, etc...)
And this is the format of the date worksheets:
Employee 00.00-00.59 | 01.00-01.59 | etc. until hours 24.00-24.59
Employee 1 18 18
Employee 2 17 17
Employee 3 12
Employee 4
Employee 5
So basically I need the data from worksheet 1 transferred over to the individual date worksheets. I believe, the amount of employees being different for each hour/day makes this difficult. Does anyone here have any ideas of how this can be accomplished?
Also, if there are any questions, please let me know.
This should be possible without any VB in two steps. First step is to add further columns to Worksheet 1 that normalise the data. Second step is to create a pivot table using that normalised data.
By "normalise" I mean add columns in Worksheet 1 for Date, Hour, Employee and Delivered using formulae that copy values from your existing columns A and B. Let me know if you need more help with that.
Edit: adding details ...
Suppose Worksheet 1 has the values you indicated in column A and B, and that you want Hour in column D. Suppose row 1 just contains column headings. Leave row 2 totally empty. The formula in col D needs to say "If the value in col A looks like an hour, then copy it, otherwise repeat the hour from the line above." A simple way to determine if a row in Worksheet 1 is an Hour is to look for a decimal point in position 3. So put =IF(MID(A3,3,1)=".",A3,D2) in cell D3 and copy that formula down.
I'm sure you can construct a similar formula for the Date, Employee and Delivered columns.
Maybe add a condition to the formulae to say "If the value in col A starts with 'Total' then leave the cell empty".
If there aren't a lot of data and the setup is exactly how you've displayed them above, a simple formula can solve this.
Assuming that my raw export is in sheet Base, cell A1 and my intended output starts in sheet Output, cell A1, you can use this formula on cell B2:
=IF(INDEX(Base!$A:$A,MATCH(B$1,Base!$A:$A,0)+ROW(1:1),0)=$A2,OFFSET(INDEX(Base!$A:$A,MATCH(B$1,Base!$A:$A,0)+ROW(1:1),0),0,1),0)
The premise is simple. It locates the correct hour using INDEX + MATCH, then OFFSETs it by the correct number of ROWs. This is assuming, of course, that your employees are in the same location under every hour (ie Employee 1 is always one (1) row below the hour, Employee 2 is always two (2) rows below, etc). We also add a check if the employee name matches so that it will return 0 if the employee is not there.
Here are some screencaps:
Sample data:
Output:
Of course, this is just the basic premise. If you have multiple dates in a sheet, it's just a matter of manipulating this formula further. Off the top of my head, locating the correct date and adding the correct offset can also work.
Let us know if this helps or if a VBA or the Pivot option seems best.

Excel: Trailing 3 month decline using data from a pivot table

(http://stackoverflow.com/questions/5283466/calculate-moving-average-in-excel seems to be somewhat related. I'm curious to know the non-vba way to do this.)
My my pivot table has source data that creates a count of how many sales a person had in the past 36 days. Each day os 1 column in the pivot table, and the persons name is on the row.
Some of these people did not have sales on day1, day2, etc, and may only have 3 days where they sold something on days 14,15 and 16. No matter what their sequence, I want to to find the most recent sale data (closest to the right edge of pivot table) and calculate three sales increases e.g. C20/C19 will be >1 if they had more sales on the whatever day C20 is. The increase ca nbe fond by subtracting 1 and changing to percent. The problem is, if a person only had sales on d10, d11, d12 then how can I put a general formula in Excel to say "look for the most recent consecutive sales, then calculate this ratio"? For a person who had sales in the past three days, that's easy. It will be chaotic to hardcode where to look for each sales value.
d1 d2 d3 d4...d7 d8 d9...d34 d35 d36 mostrecentincrease nextrecent
ant 1 5 7 7/5=1.4 5/1=5
bat ...10 11 12... 12/11=1.blah 11/10=1.1
cat 2 6 9 13
dog 19 20 20/19=1.blah 19/blank=0
elf 4 4/dnexist=0
i don't have excel here, but i hope this will guide you to the correct result (use , instead of ; if that is your list separator):
define sales as a range of values from d1 to d36 for a given row (or use actual ranges)
compute positions of the last values for each row using these array formulas (use ctrl+shift+enter instead of just enter after you write these formulas):
position_1 =max(if(sales<>0;column(sales)))
position_2 =max(if((sales<>0)*(column(sales)<>position_1);column(sales)))
position_3 =max(if((sales<>0)*(column(sales)<>position_1)*(column(sales)<>position_2);column(sales)))
retrieve the values:
value_1 =index(sales;position_1)
do some error handling (=iferror(...;0)) and the like...

Sum values in a column based on date

I have written this function that will give me a monthly sum for two columns: one has the date of each order, one has the cost of each order.
=SUMIF($C$1:$C$1000,">="&DATE(2010,6,1),$D$1:$D$1000)-SUMIF($C$1:$C$1000,">="&DATE(2010,7,1),$D$1:$D$1000)
Using data like this:
8/16/10 17:00 7.99
8/16/10 14:25 7.99
8/15/10 22:42 7.99
I end up with a table like this:
May 998
June 968.28
July 1239.76
August 514.96
However, now I would like to do daily sums and using my way I have to hand edit each row.
How can I do this better in Excel?
Use a column to let each date be shown as month number; another column for day number:
A B C D
----- ----- ----------- --------
1 8 6 8/6/2010 12.70
2 8 7 8/7/2010 10.50
3 8 7 8/7/2010 7.10
4 8 9 8/9/2010 10.50
5 8 10 8/10/2010 15.00
The formula for A1 is =Month(C1)
The formula for B1 is =Day(C1)
For Month sums, put the month number next to each month:
E F G
----- ----- -------------
1 7 July $1,000,010
2 8 Aug $1,200,300
The formula for G1 is =SumIf($A$1:$A$100, E1, $D$1:$D$100). This is a portable formula; just copy it down.
Total for the day will be be a bit more complicated, but you can probably see how to do it.
Use pivot tables, it will definitely save you time. If you are using excel 2007+ use tables (structured references) to keep your table dynamic. However if you insist on using functions, go with Smandoli's suggestion. Again, if you are on 2007+ use SUMIFS, it's faster compared to SUMIF.
Following up on Niketya's answer, there's a good explanation of Pivot Tables here:
http://peltiertech.com/WordPress/grouping-by-date-in-a-pivot-table/
For Excel 2007 you'd create the Pivot Table, make your Date column a Row Label, your Amount column a value. You'd then right click on one of the row labels (ie a date), right click and select Group. You'd then get the option to group by day, month, etc.
Personally that's the way I'd go.
If you prefer formulae, Smandoli's answer would get you most of the way there. To be able to use Sumif by day, you'd add a column with a formula like:
=DATE(YEAR(C1), MONTH(C1), DAY(C1))
where column C contains your datetimes.
You can then use this in your sumif.
Add a column to your existing data to get rid of the hour:minute:second time stamp on each row:
=DATE(YEAR(A1), MONTH(A1), DAY(A1))
Extend this down the length of your data. Even easier: quit collecting the hh:mm:ss data if you don't need it. Assuming your date/time was in column A, and your value was in column B, you'd put the above formula in column C, and auto-extend it for all your data.
Now, in another column (let's say E), create a series of dates corresponding to each day of the specific month you're interested in. Just type the first date, (for example, 10/7/2016 in E1), and auto-extend. Then, in the cell next to the first date, F1, enter:
=SUMIF(C:C, E1, B:B )
autoextend the formula to cover every date in the month, and you're done. Begin at 1/1/2016, and auto-extend for the whole year if you like.
If the second row has the same pattern as the first row, you just need edit first row manually, then you position your mouse pointer to the bottom-right corner, in the mean time, press ctrl key to drag the cell down. the pattern should be copied automatically.

Resources