Count rows where date is inside current month with multiple criteria - excel

I'm trying to create a formula that checks the date column of a row and checks it is within the current month, then checks a separate column in a row and checks it is equal to some text, the formula would then output a count of instances where that is true.
For reference, the columns are named as two ranges, LeadDate and LeadAcquisitionChannel.
[edit] The Refers to: definitions are:
'LeadDate Refers to:
=OFFSET('Enquiry Log'!$A$2,0,0,COUNTA('Enquiry Log'!$A:$A),1) 'LeadAcquisitionChannel Refers to:
=OFFSET('Enquiry Log'!$J$2,0,0,COUNTA('Enquiry Log'!$J:$J),1)
I ran the date check with following formula:
=COUNTIF(LeadDate,">"&EOMONTH(TODAY(),-1)
Which was successful in telling me how many entries fall within the current month.
So I assumed I could then add the multiple criteria in a COUNTIFS like this:
=COUNTIFS(LeadDate,">"&EOMONTH(TODAY(),-1),LeadAcquisitionChannel,B27)
B27 being a cell containing the text I want to check for.
This formula returns me a #VALUE! error.
Any ideas as to why this is happening or whether there is an alternative method I can use?

The key here lies in the Refers to: definitions for both LeadDate and LeadAcquisitionChannel as COUNTIFS requires that each criteria range be the same size and shape of cells.
For a dynamic list of dates that grows when new dates are added:
='Enquiry Log'!$a$2:index('Enquiry Log'!$a:$a, match(1e99, 'Enquiry Log'!$a:$a))
For a matching dynamic list of other information in an adjacent column use the same terminating row like this:
='Enquiry Log'!$j$2:index('Enquiry Log'!$j:$j, match(1e99, 'Enquiry Log'!$a:$a))
Each of these refers to a column of data extending down in each individual column to the row containing the last date in column A.
Your original relied on individual COUNTA counts to reshape OFFSET. There is no guarantee that there isn't an interim blank row or that they each continue down to the same row. You could homogenize the originals with,
=OFFSET('Enquiry Log'!$J$2, 0, 0, COUNTA('Enquiry Log'!$A:$A), 1)
... but that still doesn't guarantee that interim blank cells in column A wouldn't skew results.

Related

Array to lookup multiple columns and take another columns information

I am trying to lookup a part number in a separate table array, the same value could be in multiple rows. The formula should find each instance of the value in the other table, move to a date column and determine which months the value exists in and which row it belongs to. The entire table B3:F15 should be automated. I have attached an image with proper explanation of what I am trying to accomplish if at all possible.
The formulas would be entered into B3:F15. These are the cells where the data will get pulled into. Each row will search for its corresponding Part Number in column A. I.e, B3 to F3 is only looking for A3 data. The rows are used to determine if a Part number in L2:P4 matches their row value in Column A and if was used in that month belonging to months B2:F2. If the Part number was used in the corresponding month then it will output the Plant # from Column I. In some cases the Part numbers in L2:P4 can exist in multiple rows, but they are unique to each column.
As an example I have manually filled in data for PN1001 and PN1021. For PN1001, this belongs to B3:F3. Each cell in B3:F3 will search the array L2:P4 and realize it is found only once and in M4, so it will look at the dates from its row, J4 and K4, and determine which months it was used in. Since it was used in Jan, Feb, and Mar, it will output the Plant # into D3, E3, F3. In the example of PN1021 it is the same but in this case PN1021 exists in L2 and L4, so it will take the date of J2,K2, and J4,K4.
Some Part numbers can be a combination of letters and numbers, some may just be numbers.
In terms of how to build the formula I am not sure what combination of Index/Match/Lookup I should be using. My theory is that each cell in B3:F15 would include a formula that be =MATCH(Lookup(columnA with L3:P4)) (but how do you get it to lookup multiple instances??) Some research lead me to use this concept to find multiple instances and point to the row #.
SMALL(IF(Lookup Range = Lookup Value, Row(Lookup Range),Row ()-# of rows below start row of Lookup Range)
Once I can find each instance, then Index the row and compare if the cell in B3 is greater than J3 and less than K3 to determine the months which should have a value entered. IFYES, then output column I from the row number.
If you have any better solutions or ideas to perform this action then please provide feedback. Thanks in advance for the support.
I think the formula below will do what you want. I created a table out of your I1:P4 data, and used structured references, as I find it easier to follow.
B3: =IFERROR(
INDEX(partsTbl,
MAX(
($A3=partsTbl)*
(B$2>DATE(YEAR(partsTbl[[Test Start Date]:[Test Start Date]]),MONTH(partsTbl[[Test Start Date]:[Test Start Date]]),0))*
(B$2<DATE(YEAR(partsTbl[[Test Finish Date]:[Test Finish Date]]),MONTH(partsTbl[[Test Finish Date]:[Test Finish Date]])+1,1))*
ROW(partsTbl)-ROW(partsTbl[#Headers])),
1),
"")
$A3=partsTbl returns a 2D array like {FALSE,FALSE,TRUE,FALSE;FALSE,FALSE, …} to find the part number in the table
Since your start/finish dates in the partsTbl are not always at the beginning/end of the month, we need to convert them to such before we compare the dates in B2:F2
(B$2>DATE(YEAR(partsTbl[[Test Start Date]:[Test Start Date]]),MONTH(partsTbl[[Test Start Date]:[Test Start Date]]),0))
And similar for the end of the month.
We now have a 2D-array of {FALSE,FALSE,TRUE,FALSE...} where the TRUE matches the part number as filtered by the dates.
ROW(partsTbl) returns a matching array of the row numbers in the table (and we subtract the row number of the Header row to determine the row within the data).
When we do our multiplication, we then get an array where the largest value will be the desired row
INDEX, referencing column 1, will then return the appropriate Plant #

i want to find count of text for a date range as criteria 1 and criteria 2 in the array

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))

OFFSET / INDIRECT function trouble

I have two sheets within a workbook, the first with several thousand lines of expenses, separated by individuals, and the second a summary of totals and such.
On the second sheet, I've created a reference to the first to insert each individual's name (i.e. B4: ='Card Transactions'!D89). I'm having difficulty with the syntax for returning the total of each individual's total, which is in a predictable cell in the first sheet relative to the name (down 1, right 7).
I've tried the following:
=offset(indirect(B4),1,7) with only a reference error in return. This seems like it should be relatively simple but I'm not having any luck. . . any suggestions?
use this:
=OFFSET(INDIRECT(MID(FORMULATEXT(B4),2,300)),1,7)
note:
this only works if the formula in B4 only contains the one cell reference.
This is a volatile function and will cause a noticeable lag in calculations if used too many times.
The following should work for you as long as your data follows these rules:
Your columns have headers
The names are all in the same column
And you are able to set the range with row numbers and not just full columns
Let's say your first sheet is set out like this:
And you want your second sheet like this:
And your sheets are named:
Sheet1
Sheet2
This is the formula in B2 of Sheet2:
=INDEX(Sheet1!$A$1:$H$9,MATCH(A1,Sheet1!$A$1:$A$9,0)+1,MATCH("Column 8",Sheet1!$A$1:$H$1,0))
And here's what it does:
Your index array is the entire blue area, this can be the whole sheet but can't be a full column reference, the row number must be specified. In this example, the index array is $A$1:$H$9 and the $ signs mean the range won't move when you drag down the formula, so they are important!
Your first match finds the row number, it uses the name (in this case 'bart') as the lookup value, and the purple area as the array. In this example the row array is $A$1:$A$9 and the row numbers must match the row numbers in the index array. The match has a "+1" at the end, so it will find the matching row, then add one row down to get your offset.
Your second match finds the column number, it will need to use the name of your column. In this example the column array is $A$1:$H$1 and the column letters must match the column letters in the index array.
Let me know if this doesn't fit your problem, I'm sure we can figure it out.
Thanks.

Excel Array Formula, Multiple criteria

I have a table in which multiple weeks of data is stored and I'm trying to return a value based on 2 criteria.
Column A of the data sheet contains the date the report was ran (Always on the same day of the week - 24/05/17, 31/05/17 etc)
I've managed to return the value of column H by using an array formula, based on a cell value (Date) in ''Issues Data Quality Overview'!$B$4' using the following formula:
{=IFERROR(INDEX('Issues Log'!$H$1:$H$20000,SMALL(IF('Issues Data Quality Overview'!$B$4='Issues Log'!$A$1:$A$20000,ROW('Issues Log'!$A$1:$A$20000)-MIN(ROW('Issues Log'!$A$1:$A$20000))+1,""), ROW(A2))),"")}
That returns a value such as "IID-10225-22".
Problem:
Now I need to look up that value in the same table, based on a date in another cell, and return column X. (essentially adding the returned value as a criteria).
In all honesty I'm lost as to how I'd do this.
#Matthew. I understand your formula gives the list of values in column H with column A values matching 'Issues Data Quality Overview'!$B$4 in an ordered list.
Does your Issues Log, column H have multiple occurrence of the same value? (example: IID-10255-22 can have a value in Issues Log, column A that is not the same value as 'Issues Data Quality Overview'!$B$4).
If not, it doesn't make sense to use the result as a lookup value to get column X, you can simply change the code to:
{=IFERROR(INDEX('Issues Log'!$X$1:$X$20000,...}
If it does have multiple occurrences and you want to get the first occurrence of the result in column H and get the value in column X, best to add a formula right next to your array formula and do a VLOOKUP.
I've scrapped the array formula, as it really slowed down the processing speed. Instead I've created a Unique ID (=IssueID&Date) and VLookup'd that.

Find data in whole sheet and return the value under specific header

I'm not really good with excel but I've already searched and tried lots of formulas but still can't get this done. Problem is I need a cell in Sheet1...
...to return the value of the data in Sheet2 based on the column header name. Since the values might change its column number i need to search for the header name itself to return the value. As shown on the screenshot below. the lookup value will be the number and country and return the value under price that corresponds to the row.
Use a MATCH function across the top row to return the column number.
=index(A:Z, aggregate(15, 6, row(1:999)/(A1:A999=9)*(B1:B999="US"), 1), match("price", 1:1, 0))
You may have to lock some row and column range references down as absolute with $ if you plan to copy or fill the formula to other locations.
The AGGREGATE¹ function provides the two column match for 9 and US.
¹ The AGGREGATE function was introduced with Excel 2010. It is not available in earlier versions.

Resources