Summarize hours using 2 criteria - excel

I wish to add a summary sheet to a workbook that captures timesheet data.
The timesheet sheets are laid-out like the screenshot below, where each row represents a day, and then a person's total hours worked are distributed across columns C to E, which are identified by a unique combination of seg2 and seg3 codes:
So, for the first column (C) has a unique combination of 201 & 303, which totals 19.9 hours, then (D) has a unique combination of 201 & 301 which totals 5.9 hours and then (E) has a unique combination of 202 & 303 which totals 11.2 hours.
My summary sheet will look like the screenshot below; where every possible combination of seg2 and seg3 codes are listed:
What I'm looking for is a formula on the summary sheet against the "January Hours" column that will take the combination of Seg2 and Seg3 codes for its row...
Then compare that with the range of cells in the timesheet sheet that has the Seg2 and Seg3 codes used:
Where it finds a match...
I want it to return the SUM of the range of hours below it...
...returning the total number of hours for that unique Seg2 and Seg3 combination:
I'll then drag the fill handle to finish all the formulas for the Summary sheet's "January Hours" column.
I've managed to do this using a single seg code using SUMPRODUCT, but have not yet figured out how to do it for 2 criteria (i.e. 2 seg codes).

By leaving the row parameter of the INDEX function empty, you can lookup an entire column. See the below formula and enter with CTRL + SHIFT + ENTER
=SUM(INDEX($C$4:$E$8,,MATCH($G2,IF($H2=$C$2:$E$2,$C$1:$E$1),0)))
Please note that this formula sums up all the values in the table as suggested in your example, and not just the values with a date of 1/1/2015.

INDEX/MATCH can handle a 2-way match -- is it necessary to "re-sum" the hours? If the Seg2/Seg3 Totals from the time sheets are always in the same place, this will look up the total hours:
=INDEX(Timesheet!$C$2:$E$10,9,MATCH($A2&$B2,Timesheet!$C$2:$E$2&$C$3:$C$3,0))
Edit: forgot to note this is an array formula

Related

How can I average the last n number of rows when using a Average(Index, match formula)?

I have two sheets one has a table with sets across the top of the table and daily km for each set the second sheet has set numbers in column b, I wish to find the daily average for the last 6 months (so 182 days) for each of the sets and return the value in column F.
Sheet 2
sheet 1 - Table
I have used the following formula to average the entire column
=AVERAGE(INDEX(Table1[[H9003]:[H9043]],0,MATCH('FA Forecasts'!B5,Table1[[#Headers],[H9003]:[H9043]],0)))
However I only want to return the average of the last 182 rows of the table to the corresponding set number - and each time a new line is added I only want the last 182 rows.
I'm not sure what I can do to achieve this - any advice would be most appreciated.
If you have Excel 365 you can use this formula:
=LET(indexSetNumber,MATCH([#[Set Number]],tblSheet1[#Headers],0),
filterByDate,FILTER(tblSheet1,tblSheet1[Date]>=LARGE(tblSheet1[Date],cntDays)),
filterBySet,INDEX(filterByDate,,indexSetNumber),
AVERAGE(filterBySet))
I like "readable" formulas - this one reads like this:
indexSetNumber: returns the column index of e.g. H9003 --> 5
filterByDate: returns the rows with Top "cntDays" dates. I am using a parameter cntDays in C2 - for this example I chose 2, but you would put 182.
filterBySet: returns column indexSetNumber from filterByDate
from these values AVERAGE is returned.
This might also work (assuming C2 is =TODAY()):
=AVERAGEIF(Table1[Date],">" & C2 - F2,INDIRECT("Table1["& 'FA Forecasts'!B5 &"]"))
Using AVERAGEIF formula :
Table1[Date] : returns the "Date" table column (sheet 1)
">" & C2 - F2 : "date more recent than Today-182 days"
INDIRECT("Table1["& 'FA Forecasts'!B5 &"]") : returns the table column (sheet 1) which header match with Set Number in the current row (sheet 2)
Basically return the average for kms in the past 182 days for the Set Number of the row.

Only show the sum of a column if there are at least 3 records in another column

I have 3 Columns of data. Column E = Duration. The user will enter a number of days (Duration) for 20 employees. Column F = Pay. List of wage for each employee. Column R = Pay Total. This is a calculated field based on the number of days (Duration) and the employees pay (Pay).
To keep my employees from finding out their coworkers pay I only want to see a sum for Column C if there are at least 3 records in Column A.
I found this Sum only if 3
From that answer I tried to use =IF(COUNTIF(E2:E15,">2"),SUM(R2:R15))
What this does is only returns records with a duration of greater than 2 days. I don't care about the number of days. I want the indicator to be that there are 3 cells populated in this column.
I think the only issue with the formula is the CountIF part. I need to know what I should use to count cells not days.
Screenshot of My Sheet
In the image above you can see that it's returning a sum bc both records are greater than 2. Like I said before I don't care what number is in the cell I just want to see the sum if there are more than 2 cells populated in that column.
Thanks for any help
As per my understanding of the question, I came up with this solution. Give it a try.
=IF(COUNT($E$2:$E$15)>2,SUM($R$2:$R$15), 0)
This formula would give what you want
=IF(COUNTA(E2:E15)>2,SUM(R2:R15),"")
This would show sum only if count of non empty cells is greater than 2 in Range "E2:E15" else it would show empty.

How to create a dynamic formula to find the average of a set of values for a given vector

I am trying to create a formula that gives me the average of the last 12 entries in a given dataset depending on the associated vector.
Let's make an example:
I have in column F2,G2,H2 and I2 dates, Company1, Company2 and Company3 respectively. Then from row3 to row 33 I have months dates starting from May 2016.
Date Company1 Company2 Company3
May-16 2,453,845
Jun-16 13,099,823
Jul-16 14,159,037
Aug-16 38,589,050 8,866,101
Sep-16 63,290,285 13,242,522
Oct-16 94,005,364 14,841,793
Nov-16 123,774,792 7,903,600 41,489,883
Dec-16 93,355,037 12,449,604 69,117,105
Jan-17 47,869,982 13,830,712 83,913,764
Feb-17 77,109,905 10,361,555 68,176,643
The goal is to create a formula that, when I drag it down, correctly calculates the average of the last 12 values for a given company.
So for example i would have, say in table "B2:C5":
Company1 76,856,345
Company2 11,120,859
Company3 65,674,349
And, if a new Company4 is added to the list, then I just have to drag it down the formula, to calculate the average of the last 12 months for Company4.
Until now, I have came up with this formula:
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(G:G),ROW(G:G)),ROW(INDIRECT("1:"&MIN(12,COUNT(G:G))))),ROW(G:G),G:G ))
This formula correctly calculates the average of a given column, considering only the last 12 values. The last step would be to come up with a formula that includes all the columns and then calculates the average for the given company.
Thanks!
I recommend that you use a named range to define your data in columns G:I. When a company is added, just modify the named range's specs. I used the name Target. Of course, you can replace it with $G:$I if you feel so inclined but I would rather recommend reducing the number of rows in the range, which is easier to manage when it is named.
Use the formula below to extract the company names from the first row of Target into the first column of your averages table. This is to ensure that the names are spelled identically in both locations.
=INDEX(Target,1,ROW()-2)
The number 2 indicates the number of rows above the row containing the formula. it is copied here from cell M3. There, ROW()-2 creates the number 1, counting sequentially as the formula is copied down.
Now I have the formula below in my cell N3 and copied down.
=SUM(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))
The formula simply sums up the columns G, H, and I in 3 consecutive rows.
In the final step I inserted the range definition established above, meaning excluding the SUM() function, into your existing formula.
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))),ROW(INDIRECT("1:"&MIN(12,COUNT(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))

Formula for average entries per day

I have a very simple sheet with dates in column A and product names in column B. I just need to know how many sales I make on average per day.
Sales:
01/01/2018 PRODUCT A
01/01/2018 PRODUCT A
01/02/2018 PRODUCT A
01/02/2018 PRODUCT B
Average sales per day: 2
So I don't care what product it is. Just how many sales per day on average.
You can use counta(a2:a5) to get the count of items in a range (adjust your range to suit of course).
You can use the array formula sum(1/countif(a2:a5,a2:a5)) to get the count of unique items in a range. The way to get an array formula is to enter it with CTRLSHIFTENTER rather than just ENTER (it will show up in the formula bar with {braces}).
It should then be a simple matter of dividing the former by the latter to get the average items per unique item (sales per day and, again, this should be an array formula):
=counta(a2:a5)/sum(1/countif(a2:a5,a2:a5))
The only tricky bit there is the countif formula. The expression countif(range,value) will give you a count of all items in the range that match the value.
By making the value the same as the range, it counts (for each item in the range) the number of times an item appears.
So, if your range contains a,a,a,a,b,b,c, you'll get the array (4,4,4,4,2,2,1). Doing the sum of the reciprocals of those values for each cell in the range gives:
(1/4 + 1/4 + 1/4 + 1/4) + (1/2 + 1/2) + (1/1)
which is basically a sneaky way of counting the unique items.
Assuming dates are entered as real Date (and not just text that only looks like dates), use this
=COUNT(A:A)/SUM(--(FREQUENCY(A:A,A:A)>0))
(Frequency formula sourced from here )

How do I check for duplicate rows and do calculate on that rows in excel file?

I have one excel file which is having data like following
CUSIP Quantity Date Price
AF0 500000 5/6/2013 1
AE4 400000 5/6/2013 1.0825
AE4 500 5/6/2013 1
I need to check for column CUSIP and Date
If I'm having duplicate CUSIP for same date then I need to do following calculation.
1.Need to add Quantity for both of them instead of showing duplicate records need to show only one record( sum of Quantity).
2.Need to do calculation on Price as well like following
NewPrice = ((400000 * 1.0825) + (500 * 1.00))/(400000 + 500) = 1.08148
For example in using above data
Need to show output like
CUSIP Quantity Date Price
AF0 500000 5/6/2013 1
AE4 400500 5/6/2013 1.082397004
How do I achieve this in excel file using LOOKUP or else ?
Okay, after quite some research (interesting question by the way!), I came up with this:
=IF(COUNTIF($A$2:A2,A2)>1,"",SUMIF(A:A,A2,B:B))
=IF(COUNTIF($A$2:A2,A2)>1,"",SUMPRODUCT(--(A:A=A2),B:B,D:D)/SUMIF(A:A,A2,B:B))
Put these in cell D2 and E2 respectively (which is the next column after Price and in the row of CUSIP AF0).
And fill to the bottom of the worksheet to get the weighted average price or each CUSIP.
The first formula gives you the total quantity for the CUSIP and the second gives you the average price.
Copy and paste values for those two columns after calculation.
Put a filter and remove all the rows where the total quantity and average price is blank, and sort to make the worksheet neat.
Let me know if this works for you! I tried it on your sample data and it seems to be working. It's my first time using SUMPRODUCT ^^;
SUMPRODUCT(--(A:A=A2)
This bit returns the rows from column A where it equals to the row's CUSIP.
SUMPRODUCT(--(A:A=A2),B:B,D:D
This additional bit tells excel to multiply the values in column B and D of each returned row found above and SUMPRODUCT adds each result together.
EDIT:
I actually forgot about the date. You could maybe add a helper column where you'll generate an identifier to separate the different dates. To make it, you'll have to make a concatenate.
Hence in cell F2, you put:
=CONCATENATE(A2,C2)
In the formulae for cells D2 and E2, you will have to change them so they become:
=IF(COUNTIF($F$2:F2,F2)>1,"",SUMIF(A:A,A2,B:B))
=IF(COUNTIF($F$2:F2,F2)>1,"",SUMPRODUCT(--(A:A=A2),B:B,D:D)/SUMIF(A:A,A2,B:B))
reEDIT: Oops, put the wrong reference. Fixed now.

Resources