SumIfs in Excel - excel

I have a requirement to sum all the cells in a column from a sheet called Raw Data. But only if a few conditions exist.
The value in Raw Data column B matches the value of a offset from the current cell
The Month of the date in Raw Data Column C is less than or equal to H2
The Year of the date in Raw Data Column C is less than or equal to H2
Here is the formula I have assembled thus far:
=SUMIFS('Raw Data'!$D:$D,'Raw Data'!$B:$B,OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0,-4),MONTH('Raw Data'!$C:$C),<=MONTH($H$2),YEAR('Raw Data'!$C:$C),<=YEAR($H$2))
It could be a simple syntax error, but I don;t think it is. I keep getting a generic "There is a problem with the formula" error.
Can I get some help?

With SUMIFS the range expects a range not an array so MONTH('Raw Data'!$C:$C) and YEAR('Raw Data'!$C:$C) will not work.
Also when the others are expecting a string so you must use quotes and concatenate the criteria: "<=" & value
In this case we want everything less than the 1st of the next month:
"<" & EOMONTH($H$2,0)+1
Use this:
=SUMIFS('Raw Data'!$D:$D,'Raw Data'!$B:$B,OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0,-4),'Raw Data'!$C:$C,"<" & EOMONTH($H$2,0)+1)

Related

Vlookup with specific date showing N/A

I am trying to apply vllokup for a specific month of a specific year. My formula works for string lookup value but not works for date. here I attached my effort
First of all VLOOKUP() will not work in this case because VLOOKUP() always lookup values on first column of table then return value from corresponding row from specified column. So, you first column is strings and vlookup do not find dates on first column. Use Index/Match instead.
=INDEX(B3:B5,MATCH(D1,C3:C5,0))
You can also use FILTER() function.
=FILTER(B3:B5,C3:C5=D1)
Query() will also work-
=QUERY(B3:C,"select B where C= date '" & text(D1,"yyyy-mm-dd") & "'")
Vlookup works by searching columns to the left of the searched column. So you should modify your sheet to look like this (if possible) :
However, you should also take into account that the formatting of your cells could be misleading. If your D4cell has the value 1/Jan/2022 and your C6 cell has the value of 2/Jan/2022, then both of them will show as Jan 2022, but your formula will still return N/A because the real values of the cells are different.

Use VLOOKUP with split ranges

I'm making a formula that looks up a value and returns a value, fairly simple procedure as I can use VLOOKUP or INDEX-MATCH. However, I cannot seem to create or find a way to make it work when the ranges are located side by side. (Will provide example)
I know I could move all the data into two columns, but that would make it Very long and the table is often looked at for reference. I would like to keep the table the way it is.
What I am looking for is that I can type the tank level(inches) into a cell and next to it it will return the tank level(gallons).
My current formula is
=INDEX('Calib. A & B'!$B$3:$B$47,MATCH(TANKS!$E$2,'Calib. A & B'!$A$3:$A$47,1))
Currently it compares cell E2 with A3:A47 and returns the corresponding value in B3:B47. It needs to search columns C, E, G, I, etc. Any ideas other than rearranging the spreadsheet?
There are different ways of obtaining the relevant rows/columns for the lookup:
=INDEX('Calib. A & B'!$A$3:$U$47,AGGREGATE(15,6,1/((TANKS!$E$2='Calib. A & B'!$A$3:$U$47))*ROW('Calib. A & B'!$A$3:$U$47)-2,1),AGGREGATE(15,6,1/(TANKS!$E$2='Calib. A & B'!$A$3:$U$47)*COLUMN('Calib. A & B'!$A$3:$U$47)+1,1))
The -2 for the rows argument of the index function is to adjust for the header position.
The +1 for the columns argument is to return the value one column to the right of the cell that matched.
This assumes, of course, that the values matching the lookup_value in E2 are only found in the "odd" columns of the data range.
As suggested by #TerryW, if your measure in E2 is not an exact match to the value in the data table (and the data table measurements are exact to the 1/8), you can roundup E2 to the next 1/8th.
Replace TANKS!$E$2 with ROUNDUP(TANKS!$E$2*8,0)/8
One way is with a long formula basically doing a union of results of the match on each column but having result as empty string when do not match it.
So we do a match against column A, if it doesn't find it it returns a blank string, if it finds it returns the number , then does the same for column C and concatenates the results and uses this on the index.
BUT this will only work if you search for an exact match.
For example:
=INDEX('Calib. A & B'!$B$3:$B$47,IF(ISNA(MATCH(TANKS!$E$2,'Calib. A & B'!$A$3:$A$47,0)),"",MATCH(TANKS!$E$2,'Calib. A & B'!$A$3:$A$47))&IF(ISNA(MATCH(MATCH(TANKS!$E$2,'Calib. A & B'!$C$3:$C$47,0)),"",MATCH(MATCH(TANKS!$E$2,'Calib. A & B'!$C$3:$C$47)))
You would need to add one more section for each column.

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

Google spreadsheet - Sum data of array within date range match row and column

My data looks like this:
I wanted to sum a range that match 3 conditions
Country
DateRange >= Date1 and DateRange <= Date2
Tried index match for only one date and it worked, but for date range the formula keeps giving error.
IF you have the date range defined in cells J1 and K1, then this should work, provided the Dates are of the same Format (all dates or all text, etc):
=sum(INDIRECT(ADDRESS(2, MATCH(J1,B1:G1, 0)+1, 4) & ":" & REGEXEXTRACT(ADDRESS(2, MATCH(K1,B1:G1, 0)+1, 4), "[A-Z]+")))
INDIRECT() allows us to define a range from text. This uses a combination of ADDRESS() and MATCH() to build the first part of the range and it uses REGEXEXTRACT() to remove the row reference in the second part of the range.
Breaking this down from the inside, assuming J1 = 2017-07-28 and K1 = 2017-07-30, we have the first MATCH returning a value of 2 for the second column in the range B1:G1 matching the contents of J1:
MATCH(J1,B1:G1, 0)
Since we are starting our column count at column B we need to add one to that, making the associated ADDRESS evaluate to C2 as the function is looking for the second row, third column and the 4 tells it to return a relative value and therefore not include the $ characters:
ADDRESS(2, 2+1, 4)
The second use the the ADDRESS() and MATCH() functions is surrounded in REGEXEXTRACT() to remove the Row reference and evlauates down as follows
REGEXEXTRACT(ADDRESS(2, MATCH(K1,B1:G1, 0)+1), "[A-Z]+")
to
REGEXEXTRACT(ADDRESS(2, 4+1), "[A-Z]+")
to
REGEXEXTRACT("E2", "[A-Z]+")
Where this returns only the characters A-Z as many times as they occure, so AZ will be returned if ADRESS() evlautaes to AZ2.
This leaves us with the INDIRECT() which now becomes:
INDIRECT("C2 & ":" & "E")
or
INDIRECT("C2:E")
Allowing us to sum the range matched by the two values.
This can be done fairly easily by using INDEX/MATCH to find the row that matches the country, then SUMIFS to sum the amounts which are in the date range
=SUMIFS(INDEX($B$2:$H$10,MATCH(C13,$A$2:$A$10,0),0),$B$1:$H$1,">="&A13,$B$1:$H$1,"<="&B13)

SUMIFS in Excel with cell reference

I'm trying to sum the values in sheet GLTB column D where the value in column A starts with 2.21 and the value in column E is equal to the date in the cell A1. I tried this formula:
=SUMIFS(GLTB!$D$3:$D$26522,GLTB!$A$3:$A$26522,"2.21*",GLTB!$E$3:$E$26522,GLTB!$A$1)*-1
The problems:
The date in A1 doesn't appear anywhere else on the GLTB sheet, so I should get 0 for a sum, but I don't. I get some number that doesn't correspond to anything I can find.
I can make all the values in column D where column A starts with 2.21 equal to 0 (or any other number) and it has no effect on the result of the formula.
I tried this formula based on answers to other questions:
=SUMIFS(GLTB!$D$3:$D$26522,GLTB!$A$3:$A$26522,"2.21*",GLTB!$E$3:$E$26522,GLTB!"="&$A$1)*-1
This just changes the last criterion reference. However, Excel gives me a formula error response.
Any ideas?
This should get you a little closer:
=SUMIFS(GLTB!$D$3:$D$26522,GLTB!$A$3:$A$26522,">=2/21/16",GLTB!$E$3:$E$26522,GLTB!$A$1)*-1
This assumes that column A contains date values which are formatted as string. If these values are not date values, then please give some example of what cell contents in column A.
Note: I'm not clear on why you're searching for "2.21*" so I used the >= operator. That can be changed, or we could add additional criteria to the SumIfs based on your needs.
Other things to be careful of:
Ensure that column E and cell A1 both contain the same type of data (either date formatted as string, or string data representing dates. If you have inconsistent data types, the equivalence test in your Criteria2 (GLTB!$E$3:$E$26522,GLTB!$A$1) will not return the desired results.
To start with, you should format your data as table. To do this, use "Start > Format as table". This will also give you the option of giving your table a name, e.g. data. Basically this makes performing the following steps easier.
From then on, you don't need to reference with GLTB!$A3:$A26522anymore, but can use data[ColumnA].
I'm assuming you have a column ValueA with something in there that might start with 2.21, then ValueD which you want to sum and DateE which should be equal to a certain date.
The formula you are looking for is not SUMIF, but the much better SUMPRODUCT, as this allows you to check multiple conditions.
The final formula will look like this:
=SUMPRODUCT((data[DateE]=A1)*(LEFT(data[ValueA],4)="2.21")*data[ValueD])
Now what does this do. SUMPRODUCT first builds a product and then sums that up. Let's assume the following values:
A1 = 2016-03-01
DateE = 2016-03-01
ValueA = 2.213454
ValueD = 3
The formula will then do the following:
(2016-03-01=2016-03-01) is 1
("2.21" = "2.21) is 1
3 is 3
1*1*3 = 3
Now if you change the date in A1, the value of your SUMPRODUCT cell should change accordingly.
As a starter I recommend using Tables - it's much easier to read and maintain your data: Insert-->Table.
e.g.
Here I have "Table1", with the Date column formatted as a Short Date.
Name Date Quantity
Alpha 17-Mar-16 1
Beta 15-Feb-16 2
Charlie 11-Mar-16 3
Dog 11-Feb-16 4
Echo 9-Feb-16 4
Foo 6-Jan-16 5
Then in a separate row/cell, you can input a formula to sum the quantity column where the data is from 1st Feb onwards.
Here I've used a different date format, but Excel is fine with it.
Feb Onwards Total: 14 =SUMIFS(Table1[Quantity],Table1[Date],">=01-Feb-16")
Feb Only Total: 10 =SUMIFS(Table1[Quantity],Table1[Date],">=01-Feb-16", Table1[Date],"<29-Feb-16")
Try something like this:
=SUMPRODUCT((INT(GLTB!$E$3:$E$26522)=INT(GLTB!$A$1))*(ROUNDDOWN(GLTB!$A$3:$A$26522,2)=2.21)*(GLTB!$D$3:$D$26522))
If this Returns and error, then one of two things:
your dates are not true dates but text that look like dates.
your have errors in your cells in some of the columns.

Resources