Max Conditional Statements in Excel with duplicate max values - excel

I need help in Excel of finding finding the Correct Output based on Amount of four conditional statements of:
max value of Timestamp;
if there are duplicate max dates, use the last row of the same Timestamp because the data does not extend upto seconds;
per month of the Timestamp;
per (4) unique ID
and leave the other rows blank. The Correct Output column is the expected output.
Here's the sample data:
╔════╦═════════════════════════════╦═════════╦════════════════╦══╦═════════════╦════════════╦═════════╗
║ ID ║ Timestamp (yyyy-mm-dd H:mm) ║ Amount ║ Correct Output ║ ║ Date Format ║ Max Date ║ Test ║
╠════╬═════════════════════════════╬═════════╬════════════════╬══╬═════════════╬════════════╬═════════╣
║ A ║ 2013-04-15 20:42 ║ NULL ║ NULL ║ ║ 2013-04-15 ║ 2013-04-15 ║ NULL ║
║ A ║ 2013-05-23 23:09 ║ 300000 ║ 300000 ║ ║ 2013-05-23 ║ 2013-05-23 ║ 300000 ║
║ A ║ 2013-09-15 23:36 ║ 300000 ║ 300000 ║ ║ 2013-09-15 ║ 2013-09-15 ║ 300000 ║
║ A ║ 2013-12-19 5:58 ║ 300000 ║ ║ ║ 2013-12-19 ║ 2013-12-19 ║ 0 ║
║ A ║ 2013-12-19 6:18 ║ 0 ║ ║ ║ 2013-12-19 ║ 2013-12-19 ║ 0 ║
║ A ║ 2013-12-19 6:18 ║ 0 ║ 0 ║ ║ 2013-12-19 ║ 2013-12-19 ║ 0 ║
║ B ║ 2013-05-23 23:00 ║ NULL ║ ║ ║ 2013-05-23 ║ 2013-05-23 ║ 0 ║
║ B ║ 2013-05-23 23:04 ║ 0 ║ 0 ║ ║ 2013-05-23 ║ 2013-05-23 ║ 0 ║
║ B ║ 2013-05-30 3:48 ║ 2339000 ║ ║ ║ 2013-05-30 ║ 2013-05-30 ║ 1169500 ║
║ B ║ 2013-05-30 3:54 ║ 1169500 ║ 1169500 ║ ║ 2013-05-30 ║ 2013-05-30 ║ 1169500 ║
║ B ║ 2013-06-02 11:46 ║ 2339000 ║ ║ ║ 2013-06-02 ║ 2013-06-02 ║ 2339000 ║
║ B ║ 2013-06-27 8:40 ║ 2339000 ║ ║ ║ 2013-06-27 ║ 2013-06-27 ║ 2339000 ║
║ B ║ 2013-06-27 13:59 ║ 2339000 ║ ║ ║ 2013-06-27 ║ 2013-06-27 ║ 2339000 ║
║ B ║ 2013-06-27 13:59 ║ 2339000 ║ 2339000 ║ ║ 2013-06-27 ║ 2013-06-27 ║ 2339000 ║
║ C ║ 2015-02-09 12:43 ║ NULL ║ ║ ║ 2015-02-09 ║ 2015-02-09 ║ NULL ║
║ C ║ 2015-02-09 12:43 ║ NULL ║ NULL ║ ║ 2015-02-09 ║ 2015-02-09 ║ NULL ║
║ D ║ 2013-12-02 0:05 ║ NULL ║ ║ ║ 2013-12-02 ║ 2013-12-02 ║ 37000 ║
║ D ║ 2013-12-02 0:37 ║ 37000 ║ 37000 ║ ║ 2013-12-02 ║ 2013-12-02 ║ 37000 ║
║ D ║ 2014-07-16 7:36 ║ 0 ║ ║ ║ 2014-07-16 ║ 2014-07-16 ║ 0 ║
║ D ║ 2014-07-16 7:36 ║ 0 ║ 0 ║ ║ 2014-07-16 ║ 2014-07-16 ║ 0 ║
║ E ║ 2013-06-27 8:35 ║ NULL ║ NULL ║ ║ 2013-06-27 ║ 2013-06-27 ║ NULL ║
╚════╩═════════════════════════════╩═════════╩════════════════╩══╩═════════════╩════════════╩═════════╝
References:
Column A - ID
Column B - Timestamp
Column F - Date Format
Column G - Max Date
Column H - Test
Here are the steps I tried:
I created the Date Format using date
Created the Max Date with =MAX(IF($F$2:$F$22=F2,IF($A$2:$A$22=A2,$B$2:$B$22))) (in array)
Created Test with =INDEX(C2:C22,MATCH(G2,B2:B22,0)) (in array)

Assuming cell D2 for the first "Correct Output":
=IF(SUMPRODUCT(0+(MONTH(G$2:G$22)=MONTH(G2)),0+(A$2:A$22=A2))=SUMPRODUCT(0+(MONTH(G$2:G2)=MONTH(G2)),0+(A$2:A2=A2)),LOOKUP(1,0/((MONTH(G$2:G$22)=MONTH(G2))*(A$2:A$22=A2)),C$2:C$22),"")
Copy down as required.
Note that you can amend the upper row being referenced (22 here) to meet your requirements, though you should be careful not to make it too arbitrarily large (and certainly don't reference entire columns!), since, for each additional cell referenced, extra calculation will be required.
And that applies whether those additional cells are technically beyond the last-used cells in those ranges or not. With some functions, e.g. COUNTIF(S)/SUMIF(S), you can get away with referencing entire columns with no detriment to performance, though not with functions which operate over arrays, such as AGGREGATE, SUMPRODUCT, and any construction requiring CSE.
Even better, makes your ranges dynamic, such that they automatically adjust as your data expands/contracts.
Regards

Related

How can I search for an item within a cell that's also in a range?

I have Table A:
╔═══╦═════╦═════════════╦══════════════════╗
║ ║ a ║ b ║ c ║
╠═══╬═════╬═════════════╬══════════════════╣
║ 1 ║ ID ║ LIST VALUES ║ Table B Values ║
╠═══╬═════╬═════════════╬══════════════════╣
║ 2 ║ 123 ║ 231,583 ║ eggs,bacon ║
╠═══╬═════╬═════════════╬══════════════════╣
║ 3 ║ 789 ║ 518,732 ║ bacon,bread ║
╠═══╬═════╬═════════════╬══════════════════╣
║ 4 ║ 101 ║ 55,38 ║ tomato,onion ║
╠═══╬═════╬═════════════╬══════════════════╣
║ 5 ║ 213 ║ 894,231 ║ ham,eggs ║
╠═══╬═════╬═════════════╬══════════════════╣
║ 6 ║ 141 ║ 55,38,894 ║ tomato,onion,ham ║
╠═══╬═════╬═════════════╬══════════════════╣
║ 7 ║ 516 ║ 548,43 ║ milk,butter ║
╚═══╩═════╩═════════════╩══════════════════╝
And I have Table B:
╔═══╦═════╦═════════════╗
║ ║ a ║ b ║
╠═══╬═════╬═════════════╣
║ 1 ║ ID ║ LIST VALUES ║
╠═══╬═════╬═════════════╣
║ 2 ║ 231 ║ eggs ║
╠═══╬═════╬═════════════╣
║ 3 ║ 518 ║ bacon ║
╠═══╬═════╬═════════════╣
║ 4 ║ 732 ║ bread ║
╠═══╬═════╬═════════════╣
║ 5 ║ 55 ║ tomato ║
╠═══╬═════╬═════════════╣
║ 6 ║ 38 ║ onion ║
╠═══╬═════╬═════════════╣
║ 7 ║ 894 ║ ham ║
╠═══╬═════╬═════════════╣
║ 8 ║ 548 ║ milk ║
╠═══╬═════╬═════════════╣
║ 9 ║ 43 ║ butter ║
╚═══╩═════╩═════════════╝
These are representations of the data I have, and what I need help with. The actual data is a lot longer than these tables, and the delimited items can be very long.
What I'm trying to do is populate Table A, Column C. I need to search the items from TABLE B, Column A for the listed items in TABLE A, Column B within the cell through the whole range.
I guess this can be done vice versa as well. I am familiar with INDEX-MATCH function, but I'm not sure how to add searching within the cell.
Use TEXTJOIN and ISNUMBER/SEARCH:
=TEXTJOIN(",",TRUE,IF(ISNUMBER(SEARCH(","&$G$2:$G$9&",",","&B2&",")),$H$2:$H$9,""))
If you have Windows Excel, try:
C2: =TEXTJOIN(",",TRUE,VLOOKUP(FILTERXML("<t><s>"&SUBSTITUTE(B2,",","</s><s>")&"</s></t>","//s"),$G$2:$H$9,2,FALSE))
Table A
Table B
Results

Sumproduct subtract total amount from oldest month in different month columns

I hv 4 different columns of Database
╔════╦════════════╦═════════╦═════════════╦═════════════╗
║ ║ A ║ B ║ C ║ D ║
╠════╬════════════╬═════════╬═════════════╬═════════════╣
║ 1 ║ Date ║ Party ║ Debit ║ Credit ║
║ 2 ║ 25-12-2019 ║ John ║ 50,000 ║ ║
║ 3 ║ 27-12-2019 ║ Neil ║ 50,000 ║ ║
║ 4 ║ 29-12-2019 ║ John ║ 1,00,000 ║ ║
║ 5 ║ 01-Jan ║ Neil ║ 50,000 ║ ║
║ 6 ║ 05-Jan ║ John ║ 8,00,000 ║ ║
║ 7 ║ 08-Jan ║ John ║ 70,000 ║ ║
║ 8 ║ 28-Jan ║ Neil ║ 20,000 ║ ║
║ 9 ║ 02-Feb ║ John ║ 30,000 ║ ║
║ 10 ║ 15-Feb ║ Neil ║ 19,000 ║ ║
║ 11 ║ 27-Feb ║ John ║ 21,000 ║ ║
║ 12 ║ 04-Mar ║ John ║ 22,000 ║ ║
║ 13 ║ 08-Mar ║ Neil ║ 88,000 ║ ║
║ 14 ║ 09-Mar ║ John ║ 6,00,000 ║ ║
║ 15 ║ 05-Apr ║ John ║ 7,05,000 ║ ║
║ 16 ║ 21-Apr ║ Rebbika ║ 25,00,000 ║ ║
║ 17 ║ 05-May ║ John ║ ║ 7,00,000 ║
║ 18 ║ 07-May ║ Neil ║ ║ 5,00,000 ║
║ 19 ║ 19-May ║ John ║ ║ 1,00,000 ║
║ 20 ║ 21-May ║ Rebbika ║ ║ 10,00,000 ║
╚════╩════════════╩═════════╩═════════════╩═════════════╝
and here is the result table (Which actually is a Trial of each Party with monthly remaining balance
╔═══╦═════════╦════════╦════════╦═══════╦════════╦═════════╗
║ ║ E ║ F ║ G ║ H ║ I ║ J ║
╠═══╬═════════╬════════╬════════╬═══════╬════════╬═════════╣
║ 1 ║ Party ║ Dec-19 ║ Jan-20 ║ Feb-20║ Mar-20 ║ Apr-20 ║
║ 2 ║ John ║ - ║ 220000 ║ 51000 ║ 622000 ║ 705000 ║
║ 3 ║ Neil ║ 10000 ║ 70000 ║ 19000 ║ 88000 ║ 0 ║
║ 4 ║ Rebbika ║ - ║ - ║ - ║ - ║ 1500000 ║
╚═══╩═════════╩════════╩════════╩═══════╩════════╩═════════╝
and this is the Formula i am using in E2 to J4.
The formula i copied is from Cell I2
=IF(SUMPRODUCT(--(MONTH($A$2:$A$20)=12)+--(MONTH($A$2:$A$20)<=2),--($B$2:$B$20=F3),$C$2:$C$20)-SUMIF($B$2:$B$20,F3,$D$2:$D$20)>1,SUMPRODUCT(--(MONTH($A$2:$A$20)=3),--($B$2:$B$20=F3),$C$2:$C$20),IF(SUMPRODUCT(--(MONTH($A$2:$A$20)=3),--($B$2:$B$20=F3),$C$2:$C$20)-(SUMIF($B$2:$B$20,F3,$D$2:$D$20)-SUMPRODUCT(--(MONTH($A$2:$A$20)=12)+--(MONTH($A$2:$A$20)<=2),--($B$2:$B$20=F3),$C$2:$C$20))<1,"",SUMPRODUCT(--(MONTH($A$2:$A$20)=3),--($B$2:$B$20=F3),$C$2:$C$20)-(SUMIF($B$2:$B$20,F3,$D$2:$D$20)-SUMPRODUCT(--(MONTH($A$2:$A$20)=12)+--(MONTH($A$2:$A$20)<=2),--($B$2:$B$20=F3),$C$2:$C$20))))
E.g:-
Total Due Payment of Dec to John is 150,000
Payment received has nothing to do with month or date, its just the whole payment we have received till the date
So, If he has paid us 120,000
Balance of Dec=30,000, and Jan-Feb-Marc... Due payment is still pending as it is
If we have Received the Total Payment 1,021,000
Then it will clear the 150,000 of Dec and 870,000 of Jan, and 1,000 from the Feb and will Show Dec Column as Nill, Jan=Nill, Feb 50,000
Total of Received Amount should be subtracted from the oldest month of billing
But the function i am using is really messy and making sheet a bit heavy, Is there any possible shorter and nice way to perform this task without using vba codes but with shorter formula than the one i am trying. its MS 2007
Any help will be appreciated
I finally Got it
=IFERROR(1/(1/ROUND(MAX(0,SUMIFS($C:$C,$B:$B,$E2,$A:$A,"<="&EOMONTH(F$1,0))-SUMIF($B:$B,$E2,$D:$D)-SUM($E2:E2)),0)),"-")
Dragging it vertically and horizontally worked perfectly fine.

Excel 2016 Countf function using multiple criterion help needed

I am trying to figure out how to take a worksheet and break out some information using countif functions and/or be able to show it in a pivot table functions.
The problem I am having is that some of the data in one of the cells has more than one piece of information. This is a sample:
╔═══════╦════════════╦═══════╦══════════════════╗
║ Claim ║ Date ║ Model ║ Fail Description ║
╠═══════╬════════════╬═══════╬══════════════════╣
║ ║ ║ ║ ║
║ 1 ║ 1/1/2017 ║ T430 ║ KB, MOBO ║
║ 2 ║ 1/5/2017 ║ t450 ║ KB, MOBO ║
║ 3 ║ 1/6/2017 ║ T440 ║ HDD, MOBO ║
║ 4 ║ 1/7/2017 ║ CTO ║ HDD ║
║ 5 ║ 1/8/2017 ║ T430 ║ KB ║
║ 6 ║ 1/9/2017 ║ t450 ║ KB ║
║ 7 ║ 1/10/2017 ║ T440 ║ LID ║
║ 8 ║ 1/10/2017 ║ CTO ║ LID, KB ║
║ 9 ║ 1/10/2017 ║ T430 ║ HDD, MOBO ║
║ 10 ║ 1/7/2017 ║ t450 ║ KB, MOBO ║
║ 11 ║ 1/7/2017 ║ T440 ║ KB, MOBO ║
║ 12 ║ 1/7/2017 ║ CTO ║ HDD, MOBO ║
║ 13 ║ 1/7/2017 ║ T430 ║ HDD ║
║ 14 ║ 1/7/2017 ║ t450 ║ KB ║
║ 15 ║ 1/5/2017 ║ T440 ║ KB ║
║ 16 ║ 1/5/2017 ║ CTO ║ LID ║
║ 17 ║ 1/5/2017 ║ T430 ║ LID, KB ║
║ 18 ║ 1/5/2017 ║ t450 ║ HDD, MOBO ║
║ 19 ║ 1/5/2017 ║ T440 ║ KB, MOBO ║
║ 20 ║ 1/5/2017 ║ CTO ║ KB, MOBO ║
║ 21 ║ 12/25/2017 ║ T430 ║ HDD, MOBO ║
║ 22 ║ 12/25/2017 ║ t450 ║ HDD ║
║ 23 ║ 12/25/2017 ║ T440 ║ KB ║
║ 24 ║ 12/25/2017 ║ T450 ║ KB ║
║ 25 ║ 12/25/2017 ║ T450 ║ LID ║
║ 26 ║ 12/25/2017 ║ T440 ║ LID, KB ║
╚═══════╩════════════╩═══════╩══════════════════╝
As you can see the Fail Description sometimes has more than one value separated by a comma. I would like to take a count of each type of fail based on model type of computer. So a countif (C2:C27, "*450") and also countif (D2:D27, "*MOBO*") Asking how many 450's needed a Motherboard. I have tried it several different ways and either end up with a #Value or 0 error. Does anyone have any suggestions?? Thanks!
Hmmmmm...
I hope I understand your question correctly
=COUNTIFS($C:$C,"*450*",$D:$D,"*MOBO*")

Hide lines with zero values

I have the following table generated by a pivot:
╔════════╦══╦══════════╦════════════╦═════════╦═════════╦═════════╦═════════╦═════════╗
║ Line # ║ ║ Car ║ Sold/Stock ║ Store 1 ║ Store 2 ║ Store 3 ║ Store 4 ║ Store 5 ║
╠════════╬══╬══════════╬════════════╬═════════╬═════════╬═════════╬═════════╬═════════╣
║ 1 ║ ║ BMW ║ Sold ║ 5 ║ 1 ║ 0 ║ 6 ║ 4 ║
║ 2 ║ ║ BMW ║ Stock ║ 2 ║ 0 ║ 2 ║ 3 ║ 4 ║
║ 3 ║ ║ Audi ║ Sold ║ 3 ║ 4 ║ 5 ║ 5 ║ 8 ║
║ 4 ║ ║ Audi ║ Stock ║ 3 ║ 1 ║ 0 ║ 0 ║ 1 ║
║ 5 ║ ║ Mercedes ║ Sold ║ 2 ║ 0 ║ 4 ║ 5 ║ 6 ║
║ 6 ║ ║ Mercedes ║ Stock ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║
╚════════╩══╩══════════╩════════════╩═════════╩═════════╩═════════╩═════════╩═════════╝
I want to hide the last row(6). Can it be done? I can't filter columns store 1 2 3 because i only want to filter when they all are blank.
Thanks
The filters for the pivot table should show you all available values including "blank". Set the drop-down to show multiple values, and turn that one off.
You can do this in VBA, like so:
Dim hideMe as Bool
hideMe = True
For Each c In Range("E7:I7")
If c.Value <> 0 Then
hideMe = False
End If
Next
Rows(7).EntireRow.Hidden = hideMe
The script assumes that you will want to hide the row (hideMe = True), and only decides otherwise if there is any cell in that range which is different from 0.

Find a max value for each Range interval in Excel

I have data in an Excel sheet that depict the quantity of various gases present at depth say d. So for each depth interval like from 3238.1 to 3238.9 i need to find the max value of all the gases columns (C1-C3, IC4,IC5,nC5, OC2).
For example for depth 3238 there are 9 interval (3238.1 to 3238.9) so i need to find the max value in all the gas columns like for column C1 max value for 3238 will be 5650.
Sometimes there are more than 10 intervals for a meter e.g 1338.1, 1338.2, ...up to 1338.15 etc or sometimes less than 10, e.g, 1338.1,1338.3,1338.5,1338.6 etc because of missing data.
INPUT:
╔═══════╦════════╦══════╦═════╦═════╦═════╦═════╦═════╦═════╦══════╗
║ ║ Depth ║ C1 ║ C2 ║ C3 ║ iC4 ║ nC4 ║ iC5 ║ nC5 ║ OC2 ║
╠═══════╬════════╬══════╬═════╬═════╬═════╬═════╬═════╬═════╬══════╣
║ ║ 3238.1 ║ 0 ║ 125 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1200 ║
║ ║ 3238.2 ║ 5601 ║ 78 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1600 ║
║ ║ 3238.3 ║ 5610 ║ 156 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1588 ║
║ ║ 3238.4 ║ 5612 ║ 120 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1900 ║
║ ║ 3238.5 ║ 5640 ║ 300 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 2100 ║
║ ║ 3238.6 ║ 5650 ║ 401 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 2648 ║
║ ║ 3238.7 ║ 5601 ║ 366 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 2841 ║
║ ║ 3238.8 ║ 5610 ║ 102 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 450 ║
║ ║ 3238.9 ║ 5612 ║ 211 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1800 ║
║ ║ 3239.0 ║ 111 ║ 20 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1723 ║
║ ║ 3239.1 ║ 121 ║ 39 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1646 ║
║ ║ 3239.2 ║ 56 ║ 12 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1569 ║
║ ║ 3239.3 ║ 214 ║ 6 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1492 ║
║ ║ 3239.4 ║ 125 ║ 9 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1415 ║
║ ║ 3239.5 ║ 300 ║ 7 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1338 ║
║ ║ 3239.6 ║ 390 ║ 14 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 3160 ║
║ ║ 3239.7 ║ 312 ║ 16 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1523 ║
║ ║ 3239.8 ║ 360 ║ 18 ║ 3 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1634 ║
║ ║ 3239.9 ║ 380 ║ 19 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 4823 ║
║ ║ 3240.0 ║ 80 ║ 6 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 3065 ║
╚═══════╩════════╩══════╩═════╩═════╩═════╩═════╩═════╩═════╩══════╝
Example OUTPUT:
╔═══════╦══════╦═════╦════╦═════╦═════╦═════╦═════╦══════╗
║ Depth ║ C1 ║ C2 ║ C3 ║ iC4 ║ nC4 ║ iC5 ║ nC5 ║ OC2 ║
╠═══════╬══════╬═════╬════╬═════╬═════╬═════╬═════╬══════╣
║ 3238 ║ 5650 ║ 401 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 2841 ║
║ 3239 ║ 390 ║ 39 ║ 3 ║ 0 ║ 0 ║ 0 ║ 0 ║ 4823 ║
╚═══════╩══════╩═════╩════╩═════╩═════╩═════╩═════╩══════╝
The data shows the quantity of various gases present at depth d.
I need to find the maximum for each meter interval (max for 3238.1 to 3238.9).
I tried using the Excel max function but was not able to achieve the desired output.
What is the best way to achieve this, which functions should I use or macros etc.
You may use array formula max(if()) to calculate max value over the range, matching criteria and floor() function to get rid of the decimal part:
=FLOOR(A2,1)
=MAX(IF(FLOOR($A$2:$A$21,1)=K2,$B$2:$B$21))

Resources