How do I sum values if both column, row and cell value match conditions? - arrays

I have two sheets: flight control and total hours control. Link to sheets.
Flight Control:
Total Hours:
What I want: On Total Hours, Sum the Flight Time if:
Column Name(L.D.1) matches row on Line; row Pilot matches row Pilot; cell has a value of 1.
I'm using this for cell C2, and it works:
=SUMIFS('Flight Time'!B:B;'Flight Time'!A:A;B2;'Flight Time'!C:C;"1")
And it works, giving me this:
However, this isn't very practical. I have to redo the formula every time a new pilot is introduced. And if I move columns around, it stops working.
What I need: To base my conditions on a text search. Something like:
If column name in {Flight Time!C:F} matches string on {row in col Line}, and Pilot = Pilot, and cell value = 1, SUM
In other words, I need to scale this. The final product will have dozens of pilots and L.D.s, so I need to be able to move things around.

My answer is similar to player0's but it anticipates a Flight Time tab that expands infinitely sideways as well as down.
You would start a new tab and place this formula in cell A1.
=ARRAYFORMULA(QUERY(SPLIT(TRANSPOSE(TRIM(SPLIT(QUERY(TRANSPOSE(QUERY('Flight Time'!C1:1&"|"&'Flight Time'!A2:A&"|"&OFFSET('Flight Time'!C2;;;ROWS('Flight Time'!C2:C);COLUMNS('Flight Time'!C2:2))*'Flight Time'!B2:B&CHAR(10);;9^99));;9^99);CHAR(10);0;0)));"|";0;0);"select Col1,Col2, SUM(Col3) where Col3>0 group by Col1,Col2 label Col1'Line',Col2'Pilot',SUM(Col3)'Total Hours'"))
Sometimes in complicated formulas like this, it can be difficult to translate to your real sheet if you haven't placed the sample data in exactly the same layout on the sample as it is on your real sheet. If that is the case here, change the layout of the sample so that it better matches your real data, and I can try to update the formula.

use:
=ARRAYFORMULA(SPLIT(
TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(
IF(C2:F="",,"♠"&C1:F1&"♦"&A2:A&"♣♥"&B2:B),,999^99)),,999^99), "♠")), "♥"),
"select Col1,sum(Col2) group by Col1 label sum(Col2)''")),,999^99)), "♦♣"))

Related

How to use COUNTIFS based on multiple criteria over different sized ranges?

I have an attendance tracker in which I'm trying to account for each employee type per day.
I have a summary page (sheet1) in which I want the count of each type (A,B,C,D,E) based on the day in cell C2 whether or not they have an X on sheet2 for that day.
Using =COUNTIF(FILTER(Sheet2!$A$5:$GG$969,Sheet2!$A$5:$GG$5=$C$2),"X") I am able to get a total of "X" based on the date in cell C2.
However, I'm having trouble getting the formula to work counting each employee type.
=COUNTIFS(Sheet2!A5:AM31,A7,(FILTER(Sheet2!A5:AM31=C2)),"X")
This gives me an error "Array arguments to COUNTIFS are of different size."
I'm not sure how else to configure this. Below is a link to my sample sheet:
Appreciate any help!
https://docs.google.com/spreadsheets/d/1OdJTwbFsNcR1hO1qzMBGUY4iXcWgDwIAJmBAVE9cs0k/edit?usp=sharing
I think this would work, dragged down for the other groups.
=COUNTIFS(Sheet2!A$6:A$31,A7,FILTER(Sheet2!$6:$31,Sheet2!$5:$5=C$2),"X")
However, I would encourage you to look at this sample sheet (a copy of yours) where I've added a new tab called MK.Help, designed to be hidden, but used as a sort of helper tab to make all sorts of things you might like to do easier. Including the count you asked about.
It has one single formula in cell A2 where you can see what it does.
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(Sheet2!C5:5&"|"&Sheet2!A6:A31&"|"&Sheet2!B6:B31&"|"&Sheet2!C6:31),"|",0,0),"where Col3<>'' order by Col1"))
try:
={"", ""; QUERY(FILTER(Sheet2!A6:A31, FILTER(Sheet2!6:31, Sheet2!5:5=C2)="X"),
"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1)''");
"Total", COUNTA(IFNA(FILTER(Sheet2!A6:A31, FILTER(Sheet2!6:31, Sheet2!5:5=C2)="X")))}

How do you use ArrayFormula with arrays (after aggregation)?

In my example:
https://docs.google.com/spreadsheets/d/1QQNTw_r9-q-FqVNwUoYklup73niZCFyO0VDUYImP5fo/edit?usp=sharing
I'm using Google Forms as an eBay clone to sell rare items. Each bid is outputted from the form to the "Data" worksheet and then I have ArrayFormulas set up inside the "Processed" worksheet. The idea is that I want to process the bids so that we filter everything except the items with the highest bids. All data should be automatically updated, hence why I want to use ArrayFormulas.
My strategy is that in colum A, I first filter all unique items (=unique(filter(Data!A2:A,Data!A2:A<>""))) and end up with:
Jurassic Park 6-Pog Hologram Set
Princess the Bear TY Beanie Baby
Holographic 1st Ed Charizard
However, then in column B, we have to find the highest bid that corresponds to that unique item, e.g.:
=IF(ISBLANK(A2),,ArrayFormula(MAX(IF(Data!A2:A=A2,Data!B2:B))))
However, I don't want to have A2 be a single cell (A2) but an array (A2:A) so that it doesn't have to be manually copied down the rows. Similarly, I also want columns D and E to be automatic as well. Is there any way to achieve this?
Not sure if it would be considered easier than the previously posted answer, but in case this thread is found in the future, I think that this is a slightly simpler way to solve these kinds of problems:
Try this on a fresh tab in cell A1:
=FILTER(Data!A:D,COUNTIFS(Data!A:A,Data!A:A,Data!B:B,">"&Data!B:B)=0)
I did some research and found an answer very similar to what you were looking for. After rearranging the formula slightly to match your sheet, I was able to get this to work:
=ArrayFormula(vlookup(query({row(Data!A2:A),sort(Data!A2:C)},"select max(Col1) where Col2 <> '' group by Col2 label max(Col1)''",0),{row(Data!A2:A),sort(Data!A2:D)},{2,3,4,5},0))
This formula automatically populates product name, highest bid, username, and timestamp. I ran some tests, adding my own random names and values into the data sheet, and the formula worked great.
Reference: https://webapps.stackexchange.com/a/75637
use:
={A1:D1; SORTN(SORT(A2:D, 1, , 2, ), 9^9, 2, 1, )}
translated:
{A1:D1} - headers
SORT(A2:D, 1, , 2, ) - sort 1st column then 2nd column descending
9^9 - output all possible rows
2 - use 2nd mode of sortn which will group selected column
1 - selected column to be marged based on unique values

EXCEL: Create Array Formula out of INDEX/MATCH with multiple results

my aim is to convert a massive excel sheet with different projects, employees and hours worked per month into an overview per employee. It should be possible to display the projects the employee is involved in and how many hours he worked per project per month.
The original sheet looks something like this:
I managed to find the projects Person A worked in by filtering through the INDEX/MATCH function. I applied the formula to the whole row where the employees are listed and receive multiple results of projects. My question is how to transform the formula into something more effective to copy all of the matched results (projects) into a column (see 1).
This is what I have so far, if matches the employee name in a certain area; the output is the first match of the project he is involved in:
=INDEX(B2:J3;1;MATCH("Person A";Sheet1!B3:E3;0))
How can I copy this to the bottom cells to copy all of the matched results? Does it help to create an array formula with this?
You can use he following formula in cell B9:
=IFERROR(INDEX($2:$2,SMALL(IF($3:$3=$B$8,COLUMN($3:$3)-COLUMN(INDEX($3:$3,1,1))+1),ROWS(A$1:A1))),"")
It indexes row 2 and looks for the column number of the first match in row 3 that equals the value in B8 (=Person A). When dragging down it will look for the second match ROWS(A$1:A1) will become ROWS(A$1:A2) = 2.
For Person B you can use this formula in cell B14:
=IFERROR(INDEX($2:$2,SMALL(IF($3:$3=$B$13,COLUMN($3:$3)-COLUMN(INDEX($3:$3,1,1))+1),ROWS(A$1:A1))),"")
I hope this is what you where looking for.
PS
if you paste the following formula in cell C9 you will get the sum result for Person A on Project XY in month 10 2019:
=IF(OR($B9="",C$8=""),"",SUMPRODUCT(($B$2:$K$2=$B9)*($B$3:$K$3=$B$8)*($A$4:$A$6=C$8),B4:K6))
Note: That is provided that the value in cell C8 equals the value in cell A4.

How can I get text from a cell using the select statement of the query function in Google Sheets?

I try to get some data from tab A to tab B using the query function. This all works fine untill I include some data of tab B into the object in the query function.
The thing is, I want to create a scoring sheet that calculates the constructor points based of the first sheet with the driver standings. So tab A looks something like this:
Peter Wright - Team Fast Racers - 85
Cameron Bright - Team Quickies - 60
Marc Feiner - Team Fast Racers - 20
In this example the number correspond to a total amount of points on the right column.
The second tab needing to calculate the constructor points, looks simply like this:
Team Fast Racers 105
Team Quickies 60
Don't look at the amounts. It is just an example.
Now I want to use the query code where I can ask Google Sheets to grab the constructor name from tab B (i.e. Team Fast Racers) and look it up in tab A to get the score from both drivers and sum it up to get the total. This is the syntax now:
=QUERY({'Driver Standings'!$A$4:$T$32; 'Constructor Standings'!A1:C16}; "SELECT T WHERE C = 'B2'", 1)
The problem is that it does not recognize B2 to get the 'Team Fast Racers' name and search for it in tab A. How can I fix this problem.
Again, 2 drivers are in a team and those scores need to be summed up and displayed in the second tab to get a clean constructor championship standings. How can I get the data from tab A based on searching for a string out of tab B column B.
And if that is all done, I would also like the constructor standings (tab B) to be sorted based on amount of points, without losing the structure of the formulas.
first of all, all ranges in { } needs to be of the same size (at least in one dimension). if you are stacking ranges one under another then amount of columns needs to be the same:
{Sheet1!A:C; Sheet2!A:C}
then when you have a "constructed range" as above, you need to use Col references in QUERY statement
so instead of
"select A,C"
it needs to be
"select Col1,Col3"
then if B2 is a numeric number it will be
"where Col1 = "&B2
but if it is a plain text it will be:
"where Col1 = '"&B2&"'"
UPDATE:
tho all you need is:
=QUERY({'Driver Standings'!C4:C\'Driver Standings'!T4:T};
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
order by sum(Col2) desc
label sum(Col2)''")

SumProduct multiple criteria with text

I have a bank register on the left. I want a code on the right to tell me sum all the times the description "Rent" is paid in that month. I've tried index, sumproduct and sumif. I can't find the correct way to have it search by month, year and text.
It isn't clear what format your tables are in.
SUMIFS is the easiest solution but it will only work if the bank register uses the Excel date format. It gets a little complicated if those dates are formatted as text.
Building a SUMIFS formula begins with the SUM RANGE. This is a welcome change away from the backwards construction and wrongly named SUMIF (looks more like an IFSUM if you ask me)
=SUMIFS(SUM RANGE, CRITERIA 1 RANGE, CRITERIA 1, CRITERIA 2 RANGE, CRITERIA 2, CRITERIA 3 RANGE, CRITERIA 3...)
Using your example:
SUM RANGE, the 'Debit' column of the bank register (BankRegisterDebitRange)
CRITERIA 1 RANGE, the 'Description' column of the bank register (BankRegisterDescriptionRange)
Criteria 1, the string "Rent"
Criteria 2 Range, the 'Date' column of the bank register (BankRegisterDateRange)
Criteria 2, this formula string ">="&EOMONTH(RentTableDate,-1)+1
Criteria 3 Range, the 'Date' column of the bank register (BankRegisterDateRange)
Critera 3, this formula string "<="&EOMONTH(RentTableDate,0)
Putting it together:
=SUMIFS(BankRegisterDebitRange, BankRegisterDescriptionRange, "Rent", BankRegisterDateRange, "=>"&EOMONTH(RentTableDate, -1)+1, BankRegisterDateRange, "<="&EOMONTH(RentTableDate, 0))
If your data is fornatted as text then you need to decide if you want to use helper column or not or if you want a single formula. A helper column may be desirable if you want to use SUMIFS or perform additional analysis with simple formulas. A different array based formula may be desirable if you don't want a helper column, for example SUMPRODUCT or SUM.
There's already great answer showing how to use SUMPRODUCT so here is an example of how to build an array formula with SUM when your data is fornatted as text.
=SUM((BankRegisterDebitRange)*(BankRegisterDescriptionRange="Rent")*(DATEVALUE(BankRegisterDateRange)=>(EOMONTH(RentTableDate,-1)+1))*(DATEVALUE(BankRegisterDateRange)<=MONTH(RentTableDate,0)))
This is a true array formula and must be entered with Ctrl + Shift + Enter
I was surprised by some of my results during testing. Mainly how Excel was still treating text as dates in some cases but not others. For example, DATEVALUE was needed on the bank register side but that wasn't the situation with the rent table because EOMONTH worked just fine without it. I believe it is related to another odd behavior: when I create a text formatted cell in A1 and then enter a date, regardless if it is preceded by an apostrophe or not, then if I enter =A1+1 in any other cell, that cell becomes formatted as text and displays Excel's numerical value of the day after the date in A1. What I expected was a #Value! error. I suspect this is what keeps EOMONTH from bonking and the mysterious nature of arrays somehow preventing that behavior from carrying over to SUMIFS... but I really do not know why this is happening.
Suppose you have the following named ranges:
BankDate being the date column in your bank register table;
Desc being the description column in your bank register table;
Dr being the debit column in your bank register table.
If the look up date are text, you can use the following formula:
=SUMPRODUCT((Desc="Rent")*(TEXT(BankDate,"mmm yyyy")=G2)*Dr)
If the look up date are date, you can use the following formula:
=SUMPRODUCT((Desc="Rent")*(TEXT(BankDate,"mmm yyyy")=TEXT(G4,"mmm yyyy"))*Dr)
Change G2 or G4 in the above formulas to suit your actual case.
The logic is to use TEXT function to convert the BankDate into the same format as your look up date, and then use SUMPRODUCT function to return the rent by month.
Ps. using SUMPRODUCT may be an overkill, as SUMIFS suggested by #ProfoundlyOblivious is actually faster in excel calculation.
Ps2. as pointed out by #ProfoundlyOblivious, there is an interesting behavior in Excel in terms of treating text date as 'real' date in certain scenarios. Although it may not be relevant to the question of this post, I'd like to share some of my test results here for anyone interested:

Resources