Using COUNTIFS on Multiple Columns - excel

I have a table named "Input" that takes the form below, with the actual table extending to around 20 columns and over 1000 rows. The "Site" column on the left will regularly have repeat data, but it will never be the case that, for example, W01 appears twice in the same row - though Site01 may have W01 in twice, just in different rows.
Site | Item1 | Item2 | Item3
------------------------------------------
Site01 | W01 W02 S01
Site02 | W01 S02
Site03 | S01 W01
Site01 | W01
I then have another table called "Results" with all of the sites along the top row and all of the items along the leftmost column. The idea would be to have the table show how many times a site used a given item, so the result from the above table should be:
| Site01 | Site02 | Site03
---------------------------------
W01 | 2 1 1
W02 | 1 0 0
S01 | 1 0 1
S02 | 0 1 0
It seems relatively easy to do this over one column in the Input table using a COUNTIFS formula that takes the form:
=COUNTIFS('Input'!$C$4:$C$1000,'Results'!C$2,'Input'$D$4:$D$1000,'Results'!$B3)
Which can then be copied across the entire "Results" table.
I can use this approach over two or three columns by stringing together multiples of the above COUNTIFS with pluses, and just changing the next COUNTIFS to check 'Input'$E$4:$E$1000 in the second part of the COUNTIFS - but this quickly becomes unfeasible over a sheet with up to 20 rows - so I need a formula or VBA code which does exactly as this COUNTIFS approach does, but that scales over a much larger number of rows.

Switch to SUMPRODUCT:
=SUMPRODUCT((Input!$C$4:$C$1000=Results!C$2)*(Input!$D$4:$F$1000=Results!$B3))
Amend the F in the portion:
Input!$D$4:$F$1000
as desired.
Regards

Related

SUM columns on the same sheet based on conditions or SUMIFS from another sheet

Here is a small sample table
+--------+-------+--------+
| COL 1 | COL 2 | COL 3 |
+--------+-------+--------+
| abc123 | Total | |
+--------+-------+--------+
| abc123 | cat1 | 100.00 |
+--------+-------+--------+
| abc123 | cat2 | 200.00 |
+--------+-------+--------+
| def123 | Total | |
+--------+-------+--------+
| def123 | cat1 | 100.00 |
+--------+-------+--------+
| def123 | cat2 | 200.00 |
+--------+-------+--------+
In COL 3, IF COL 2 is "Total" I need to SUM everything in COL 3 for each row in COL1 that is the same. (EG. COL3 Total row should be 300.00 for abc123 and then 300.00 for def123) Otherwise if COL 2 is NOT "Total" I need to do SUMIFS('Sheet3'!N:N,'Sheet3'!A:A,Sheet2!A473,'Sheet3'!Q:Q,Sheet2!Q473)*Sheet4!$U$2)
How can I can I accomplish the first part of the SUM?
Edit:
I think my example is too rigid and appears like it is set.
Let me see if I can explain in more fluid terms. I will have to describe this some what in database terms. All of the columns are on one sheet for the purposes of the "Total" portion.
COL 1 is my partition. Each of the "ID's" in COL 1 consists of 57 rows. Within 1 of those 57 rows is "Total" in another column, in the example that is COL 2.
So I have a large table that in COL 1 there are say 5 different ID's with 57 rows for each ID resulting in 285 rows.
Now I had a sorting function that would likely make this whole thing easier, but that function is crashing excel and not sorting both required sorts ( https://techcommunity.microsoft.com/t5/excel/sort-function-causes-a-crash-and-does-not-perform-secondary-sort/m-p/1477123#M66205 )
I suppose if I can get the sorting function to stop crashing excel this becomes slightly easier as then "Total" is consistently placed in row 2, 58, 116, etc. and I can add up everything below it. Right now, because that sort doesn't work, I have to add up everything from COL 3 that is NOT assigned to "Total" in COL 2 and has the same ID in COL1.
So in the table above abc123 is 3 rows and I need to add up the two rows that are not total for abc123 and have the formula spit out 300 into COL 3 for total.
Then def123 needs the same treatment.
Here is the tough part: the sorting is inconsistent because the data comes from a Redshift query so it is random for each ID. The IDs themselves are in random order. I think I can get the sort for COL 1 to work without crashing excel, but the secondary sort with the custom order is crashing it.
One way to avoid the Circular Reference error when trying to Total a column is to use two Sums, one above and one below.
So, assuming that your Columns 1, 2 and 3 are A, B and C, and that data starts in Row 2 (Row 1 being a header), you need the Sum of cells above the current row:
SUMIFS(C$1:C1, A$1:A1, A2)
Plus the Sum of the cells below the current row:
SUMIFS(C3:INDEX(C:C, 1+COUNTA(A:A)), A3:INDEX(A:A, 1+COUNTA(A:A)), A2)
(Note that this will actually terminate one row above and below the dataset)
Put this together with an IF statement:
=IF(B2="Total", SUMIFS(C$1:C1, A$1:A1, A2) + SUMIFS(C3:INDEX(C:C, 1+COUNTA(A:A)), A3:INDEX(A:A, 1+COUNTA(A:A)), A2), EXISTING_FORMULA_HERE)
Alternatively, you could try writing an Array Formula to calculate the SUM directly, a bit like when using multiple conditions in a MATCH, something like this: (not enough information in the question to do this exactly)
=SUMPRODUCT('Sheet3'!N:N*(COUNTIFS(A:A,'Sheet3'!$A:A)>0)*(COUNTIFS(B:B,'Sheet3'!$Q:Q)>0))
(Sum of Sheet3!N:N when a row exists in the current sheet that matches columns Sheet3!A:A in Column A and Sheet3!Q:Q in Column B)
Note that working on Entire Columns with Array Formulae is quite slow, so you may want to limit those just to the Used Range

Check if a Cell Value is between Two Values using Vlookup

In Excel, I have a table as follows, which shows pricing based on volume.
If you buy up to 4 items per month, the unit price is $100, 5 to 8 is $90, 9 to 20 is $80, anything above 20 is $50.
A | B | C
----------------
1 | 4 | 100
5 | 8 | 90
9 | 20 | 80
21| 1000 | 50
I have my monthly purchase volumes in another column, say d:
D
--
3
6
2
4
3
10
7
7
10
2
I need to find the unit prices (C column values) based on this series falling between the values of A and B columns. I know I can use a compound if statement like =IF(AND(D$1>=A1,B1>=D$1),C1,0) ... but since my pricing table is actually much larger than my example, this approach becomes convoluted. How can I do this with a Vlookup in an elegant way?
I'd go with the following in E1:
=INDEX(C$1:C$4,MATCH(D1,A$1:A$4))
which, at worst should be just as fast as VLOOKUP but at best is much faster.
This can be done by dragging the following formula down to cover the full column D:
=LOOKUP(2,1/($A$2:$A$5<=D2)/($B$2:$B$5>=D2),$C$2:$C$5)
This will take each D value, compare with A and B, locate which bucket it falls into, and pull the C value. If not found, will return an N/A.
Here is an approach using SUMIFS:
=SUMIFS($C$1:$C$4,$A$1:$A$4,"<="&E1,$B$1:$B$4,">="&E1)

Distinct Values based on two columns in Excel

I must be tired because I feel like I've done this before, but just banging my head tonight.
Anyway, I have a data sheet. 10 columns 800 rows. ( an excel table if you will)
On a separate sheet I have various drop downs. I want to be able to have a drop down that is based on the first 2 selections the user makes.
So If in cell A2 user selects 10 and in cell B2 user selects 12 , I want to be able to filter the excel table for values in Column D, based on the 2 values selected beforehand.
I've looked at formulas and doing data validation. I'm using the data validation for cells A2 and B2, however they are just list.
I tried a 3rd data validation using an If statement, but that continues to fail.
datasheet could contain:
column1 |column2 | column3|etc...
10 | 12 | 22
11 | 13 | 23
11 | 33 | 23
10 | 12 | 25
11 | 13 | 24
10 | 12 | 26
What I think should work, something like:
if(A2=10, list1, list2)
My issue is list 1 is where I need to select distinct values from column 3 based on filtering items from columns 1 & 2, but I cant get it to work to save my life...
When I try to do the formulas as listed above, it barfs telling me I need a delimited list...
Just looking for pointers in the right direction. Like I said , I feel like I've done this before, just can't recall it tonight. Thanks in advance for any direction. Respectfully.

Pivot table to return the FIRST value in a range

Glad to be joining the forum.
My question deals with attempting to return the FIRST value that occurs over several columns of data, using a pivot table that is filtered within a narrow time range. My current pivot table works by counting values in each column over the time rows. However I'm really only interested in the FIRST value that I come across for each person. So the raw looks something like this:
Person|TimeValue|Variable1|Variable2
1 | 1 | 1 | 0
1 | 2 | 1 | 0
2 | 1 | 1 | 0
2 | 2 | 0 | 1
What I currently get for a pivot using a range of time1 to time 2 is
1 | |2 | 0
2 | |1 | 1
Clearly, the time range I select includes MULTIPLE values in the same column, leading to counts of >1. What I'm thinking is that there is a way to use the same time sorting, but count only the FIRST time a value occurs in that variable, so that the pivot reports only the first time a value occurs within the range for the variables of interest.
Is there a simple way, or am I going to have to do this in VBA?
Much appreciated for any and all help. This is my first more complicated attempt with the newer pivots.
This is probably not the problem you would want to solve using a pivot table. You could just use the VLOOKUP Excel function to solve this issue in a simple way. VLOOKUP will always return the first value in the lookup range that matches the lookup value.

Excel Formulas: Show total based on date entry

I've got a spreadsheet with two columns that represent the number of processed records, and the date the records were processed. In some cases, the records can be processed in multiple batches, so the document looks something like this:
33 4/1/2009
22 4/1/2009
12 4/2/2009
13 4/4/2009
36 4/4/2009
I'm trying to add a new set of columns that contain a date, and shows the total number of records for that date, automagically:
4/1/2009 55
4/2/2009 12
4/3/2009 0
4/4/2009 49
I know how to do this algorithmically, and I could probably manipulate the spreadsheet outside of Excel, but I'm trying to do this in the live spreadsheet, and am a bit bewildered as to how to pull it off.
Any ideas?
Thanks!
IVR Avenger
Will the SUMIF function work for you? SUMIF([range],[criteria],[sum_range]) I think you could set range = the set of cells containing dates in your first listing, criteria would be the cell containing the date in the second listing, and sum_range would be the counts in the first column of your first listing.
I would suggest using a Pivot Table. Put the dates into the row area and 'sum of' records in the data area. Nothing in the columns area.
A pivot table will be more dynamic than a formula solution because it will only show you dates that exist.
Assuming your dates are in column B and the numbers to be accumulated are in A, you could try something like this:
| A | B | C D
1 | 33 | 4/1/2009 | =MIN(B:B) | {=SUM(IF(B1:B5=C1,A1:A5,0))} |
2 | 22 | 4/1/2009 | =C1+1 | {=SUM(IF(B1:B5=C2,A1:A5,0))} |
3 | 12 | 4/2/2009 | =C2+1 | {=SUM(IF(B1:B5=C3,A1:A5,0))} |
4 | 13 | 4/4/2009 | =C3+1 | {=SUM(IF(B1:B5=C4,A1:A5,0))} |
5 | 36 | 4/4/2009 | =C4+1 | {=SUM(IF(B1:B5=C5,A1:A5,0))} |
Note the {} which signifies an array formula (input using Control-Shift-Enter) for any non-trivial amount of data it's heaps faster than SUMIF().
I'd be inclined to define dynamic names for the A1:A5 and B1:B5 parts, something like
=OFFSET(A1,0,0,COUNT(A:A),1)
so that I didn't have to keep fixing up my formulae.
There's still a manual element: adding new rows for extra dates, for example - that might be a good place for a little VBA. Alternatively, if you can get away with showing, for example, the last 90 days' totals, then you could fix the number of rows used.

Resources