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

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.

Related

Number occurrences in another cell (Excell) [duplicate]

I have simple problem, but I've not be able to get an answer from searching. I require a column to calculate the number of the nth occurrence of a value. It's best explained in this picture
I require a method to calculate column B.
I'd be very grateful for any help.
Are you looking to merely provide a count of the distinct entries of column A in column B? Or merely add a formula to come up with the table in your link?
If the latter, then the formula to write in cell B2 is:
=COUNTIF(A$2:A2,A2)
then copy/paste it down column B. Note - if your data is both a Date and Time, but the cell is formatted to only display a date, you may not get the results you want. You'd need to interject a new column with a "floor" calculation to round the date/time value to a date (Excel date times are decimal, with integer part dictating the date, and remaining 0.0 -> 1.0 dictating the time of day)
If you just want to derive a table of the counts of distinct entries in column A, then a pivot table will do this for you - simple add a pivot table to cover the data in column A, then select column A into the rows category, and then also drag it into the values category, ensuring the field is set to "Count of". You should then have a table with the distinct entries in your data set in one column, and the count of their occurrences in the other column.
You can use the COUNTIF worksheet function, with a relative address.
Eg. In cell B2, enter this formula:
=COUNTIF(A$2:A2,A2)
And then fill-down.
Use the following formula to generate the required series:
=COUNTIF($A$1:A1,A1) and strech(copy) it in all the cells
This will generate result like this:
A 1 COUNTIF($A$1:A1,A1)
A 2 COUNTIF($A$1:A2,A2)
C 1 COUNTIF($A$1:A3,A3)
C 2 COUNTIF($A$1:A4,A4)
B 1 COUNTIF($A$1:A5,A5)
B 2 COUNTIF($A$1:A6,A6)
A 3 COUNTIF($A$1:A7,A7)
C 3 COUNTIF($A$1:A8,A8)
D 1 COUNTIF($A$1:A9,A9)
D 2 COUNTIF($A$1:A10,A10)
D 3 COUNTIF($A$1:A11,A11)
D 4 COUNTIF($A$1:A12,A12)

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.

I need to retrieve the date from the row that will deplete my inventory

This is hard to explain...
I have a report that I pull from our MRP system that gives me a Due-Date, a quantity required, and an item number. There will usually be several rows with the same item number each representing a different sales order.
I have another report that gives me the on hand quantity for each item. In this report, each item is listed only once.
I need to get the Due-Date that corresponds to the row that will use up the last of my inventory. (Inv. runout)
Due-Date
QTY-REQ
ITEM
4/27/21
5
C
4/28/21
7
A
4/29/21
5
B
4/30/21
1
B
5/1/21
13
A
5/2/21
4
A
5/3/21
12
C
5/4/21
1
A
OHQ
ITEM
INV. RUNOUT
10
A
formula here, Result should be 5/1/21
5
B
result should be 4/30/21
10
C
result should be 5/3/21
I hope this makes sense, if not, please let me know what I can answer to help with this.
-Nick
You'll need a new column on the first report using IF(SUMIFS( to only show the date of lines where the stock runs out at or before that date. Then in the second report, use MINIFS(), a fairly new function, to find the earliest date in that column that matches each product.
In the below example I'm assuming that both of the above reports shown are columns A B and C, in the same excel file, on sheets called Report1 and Report2.
In cell Z2 on Report1, enter the formula IF(SUMIFS(B:B, A:A, "<=" & A2, C:C, C2) > INDEX(Report2!A:A,MATCH(Report1!C2,Report2!B:B,0)), A2, "") and copy down for all rows. This will only display the date where the stock has run out by that date.
In cell C2 on Report2, enter the formula =MINIFS(Report1!Z:Z, Report1!C:C, Report2!B2) to get the earliest relevant date displayed from the formula above.

Calculate Occurrence Number - Excel

I have simple problem, but I've not be able to get an answer from searching. I require a column to calculate the number of the nth occurrence of a value. It's best explained in this picture
I require a method to calculate column B.
I'd be very grateful for any help.
Are you looking to merely provide a count of the distinct entries of column A in column B? Or merely add a formula to come up with the table in your link?
If the latter, then the formula to write in cell B2 is:
=COUNTIF(A$2:A2,A2)
then copy/paste it down column B. Note - if your data is both a Date and Time, but the cell is formatted to only display a date, you may not get the results you want. You'd need to interject a new column with a "floor" calculation to round the date/time value to a date (Excel date times are decimal, with integer part dictating the date, and remaining 0.0 -> 1.0 dictating the time of day)
If you just want to derive a table of the counts of distinct entries in column A, then a pivot table will do this for you - simple add a pivot table to cover the data in column A, then select column A into the rows category, and then also drag it into the values category, ensuring the field is set to "Count of". You should then have a table with the distinct entries in your data set in one column, and the count of their occurrences in the other column.
You can use the COUNTIF worksheet function, with a relative address.
Eg. In cell B2, enter this formula:
=COUNTIF(A$2:A2,A2)
And then fill-down.
Use the following formula to generate the required series:
=COUNTIF($A$1:A1,A1) and strech(copy) it in all the cells
This will generate result like this:
A 1 COUNTIF($A$1:A1,A1)
A 2 COUNTIF($A$1:A2,A2)
C 1 COUNTIF($A$1:A3,A3)
C 2 COUNTIF($A$1:A4,A4)
B 1 COUNTIF($A$1:A5,A5)
B 2 COUNTIF($A$1:A6,A6)
A 3 COUNTIF($A$1:A7,A7)
C 3 COUNTIF($A$1:A8,A8)
D 1 COUNTIF($A$1:A9,A9)
D 2 COUNTIF($A$1:A10,A10)
D 3 COUNTIF($A$1:A11,A11)
D 4 COUNTIF($A$1:A12,A12)

Summarize hours using 2 criteria

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

Resources