Google spreadsheet how to count data if contains a value - arrays

been trying to solve this but am struggling. Hopefully this is the right place to ask.
What I need to do is search a row for a certain word, say "cat". If that word is found within that row then take the value of another cell ("Gain") in that row and add this to a total. Then what I need to do is take that total, and divide it by the number of times "cat" was found within a group of rows. Is this possible?
Hopefully that explains what I am trying to do.
For example my data looks likething like this -
1 2 3 4 5 Gain
1/6/22 cat bear elephant sheep 7
2/6/22 dog cat mouse cow 12
3/6/33 cat cow horse goat 5
Cow total: 2
Rows containing cow / gain (2/12+5) = 8.5
EDIT: What I have noticed it if I use SUMIF it will work ASLONG as the value I am search for is across a single column. However, if it is spreadout across multiple columns I get a value that isn't correct.

try:
=COUNTIFS(B1:E3, A6)
and then:
=INDEX(SUM(IF(B1:E3=A6, F1:F3, ))/COUNTIFS(B1:E3, A6))

This is the formula I use when I need to count how many cells have specific values in it within a range:
=COUNTif($J56:$J956,"=Cat")

Related

Random generated number for percentage of likelyhood

My programming class is having us do dog race betting, and one of the aspects of it is putting the Odds of a dog (9 total) winning into a struct. For example, dog 1 is supposed to win 40% of the time, dog 2 is supposed to win 10% of the time, etc. Our teacher has also made one of our goals to "demonstrate the ability to generate and use random numbers." Here's what my struct/array is:
//DOG
typedef struct {
char Name[100];
int Payout;
double OddsWin;
}Dog;
Dog racers[9];
Any tips/ ideas for how to tackle this? Other than how to do the random generation of numbers with the probabilities, I am also wondering how I would use that to determine if a dog won or lost. Would the probability of a certain number equal a win? like if I made 1 = to a win, would I have to code it so that it generates 1 about 40% of the time for dog 1 and so on?
Basically, if I understand you correctly and trying to simplify your question, you want to find a way to choose randomly between an array of Dog's (racers), based on their OddsWin field, which holds the chance they have to win (in percent, e.g. 40%, 20%, 5.6%, etc.).
General tip - always try to solve a programming problem without coding, and then start coding.
So basically you are trying to make a choice between X, say 4, dogs. Say they have the following odds:
dog 1 - 40% to win
dog 2 - 20% to win
dog 3 - 7% to win
dog 4 - 33% to win
So I would take an array of 100 pieces, and fill it up with 40 times 1, 20 times 2, 7 times 3 and 33 times 4. Then, I would choose a random number, between 0 and 99 (array indices), and check the array at than index. If the array's value in that index is 1, than dog 1 won the race, and same for the other dogs.
Maybe there's a more efficient way, but I think that solves the problem :)
One approach would be
-Choose a random num between the range(initially 100)
-assign it to the dog
-subtract the num from range
(loop till 8 and give the rest to 9)
Implementation has many alternatives, the easiest would be giving out values and in a way rigging the system to generate the said probability of that dog say out of 100, run down counters and choose the dog if val>0. Here you can have another random 0-9 generator as well to automate the process.

(excel) How to return an array from a sum of ranges?

I'm setting up a morphological table that will have to go through potentially a couple hundred items, so it's desirable for this process to not be done by hand.
Here's a small summary of the situation:
fin
eng
op
fli
A
2
4
6
8
B
1
3
5
4
C
1
2
3
5
D
1
4
7
2
The first column holds named ranges A through D which have associated values from the 4 categories in row 1.
In a second table we create configurations based on which features are selected, something like this:
Config 1
Config 2
A
B
C
D
What I'm looking for is a formula that would read for each configuration which named range is selected, add the score for each category and return it in a simple array. Something like
Config 1 {3,6,9,13}, Config 2 {2,7,12,6}
So far I've found that the Indirect formula works exactly the way I want but I have to manually input each range. Something like:
=INDIRECT(A1)+INDIRECT(A2)
I've played around with different permutations of sum functions but instead of returning the arrays it returns the sum of the first values.
=SUM(INDIRECT(A1:A2))
Amy suggestion would be welcome.
I know this would probably be much simpler with code but this study needs to be done in excel..
I'm not sure if this answers your question as it doesn't use named ranges, but you could try something like this:
=MMULT(SEQUENCE(1,4,1,0),$B$2:$E$5*COUNTIF(INDEX($H$2:$I$3,0,ROW()-ROW($A$7)+1),$A$2:$A$5))

SUMIF for first 5 cells meeting criteria, with moving window

I am looking to find a formula that calculates the cumulative points a team has for its last 5 games (so the first 5 games would have no values) in the last column. So it should return a number between 0 and 15. In the image below you can see an extract of the dataset. The range can also not continue to move down because then for the last 4 games, only the last 4, 3, 2, 1 game points would be added (hope I did not confuse you more).
I have tried to use SUMIF, SUMIFS with relative row numbers in the cell address within a function but sadly, that did not help. I also looked at SUMIF for first 5 cells meeting criteria but that did not make me any wiser.
Link to the full dataset: https://www.dropbox.com/scl/fi/thu7f8ajsz9g8wtfo9q2w/Data.xlsx?dl=0&rlkey=aq8d7xi4zyg7hvkophsrhswpi
Does anyone know how to do this?
FTHG = Full-Time Home Goals
FTAG = Full-Time Away Goals
FTR = Full-Time Result
PH = Points Home
PA = Points Away
Not sure I really get what you want, but I have used two vlookup's to collect PH and PA and add them using a list of unique names from column A.
Here is the formula as text:
=IFERROR(VLOOKUP(J4,$A$4:$H$20,6,0),0)+IFERROR(VLOOKUP(J4,$B$4:$H$20,6,0),0)
Given your original table and assuming that for any particular row, you are interested in the five games above that row (played by the same home team), you can use:
=LET(x,FILTER(G2:$G$2, B2:$B$2=B2),IF(COUNT(x)>=5,SUM(INDEX(x,SEQUENCE(5))),0))
You can see the first NON-ZERO result is in Row 65

INDEX with MATCH with FIND/SEARCH & position

I have a fairly complex issue where I need to find matches of a short string within a long string, with the search being limited to the first few characters of the long string.
A long search turned up this array formula:
{=IFERROR(INDEX($A$8:$A$9,MATCH(1,--NOT(NOT(FIND($A$8:$A$9,A1))),0)),"no match")}
With that I was able to get part of the way, but don't want to get matches if the value is not found within the first few characters, and I can't figure out how to accomplish this.
How can I adapt the above formula to limit the search to within the first few characters of the value, thus getting the formula to not match the values in cells A3 and A6, returning the desired result in Col-C? Or is there a different formula I should be using?
Here is a sample of the data (my data is quite complex, but the principle is the same):
A (value) B (result with current formula) C (desired result)
1 1apple1234567890 apple apple
2 12apple123456789 apple apple
3 123456apple12345 apple no match
4 1peach1234567890 peach peach
5 12peach123456789 peach peach
6 123456peach12345 peach no match
7
8 apple
9 peach
screenshot
EDIT:
After playing around to learn how the formula works, I was able to achieve the desired result using an IF criteria for the FIND array:
{=IFERROR(INDEX($A$8:$A$9,MATCH(1,--NOT(NOT(IF(FIND($A$8:$A$9,A2)>3,0,1))),0)),"no match")}
use Search and Left:
=IFERROR(INDEX(A:A,AGGREGATE(15,7,ROW($A$9:$A$10)/(ISNUMBER(SEARCH($A$9:$A$10,LEFT(A2,4+LEN($A$9:$A$10))))),1)),"no match")
Replace the 4+ with the number of characters that would satisfy: the first few characters of the value
This array formula (use Ctrl+Shift+Enter) works perfectly:
{=IFERROR(INDEX($A$8:$A$9,MATCH(1,--NOT(NOT(IF(FIND($A$8:$A$9,A2)>3,0,1))),0)),"no match")}

How many trucks came empty but bought something

I think this is the hardest to date I have had to crack - so hard I had a hard time finding a good headline.
So we have a site where trucks come and buy say Gravel, or sand or other building materials.
Sometimes they also unload demolition waste first.
I need to find out a couple of things
how many trucks (and from what companys) came empty
if they came empty what did they buy from us.
what companys are sending full trucks and what are sending empty trucks.
a tope 10 of materials they will drive to us from to buy even when coming empty to our facility.
a list of all the order numbers that they drove to us til fill and came with empty trucks. ( I have distances linked to order numbers, so now I can estimate the value of our products)
The data I have available:
I have a full data set of when what customer buys what and / or pay to deliver.
E.G.:
I can see the parts I need to split the data into I think it should be something like this
find all unique licence plates
somehow map if they bought materials within 30 minutes of
offloading demolition waste (most trucks will come between 2 and 10
times per day)
Present all this data (on a normal day we have about 800 trucks = 2000 lines since they weigh in, weigh out, and then some buy something = 2 more weigh lines)
I can easily find unique licence plates per day (either by formula or by Excel function Data/delete doublets,
but after that I have no clue where to start.
I think I need some sheets in between, where I somehow mark if a material was bought from an "empty truck" and I need a counter for that .. somehow...
Any help on how to get started is appreciated.
It seems like the best way to start is with a helper column (in the following exampes, I have chosen "Column M") to flag whether the truck arrived empty.
In the helper column, you can use something similar to the following formula.
{=IF(ISBLANK(B2),0,IF(C2="In",0,IF(B2=$B$2:$B$13,IF($C$2:$C$13="In",IF($A$2:$A$13>(A2-TIME(0,30,0)),0,1),1),1)))}
This is an array formula, which means you have to press ctrl+shift+enter after pasting it in the cell. Then you can copy that cell down the column.
Just to explain, the first if statement knows the truck is not arriving empty if Column C is 'In'. The second if statement creates an array and tests to see if other the same truck appears in other rows. The third if statement checks to see if the same truck checked 'In' in the matching rows, and the fourth if statement verifies if the time they checked in was less than thirty minutes ago. You can adjust the length by editing the TIME(0,30,0) function. The format is TIME(hours,minuites,seconds). Unless the truck matches all three of the second, third and fourth if statements, it is marked as coming empty.
Once you have this helper column, just about all of your tasks are quite simple.
1a: How many trucks came empty? Sum Column M
1b: How many trucks from what company? Create a unique list of companies. Then create a COUNTIFS formula based on Column M = 1 and Column K = Company. For example, if C32 had Company B then the formula =COUNTIFS($M$2:$M$13,1,$K$2:$K$13,C32) would return 2
1c: How many times did a truck come empty? Similar to 1b, create a unique list of License Plates, then use a COUNTIFS based on Column M = 1 and Column B = License Plate.
2: Similar to 1b, just use a unique list of products tested against Column F
3: Similar to 1b, just create a second column, next to the first that uses =COUNTIFS($M$2:$M$13,0,$K$2:$K$13,C53,$C$2:$C$13,"In") Which tests that Column M reports the truck did not come empty, that matches the company in Column K and that the truck came 'In' so you don't double count the same truck when it goes 'out'
4: Just sort list created by number 2. You can highlight the range, right-click and select "Sort" > "Custom Sort", then select the column you want to sort on and largest to smallest.
5: There are a couple of different ways, you could do this. The formula
{=TEXTJOIN(", ",TRUE,IF($M$2:$M$13=1,$J$2:$J$13,""))}
(again, entered as an array formula)
would create a comma separated list of order numbers. An alternative if you want a column of order numbers (but would only work if they are actually numbers), is to paste the formula {=MAX(IF($M$2:$M$13=1,$J$2:$J$13,))} in the first row of the column (in my example, its O2) and then {=MAX(IF($M$2:$M$13=1,IF($J$2:$J$13<O2,$J$2:$J$13,)))} in the row below (change the reference to O2 if you pasted it in a different spot)(again, note that both of these are array formulas). Then copy and paste the second formula down the column. When order numbers of trucks that came in empty are exhausted, the formula will report 0.

Resources