Count specific columns based on multiple criteria and ranges of different sizes - google-sheets

I'm working with the following table:
I need a formula I can use on a different sheet that counts how many classes were cancelled for each of the groups like:
GE COL INT MIX W 2
GE COL INT 1 W 0
The criteria to decide if a class was cancelled or not is to type "C" in one of the columns for the class (i.e. January 2, meaning second class in January). In the example table you can see that the 4 people are part of 2 groups, 2 per group; if I write C for 2 of them in the same group I want the macro to only count 1 cancelled class instead of counting both "C"s.
All of the space where the "C"s will be written (Month columns) is a named range = "Attendance" and there's also a named range for the names of all groups = "Groups".
I'm using the following simple formula:
=COUNTIFS(METRICS!F:F,H5,Attendance,"C")
METRICS!F:F Refers to the sheet where the table I just showed you is and F is the column where the levels are. H5 is the cell in the other sheet where I'm comparing the group name to make sure it's the same. Attendance is the range where I'll be writing "C".
However, I get the error:
Array arguments to COUNTIFS are of different size.
And that would count all "C"s, not only the ones I need.

A simple solution would be to add a column after the group name that counts absences, and then use UNIQUE() over the group names and absence count to get a unique list. eg.
Alternately, to do this without the extra column, first create a unique list of group names with =UNIQUE(Groups).
Then add a named range called GroupAttendance that includes the ranges of both Groups and Attendance.
Lastly, fill the column next to the group names with:
=COUNTIF(FILTER(UNIQUE(GroupAttendance), UNIQUE(Groups) = A2), "C")
eg.

Related

How to count the number of contiguous blocks of cells, each block comprising of the same row values?

In Google Sheets, I have a sheet with a list of customers.
Row 1 has headers, and data starts in row 2.
Column A is Customer name,
Column B is street address,
Column C is City and Post Code,
Column D is Country.
I would like to count the number of occurrences of each customer's row, i.e. when A, B, C, D are the same as a composite key.
However, I want to count different occurrences of a row ONLY IF those occurrences are not adjacent / concurrent, i.e.
I do want to count separate occurrences if row 5 and 7 have the same customer,
but not if row 5 and 6 have the same customer...in this case I will count it as one occurrence
Sample sheet (Customers) with examples:
https://docs.google.com/spreadsheets/d/1J7WajZjJfl94tpgXXgk0y5ALCwG2PxoJw6poxwUyrU8/edit?usp=sharing
I have added explanations for counts in column N.
Say for example, you want to know the number of contiguous blocks whose column A value equals "O2 Arena", you can do
=countifs(FILTER(A2:A,A2:A<>A3:A),"="&A5)
It works because we want to omit rows where the value in column A is repeated in the next row. In other words, we keep those with different values than their next rows. Hence, A2:A<>A3:A.
If you want a list of counts for unique blocks, I recommend setting up the a list of the unique values first, ie. say in another sheet's A1, you have
=unique(Customers!A2:A)
then in B1, you can do
=countif(FILTER(Customers!$A$2:$A,Customers!$A$2:$A<>Customers!$A$3:$A),"="&A1)
and spread the above formula by double clicking the square on the lower right when you select B1.
The ranges in filter() should be absolute because the location of your data does not change. The range in the 2nd input of countif() should be relative because that is meant to iterate.
If values in column A does not uniquely identify your customers, you can add more columns to the input of filter() as required. For example, FILTER(A2:A,A2:A<>A3:A,B2:B<>B3:B)
For function usage, please consult official documentation by typing the function name in the search bar.

Sum range in Google Sheets of only unique values

I need a formula to sum a column of values. But I only want to sum the rows that have an ID that are unique among the entire range. See the example below where I have 6 rows, but there are only 3 unique IDs. My ideal outcome is a sum of 6 by adding one instance of UNIQUEID-00A, UNIQUEID-00B, and UNIQUEID-00C.
Notes:
A unique ID will always have the same value. For example, if UNIQUEID-00A were listed 1 or 100 times, the associated value will always be 1. So I don't need the formula to account for a scenario where the duplicate IDs have different values.
There may be instances where Column A (the ID) and Column B (the value) will not be sitting side-by-side. It might be that 2, 3, or more columns are in between the two columns.
The IDs and values could be 6 rows tall, or hundreds of rows tall. For that reason I'd like to use something like A2:A to target the IDs rather than having to specify a specific start and end point like A2:A7.
See the linked Google Sheet below for a copy of this.
ID
Value
UNIQUEID-00A
1
UNIQUEID-00B
2
UNIQUEID-00C
3
UNIQUEID-00A
1
UNIQUEID-00B
2
UNIQUEID-00C
3
https://docs.google.com/spreadsheets/d/1bU4J1RL5S0a_NvFjW_KVpiKi8603Tj9iVPeNzBlw-OA/edit?usp=sharing
Given the above table in A1:B7:
=sum(index(unique(A2:B7),,2))
UNIQUE returns the distinct rows of the range, INDEX (with column parameter = 2) to return only the value column, then SUM the result.
EDIT Based on the updated requirements, with the IDs in, for example, column A and the values to be summed in column C, use something like:
=sum(index(unique({A2:A,C2:C}),,2))
I'm assuming that there is no data below your input table. The UNIQUE here will return an extra blank row because of the unlimited range, but SUM will disregard it.
use:
=SUM(SORTN(B3:B, 9^9, 2, A3:A, 1))
sum B column
while returning all rows 9^9
that are unique 2
within A column
in whatever order 1

Combine / merge contents of columns in Google Sheet into a single column based on a condition

I have a spreadsheet with multiple columns. Each column represents a recipe. Each column has a week number (e.g. 2, 3, 4)... Below the week number, we have a variable number of ingredients. I want to be able to merge all the ingredients from a given week into a single column.
Sample Data
I know how to statically merge ranges e.g. {B4:B20;C4:C20} and I can also think of using an if statement to check the week value e.g. =if(B1=2,{B4:B20;C4:C20}) but that's not close to what I want. I need to check the range B1:Z1 for the value of the week and if that value is X (let's say 2) then take the ingredients in B2:B and tack them on to C2:C and so on...
If I'm understanding you correctly, this should do it:
=QUERY(FLATTEN(FILTER(B3:Z,B1:Z1=2)),"WHERE Col1 Is Not Null")
FILTER keeps (i.e., "filters in") only the data in B3:Z where the header is 2.
FLATTEN forms one column (blank cells and all) from the FILTER results.
QUERY keeps only non-blank (i.e., Is Not Null) entries from that single-column list.

Reference Specific Row in Named Range within another Named Range

I'm writing a spreadsheet to keep track of a small business' financials. They operate a few Rooms for rent, and the structure of the document is made so that each sheet holds a year's worth of booking for all the rooms.
Essentially, each row is defines a specific date, while each rooms spans a few columns (reason is that they don't just want to track whether or not a room is booked, but also record names of clients & other remarks), among which the daily calculated income (some factors alter the daily rate each room will generate).
So this is all fine and dandy, and I've created named ranges for each month of the year, and for each room.
For example, rows 6:36 will represent the month of January, while columns C:I will represent Room 1. Room 2 will span J:P and so forth.
Now, in another sheet, I wanted to make a dashboard which lists the earning for each room, per month. It's a very simple table with 12 rows (one for each month) and 10 columns (1 for each room) where I planned to sum up all the earnings.
So my issue is that I can't find a way to retrieve a specific column of a named range for a room ('vertical named range'), which is also limited in a named range for a month ('horizontal named range'). I had read about using ARRAYFORMULA(INDEX(named_range, ,wished_column)) but that only works for a single named range. My knowledge of these two functions being non-existent, I didn't manage to extend it to a 2-named-range version...
(I mean I did try something along the lines of ARRAYFORMULA(INDEX(January, , INDEX(Room1, , 3))) but that didn't work)
So because there isn't a one-to-one relation from the Dashboard cells to the Rooms cells, my current only solution is to manually reference everything, which you'll understand is inefficient and time-consuming...
My question, in fine, is: How can I retrieve a range that results of the intersection of 2 (or more) named ranges ? Once I have that resulting range, I know it will be very easy to use INDEX().
Define a named range Base as
A:Z
Define a range named Horizontal as
6:36
Define a range named Vertical as
C:I
Then the intersection of the vertical and horizontal ranges is given by:
index(Base,row(Horizontal),COLUMN(Vertical)):index(Base,row(Horizontal)+rows(Horizontal)-1,COLUMN(Vertical)+columns(Vertical)-1)
This can be verified by using it in a function e.g.
=countblank(index(Base,row(Horizontal),COLUMN(Vertical)):index(Base,row(Horizontal)+rows(Horizontal)-1,COLUMN(Vertical)+columns(Vertical)-1))
gives the result 7 * 31 = 217 in my sheet because I haven't filled in any of the cells.
The Offset version of this would be:
=countblank(offset(A1,row(Horizontal)-1,COLUMN(Vertical)-1):offset(A1,row(Horizontal)+rows(Horizontal)-2,COLUMN(Vertical)+columns(Vertical)-2))
or more simply:
=countblank(offset(A1,row(Horizontal)-1,COLUMN(Vertical)-1,rows(Horizontal),COLUMNS(Vertical)))
So this works well in OP's case where you have two fully overlapping ranges like this:
Partial Overlap
Suppose you have two partially overlapping ranges like this:
You can use a variation on the standard overlap formula (This is one of the early references to it as used with a date range)
max(start1,start2) to min(end1,end2)
So the previous formula becomes
=countblank(index(Base,max(row(index(Partial1,1,1)),row(index(Partial2,1,1))),max(COLUMN(index(Partial1,1,1)),column(index(Partial2,1,1)))):
index(Base,min(row(index(Partial1,1,1))+rows(Partial1)-1,row(index(Partial2,1,1))+rows(Partial2)-1),min(COLUMN(index(Partial1,1,1))+columns(Partial1)-1,column(index(Partial2,1,1))+columns(Partial2)-1)))
and the offset version is
=countblank(offset(A1,max(row(offset(Partial1,0,0)),row(offset(Partial2,0,0)))-1,max(COLUMN(offset(Partial1,0,0)),column(offset(Partial2,0,0)))-1):
offset(A1,min(row(offset(Partial1,0,0))+rows(Partial1)-2,row(offset(Partial2,0,0))+rows(Partial2)-2),min(COLUMN(offset(Partial1,0,0))+columns(Partial1)-2,column(offset(Partial2,0,0))+columns(Partial2)-2)))
I have tested this on ranges C2:F10 and D3:G11 which gives the result 24 as expected.
However, if there is no overlap, this can still give a non-zero result, so a suitable test needs adding to the formula:
=if(and(max(row(index(Partial1,1,1)),row(index(Partial2,1,1)))<=min(row(index(Partial1,1,1))+rows(Partial1)-1,row(index(Partial2,1,1))+rows(Partial2)-1),
max(column(index(Partial1,1,1)),column(index(Partial2,1,1)))<=min(column(index(Partial1,1,1))+columns(Partial1)-1,column(index(Partial2,1,1))+columns(Partial2)-1)),"Overlap","No overlap")
Perhaps the best approach in Google Sheets is to go back to the full version of the Offset call OFFSET(cell_reference, offset_rows, offset_columns, [height], [width]) . Although this is rather long, it will return a #Value! error if there is no overlap:
=Countblank(offset(A1,
max(row(offset(Partial1,0,0)),row(offset(Partial2,0,0)))-1,
max(COLUMN(offset(Partial1,0,0)),column(offset(Partial2,0,0)))-1,
min(row(offset(Partial1,0,0))+rows(Partial1),row(offset(Partial2,0,0))+rows(Partial2))-max(row(offset(Partial1,0,0)),row(offset(Partial2,0,0))),
min(COLUMN(offset(Partial1,0,0))+columns(Partial1),column(offset(Partial2,0,0))+columns(Partial2))-max(COLUMN(offset(Partial1,0,0)),column(offset(Partial2,0,0)))
))
Notes
Why did I have to introduce some more indexes (indices?) in the second formula to make it work? Because if you use the row function with a range in an array context, you get an array of row numbers which isn't what I want. As it happens, in the first formula you are not using it in an array context, so you just get the first row and column of the given range which is fine. In the second formula, Max and Min try to evaluate all the rows in the array, which gives the wrong answer, so I have used Index(range,1,1) to force it to look only at the top left hand corner of each range. The other thing is that both index and offset return a reference, so it is valid to use the construct Index(...):Index(...) or Offset(...):Offset(...) to define a new range.
I have also tested the above in Excel (where as mentioned the Index version would be preferable). In this case Base would be set to $1:$1048576.
Although in Excel you have the Intersect Operator (single space) so it's not necessary to use an Index or Offset formula at all e.g. the first example above would simply be:
=COUNTBLANK(Vertical Horizontal)
and if there is no overlap the formula returns a #NULL! error.
"I've created named ranges for each month of the year, and for each
room. For example, rows 6:36 will represent the month of January,
while columns C:I will represent Room 1. Room 2 will span J:P and so
forth."
What I suggest is that if "January" is defined for columns C to whatever (the last column of the last room), then that's all you need.
You haven't shown us the layout of the dashboard. But let's assume that at the very least you're interested in the income generated by each room.
=query({January},"select sum(Col3) label sum(Col3)'' ")
In this image, the range called "January" is highlighted. Note that it does NOT include the header. Note also that it can be many columns wide; in this example, I've just made up a few columns, but your range should cover all the columns for rooms 1 to n.
Syntax: QUERY(data, query, [headers])
Data: This formula queries the range called "January". That range can be on the same sheet, on on another sheet (such as your Dashboard). Reminder: in this screenshot, "my version of "January" is highlighted.
Query to count Number of People: "select sum(Col3) label sum(Col3)'' "
Query to sum the income earned: "select count(Col2) label count(Col2)'' "
Col2 & Col4 = Number of People for Room#1 and Room#2 respectively.
Col3 & Col5 = Income for Room#1 and Room#2 respectively.
[headers]: You can ignore them.
This formula delivers just the value of the query; even though it includes a "label", the label will not print.
Modify and adapt these formulae to create the other information required for your Dashboard.

Google Sheets Formula: List Column A input if Column G input is equal to "X"

I am interested in finding a formula that will allow me to make lists of cell input from Column A, but only list the cells that have a certain value in the same row in Column G.
Here's more information: I am doing rosters for my school. Each row is a different student, their name entered into Column A. Columns G through J are their assigned classes on certain days. I would like to pull lists of students who have certain classes on specific days (Tuesday through Friday). For instance, I would like a formula that allows me to make a list of all students (Name from Column A) whose class listed in column G is "Art." So basically I want to pull daily class rosters.
Any help would be greatly appreciated by me and all of my teacher colleagues!
Thanks!
=FILTER (A1:J200,G1:G200="Art")
Or
= FILTER ({A1:A200,G1:G200},G1:G200="Art")
You're looking for this function FILTER
You could also use QUERY
=QUERY (A1:J200,"Select A,B,C,D,F,G,J Where G contains 'Art'")

Resources