I want the code value in column B if the handset matches and the date is between the date from and the date to.
How the results should look:
Since you problem involve Date Range, therefore Vlookup & Index match is not possible to solve, I will use If + AND formula to solve your problem:
=IF(AND(E9>$G$3,E9<$H$3,F9=$E$3),$F$3,
IF(AND(E9>$G$4,E9<$H$4,F9=$E$4),$F$4,
IF(AND(E9>$G$5,E9<$H$5,F9=$E$5),$F$5,
IF(AND(E9>$G$6,E9<$H$6,F9=$E$6),$F$6,""))))
If you are able to add a column, one possible solution without using VBA would be to use a combination of SUMIFS and XLOOKUP formulas.
In column E, add a unique identification number (e.g., 1, 2, 3, 4, 5, etc.). You could reference the row number or manually fill down the numbers or whatever. It wouldn't matter as long as the values are unique and all numbers (no text).
Then, use a SUMIFS formula to sum Column E if (1) the handset number matches, (2) the date is greater than or equal to the start date, and (3) the date is less than or equal to the finish date. As long as there are no overlaps in the date ranges for each handset, this will return the unique ID number. Depending on the dataset, you might have to play with the less/greater than or equal to vs just less/greater than.
=SUMIFS(E:E,A:A,I2,C:C,"<="&H2,D:D,">="&H2)
Then, use the XLOOKUP to return the code based on the unique ID. In this example, I combined the SUMIFS and XLOOKUP in one formula, but you could also do it in two columns to provide better visibility.
=XLOOKUP(SUMIFS(E:E,A:A,I2,C:C,"<="&H2,D:D,">="&H2),E:E,B:B,"NOT FOUND",0,1)
Here's an example.
screenshot
Related
I want to count number of values (N/D) in the array (below:table: list) for criteria 1 is date range( from date and through Date) and criteria 2 is Shift A, b acros ( as shown in below table-output). I want to fill column D/N with how many times D/N occur for a date range and shift A,B,C,D?
output
From Date Through Date Shift D/N
25-May-19 26-May-19 A ?
25-May-19 26-May-19 B ?
Table- list
Dates A B C D
25-May-19 N D - -
26-May-19 N D - -
27-May-19 - D N -
INDEX(A:E,MATCH(H7:I7,A:E,0),MATCH(J7,A:E,0))
Value -?
Part of the problem you may be having is dates. Make sure your dates are excel dates and not string/text that looks like a date. Simply changing the formatting of a cell does not make it a date, it simply tells excel how to try and display the information in a cell.
Dates in excel are stored as integers and they represent the days since 1900/1/1 with that date being day 1. One of the easiest ways to test if a cell contains a date or a string is:
=ISTEXT(A1)
or
=ISNUMBER(A1)
Where A1 is the cell with the date to be tested.
If it returns TRUE for the first formula it is a string/text. FALSE means it is a number. The opposite TRUE and FALSE results apply for the second formula.
In your formula's when you have something between quotes "", it will be interpreted as a string. SO something like "<=19/05/26" mean its looking for a string less than that, not a date less than that. For doing a date comparison, either concatenate the text comparison with with a cell containing a date to compare to "<="&B2 where B2 has the date or if you want to hard code it use something like "<="&Date(19,05,26)
In order to make the following solution work, your dates all need to be stored as a number. AKA Excel serial date format.
Based on the data being layed out as per the image below, you can use COUNTIFS, INDEX, and MATCH to get the date your are looking for. I recommend find your count of D and N separately and adding them together after for a total. However if you want it in a single cell formula solution it can be achieved as well as demonstrated by the results in column N. however the formula starts to get long and can be difficult potentially read/maintain at a later date.
The core of the solution will be the COUNTIFS functions. The format of the COUNTIFS function is as follows:
COUNTIFS(Range to count 1, Criteria 1, Range to count 2, Criteria 2,...,Range to count n, Criteria n)
Let start building your formula one criteria at a time. The first Criteria will be all dates that are greater than or equal to the from date. If you only want the dates after the from date, drop the equal sign or the criteria.
=COUNTIFS($A$2:$A$4,">="&$G2,
Note the $ to lock the cell references. This is done so that when the formula gets copied, the column or row references beside the $ does not change.
Then second criteria is similar to the first except you want to grab all the dates less than or equal to the through date. Again include/drop the equal sign to suit your needs.
=COUNTIFS($A$2:$A$4,">="&$G2,$A$2:$A$4,"<="&$H2,
The next criteria will be to get all the cells that match D or N the column header. Lets just focus on D for now. The tricky part is to define which column to look in. For now lets call the column to look in XXX which will make the formula become:
=COUNTIFS($A$2:$A$4,">="&$G2,$A$2:$A$4,"<="&$H2,XXX,J$1)
OR
=COUNTIFS($A$2:$A$4,">="&$G2,$A$2:$A$4,"<="&$H2,XXX,"="&J$1)
NOTE: both formulas are the same. When no comparison operator is provided
it is taken as "=" by default.
Now in order to define XXX, INDEX and MATCH will be your friends. An important side note about INDEX is that it does not directly return the value of a cell but instead returns a cell address which in turn pulls a cell value. The basic format of INDEX is:
INDEX(Range to look in, Range's ROW to look in, Range's COLUMN to look in)
That is for a 2 dimensional range. If your range is 1 dimensional, either just a column or just a row, then only the second argument "Range's Row..." need to be provided and it represents how far down the list to go.
What gets interesting about a 2D INDEX is that when 0 is provided for ROW to look in or the Column to look in, instead of throwing an error, it instead returns all rows or columns. THIS IS IMPORTANT because you want all rows of just 1 specific column. That mean your INDEX formula is going to start to look like:
INDEX($B$2:$E$4,0,SPECIFIC COLUMN NUMBER)
So now you need to find a specific column number. That is where MATCH will be your friend. MATCH takes the following format:
MATCH(Value to find, 1D range to look in, what type of search)
You already know you are going to try and match your shift column so that will be your look up value, and the range to look in will be your column headers. The type of search you will want in this case is an exact match which is represented by 0. That means your MATCH formula will look like:
MATCH($I2,$B$1:$E$1,0)
Now to combine the various pieces, throw the MATCH formula into your INDEX and replace the "SPECIFIC COLUMN...". Your INDEX will now look like:
INDEX($B$2:$E$4,0,MATCH($I2,$B$1:$E$1,0))
And the formula above can now replace the XXX in your COUNTIFS formula and you will get:
=COUNTIFS($A$2:$A$4,">="&$G2,$A$2:$A$4,"<="&$H2,INDEX($B$2:$E$4,0,MATCH($I2,$B$1:$E$1,0)),J$1)
Place the above formula in J2 and copy the cell down and to the right.
In L2 use one of the two formulas to get the total of D and N in the date range:
=SUM(J2:K2)
OR
=J2+K2
Now to get your formula all in one cell, look at the second formula above. You can simply go to the contents of cell J2 and copy the entire formula. Then edit cell L2 and replace the cell reference for for J2 with the copied formula. Repeat the process by copy formula in K2 and replacing the reference to K2 in L2. You will wind up with a formula that looks like:
=COUNTIFS($A$2:$A$4,">="&$G2,$A$2:$A$4,"<="&$H2,INDEX($B$2:$E$4,0,MATCH($I2,$B$1:$E$1,0)),J$1)+COUNTIFS($A$2:$A$4,">="&$G2,$A$2:$A$4,"<="&$H2,INDEX($B$2:$E$4,0,MATCH($I2,$B$1:$E$1,0)),K$1)
Much longer and harder to read which is why I recommend breaking the formula down into its parts for D and N separately.
Now as an alternate method you could use SUMPRODUCT and get into array operations. Your SUMPRODUCT formula to place in I2 and copy down and right could be:
=SUMPRODUCT(($A$2:$A$4>=$G2)*($A$2:$A$4<=$H2)*(INDEX($B$2:$E$4,0,MATCH($I2,$B$1:$E$1,0))=J$1))
I have the following data-set:
Given a number that occurs only once in the data range B2:E5, I would like to use a formula that returns the corresponding date value. The date values are given in the first column (A2:A5). I would also like to be able to return the corresponding hour value using a similar formula. Note, it is not necessary to return the hour and the date for a given value using the same formula.
Example: In the image of the data-set above – given the number 5, I would like to return the corresponding date. In this case, the formula would return the date value 03/01/2013. Similarly, I would also like to be able to return the "hour 3", given the same initial value.
Maybe the answer to this question is quite straight forward, but as of yet I have had no luck in figuring it out. Some things that I have tried, but to no avail, are the following: VLOOKUP/HLOOKUP, LOOKUP, a combination of INDEX+MATCH, and a combination of INDEX+MATCH+MATCH.
Any help is very much appreciated.
Given that Number to Find is unique, you can use this:
For the date in Column 1
=INDEX(Table1[Column1],MAX((NumToFind=Table1 )*ROW(Table1 ))-1)
For the hour in the Headers fields:
=INDEX(Table1[#Headers],1,MAX((NumToFind=Table1)*COLUMN(Table1)))
The above formulas are array formulas and must be confirmed by holding down CTRL + SHIFT ENTER
The -1 in the first formula is to compensate for the fact that the table starts in Row 1, with the data starting in Row 2, and the formula determines the absolute row number; if it starts other than in A1, different compensating values will be required.
I am currently drawing up a spreadsheet that will automatically remove duplicates and alphabetize a list:
I am using the COUNTIF() function in column G to create a sort order and then VLOOKUP() to find the sort in column J.
The problem I am having is that I can't seem to get my SortOrder column to function properly. At the moment it creates an index for two number 1's meaning the cell highlighted in yellow is missed out and the last entry in the sorted list is null:
If anyone can find and rectify this mistake for me I'll be very grateful as it has been driving me insane all day! Many thanks.
I'll provide my usual method for doing an automatic pulling-in of raw data into a sorted, duplicate-removed list:
Assume raw data is in column A. In column B, use this formula to increase the counter each time the row shows a non-duplicate item in column A. Hardcord B2 to be "1", and use this formula in B3 and drag down.
=if(iserror(match(A3,$A$2:A2,0)),B2+1,B2)
This takes advantage of the fact that when we refer to this row counter in our revised list, we will use the match function, which only checks for the first matching number. Then say you want your new list of data on column D (usually I do this for display purposes, so either 'group-out' [hide] columns that form the formulas, or do this on another tab). You can avoid this step, but if you are already using helper columns I usually do each step in a different column - easier to document. In column C, starting in C3 [C2 hardcoded to 1] and drag down, just have a simple counter, which error-checks to the stop at the end of your list:
=if(C2<max(B:B),C2+1," ")
Then in column D, starting at D2 and dragged down:
=iferror(index(A:A,match(C2,B:B,0)),"")
The index function is like half of the vlookup function - it pulls the result out of a given array, when you provide it with a row number. The match function is like the other half of the vlookup function - it provides you with the row number where an item appears in a given array.
Hope this helps you in the future as well.
The actual reason that this is going wrong as implied by Jeeped's comment is that you can't meaningfully compare a string to a number unless you do a conversion because they are stored differently. So COUNTIF counts numbers and text separately.
20212 will give a count of 1 because it is the only (or lowest) number.
CS10Z002 will give a count of 1 because it is the first text string in alphabetical order.
Another approach is to add the count of numbers to the count if the current cell contains text:-
=COUNTIF(INDIRECT("$D$2:$D$"&$F$3),"<="&D2)+ISTEXT(D2)*COUNT(INDIRECT("$D$2:$D$"&$F$3))
It's easier to show the result of three different conversions with some test data:-
(0) No conversion - just use COUNTIF
=COUNTIF(D$2:D$7,"<="&D2)
"999"<"abc"<"def", 999<1000
(1) Count everything as text
=SUMPRODUCT(--(D$2:D$7&""<=D2&""))
"1000"<"999"
(2) Count numbers before text
=COUNTIF(D$2:D$7,"<="&D2)+ISTEXT(D2)*COUNT(D$2:D$7)
999<1000<"999"
(3) Count everything as text but convert numbers with leading zeroes
=SUMPRODUCT(--(TEXT(D$2:D$7,"000000")<=TEXT(D2,"000000")))
"000999" = "000999", "000999"<"001000"
this should be simple enough, but numbers (on OSX) keeps throwing an error about the ranges being different sizes.
I have a list of numbers, each with an associated date, and I want a sum of all numbers within a particular month (to give, on a separate sheet, a monthly total).
Here is what I've tried:
SUMIFS(
Sheet1::Table 1::D2:D84,
MONTH(Sheet1::Table 1::A2:A84), "=04",
YEAR(Sheet1::Table 1::A2:A84), "=2014"
)
Sorry if this is a stupid question, but I've tried fiddling with it and it just won't accept it.
Thanks in advance.
You cannot put a function inside the range:
=SUMIFS(C1:C25;Month(A1:A25);"=3")
than you need to add two (hidden?) columns with Month & year function.
After you build SUMIFS based on the new columns
=SUMIFS(C1:C25;D1:D25;"=4";E1:E25;"=2014")
sum all value from C1 to C25 if column of month (D) is equal to 4 and column of year (E) is equal to 2014.
I would suggest considering using a SUMIFS function with an upper\lower limit, and then either referencing a cell with the dates, or using their numerical value in the formula (the former is my preference, to reduce hard coded values = easily updated\reused). So something like:
=SUMIFS(D2:D84, A2:A84, ">="&E1, A2:A84, "<="&E2)
In this example:
column 'D' has the values you want to sum
column 'A' has the date values
columns 'B' and 'C' are treated as irrelevant (for the sake of this formula)
column 'E' has 2 values, in row 1, the lower limit (for this specific question, the first of April) and in row 2 the upper limit (for this specific question, the final day of April)
Then have your lower limit for dates (the first day from when you would like column 'D' to be counted) in cell E1, and your date upper limit in E2.
Easily updated for future months, so might save you some work down the line.
The next easier option would be to update it to be formatted as a table, because your formula would be slightly more legible, add in some named ranges (in this case, E1 = 'lowerlimit' and E2 = 'upperlimit) to once again make it easier to read the formula, in which case you'd end up with something like:
=SUMIFS(table[FigureToBeAccrued], table[dates], ">="&lowerLimit, table[dates], "<="&upperlimit)
Might've overcooked this answer, it's my first, so wanted to make sure I didn't skimp. Let me know if you've got any follow up questions.
I'm struggling with integrating a condition into my COUNTIFS statement. I have about 5 conditions which I've been able to easily work in, but I can't figure out the last one. The criteria range would be A1:A40000, and the criteria would count the number that match any value in a list of 30 text strings on Sheet 2, Cells A1:A40. Is this possible? I can get the result without the other conditions. Unfortunately, I do not have the flexibility to add a column next to A1:A40000 that checks to see if it is in the list.
Edit: Clarification per request.
Simplified version of what I'm doing. I need to count the number items (column A) that meet several conditions depending on the column in the entire data set. So, I need to find the number of items that have a value of "1" in column B - AND - a value of "YES" in column "C" - AND - a value of "OLD" in column "D" - AND - (the part I'm struggling with) column "E" must contain any one of the values that's in a completely separate range (call it Z1:Z40). The formula for the first 3 conditions would be:
=COUNTIFS(B:B,1, C:C,"YES", D:D,"OLD")
The final criteria in bold would be something like:
=COUNTIFS(B:B,1, C:C,"YES", D:D,"OLD", **E:E,isnumber(match(E:E,Z1:Z40,0))**)
But that does not work...
You can simply use the range as a criteria. If you do that then your COUNTIFS function will return an array (one value each for each value in Z1:Z40) so you need a function to sum that array - I use SUMPRODUCT because it doesn't require array entry
=SUMPRODUCT(COUNTIFS(B:B,1,C:C,"yes",D:D,"old",E:E,Z1:Z40))
That approach has some limitations - you can only use two "multi-item" criteria in one COUNTIFS function (and if you do one must be a column, the other a row, or you need to use TRANSPOSE to make it that way), and items in Z1:Z40 should not be repeated (or you may get double counting).
To overcome either of those limitations you can use SUMPRODUCT in place of COUNTIFS - with ISNUMBER(MATCH for the multi-item criterion. If you use SUMPRODUCT like that then it's better to restrict the ranges for efficiency reasons, e.g.
=SUMPRODUCT((B2:B100=1)*(C2:C100="yes")*(D2:D100="old")*ISNUMBER(MATCH(E2:E100,Z1:Z40,0)))
You can add as many ISNUMBER(MATCH criteria as you want and Z1:Z40 can be any single row/column range
Let's say all your headers are in row 1 and the real data starts in row 2.
I would add a column on the end and put in the formula
=IF(AND(B2=1, C2="YES", D2="OLD", COUNTIF($Z$1:$Z$40,E2)),"YES","NO")
Then copy that down and any row where Column F was "YES" is a row that met all the criteria.
There is also a way to use wildcards
=countifs(A1:D1;"*yes")
which counts all cell which contain 'yes'