Countifs with array formula to collect data - google-sheets

I have a raw data that contains types of defect (EC,VC,BC,NC).
first type of defect calculation (EC,VC,BC).
For example if i want to collect the EC defects, I will collect all the rows that contain EC=0 & if the EC is score down to 0 twice in the same row it will count as 1 EC only in this raw.
Same goes for VC & BC.
Second Type of defect calculation (NC)
Same calculation of the above but if the NC defect was repeated on the same row it will be summed.
I have the headers containing VC,EC,BC,NC above each column containing the score.
What i need here to calculate each raw containing defects.
I tried the below formula for VC,EC,BC.
=ARRAYFORMULA(IF(ROW(E2:E)=2,"EC",IF(LEN(E2:E)=0,IFERROR(1/0),IF(COUNTIFS($E$1:$BH$1,"EC",$E:$BH,0)>=1,1,0))))
The formula is working without the array formula, but will be hard to drag each time the data is updated.
Also, Tried the below for NC.
=ARRAYFORMULA(IF(ROW(H2:H)=2,"NC",IF(LEN(H2:H)=0,IFERROR(1/0),COUNTIFS($E$1:$BH$1,"NC",$E:$BH,0))))
Sample Sheet: Test Sheet

={"EC"; ARRAYFORMULA(IF(LEN(TRIM(FLATTEN(QUERY(TRANSPOSE(IF(FILTER(
INDIRECT("E3:"&ADDRESS(MAX((E:Z<>"")*(ROW(A:A))), COLUMNS(1:1))),
E1:1="EC")=0, 1, )),,9^9))))>0, 1, 0))}
={"VC"; ARRAYFORMULA(IF(LEN(TRIM(FLATTEN(QUERY(TRANSPOSE(IF(FILTER(
INDIRECT("E3:"&ADDRESS(MAX((E:Z<>"")*(ROW(A:A))), COLUMNS(1:1))),
E1:1="VC")=0, 1, )),,9^9))))>0, 1, 0))}
={"BC"; ARRAYFORMULA(IF(LEN(TRIM(FLATTEN(QUERY(TRANSPOSE(IF(FILTER(
INDIRECT("E3:"&ADDRESS(MAX((E:Z<>"")*(ROW(A:A))), COLUMNS(1:1))),
E1:1="BC")=0, 1, )),,9^9))))>0, 1, 0))}
={"NC"; ARRAYFORMULA(MMULT(IF(FILTER(
INDIRECT("E3:"&ADDRESS(MAX((E:Z<>"")*(ROW(A:A))), COLUMNS(1:1))),
E1:1="NC")=0, 1, 0), SEQUENCE(COUNTIF(E1:1, "NC"))^0))}

Related

How to write a formula in Google Spreadsheet that will count sum of multiplied values using VLOOKUP function

I have created a spreadsheet to track my crypto portfolio.
Here is the simple view of the task, that I'm trying to solve:
I want to:
Take the number (amount) from each cell (A3:C3)
Take the Currency name (A2:C2) that is related to the number in A3:C3
VLOOKUP the data for EACH currency (F2:F4) and "*" amount (A3:C3) and the current price (G2:G4) accordingly
The final step is to Sum all multiplied values and return the TOTAL SUM
So, basically, it works for me if I use the formula like that in my original sheet:
=(O8*(VLOOKUP(O$1,AD$2:AE,2,FALSE)))+(P8*(VLOOKUP(P$1,AD$2:AE,2,FALSE)))+(Q8*(VLOOKUP(Q$1,AD$2:AE,2,FALSE)))+(R8*(VLOOKUP(R$1,AD$2:AE,2,FALSE)))+(S8*(VLOOKUP(S$1,AD$2:AE,2,FALSE)))+(T8*(VLOOKUP(T$1,AD$2:AE,2,FALSE)))+(U8/(VLOOKUP(U$1,AF$2:AG,2,FALSE)))+(W8*(VLOOKUP(W$1,AD$2:AE,2,FALSE)))+(X8*(VLOOKUP(X$1,AD$2:AE,2,FALSE)))+(Y8*(VLOOKUP(Y$1,AD$2:AE,2,FALSE)))
However, this is too long and I'd like to find a better way to do this function
The problem with the current formula is that if I add a new wallet/currency between A:C (according to the sample screenshot), the whole formula will break because it won't include a new value. That's why I'm looking for a better formula to be able to insert the new column between A:C (so it becomes A:D) and the total SUM works in a way =sum(A:D)
No matter, how many cells I insert between A:D, the final function will work
If I use the long function like above, I have to rewrite many other cells to include a new column function once I add a new column.
Please let me know if you have any questions.
try:
=SUMPRODUCT(QUERY({TRANSPOSE(A3:C4),
IFNA(VLOOKUP(FLATTEN(A3:C3), F:G, 2, ))},
"select Col2*Col3"))
=ARRAYFORMULA(MMULT(IF(B6:G10="", 0, B6:G10*HLOOKUP(B4:G4,
TRANSPOSE(J2:K10), 2, 0)), SEQUENCE(COLUMNS(B6:G10), 1, 1, 0)))

Array of different sizes

I am using this formula and I am getting some troubles in there:
=ARRAYFORMULA(IF(A2:A="","",IF($B$2:$B>=Helper!$B$2:$B,1,0)))
B2:B Column Main sheet contains almost 30k rows with values and Helper2 B column sheet has only 60 values but the sheet has 1000 rows.
I am checking whether the values in B (Main sheet) are greater or equals to what I have in the Helper B column. But I am only getting the results for the first 1000 rows in the main sheet.
can anyone please tell me what is the workaround for this?
ERROR is: Array arguments to GTE are of different sizes.
Thanks
Here is the link to the Spreadsheet
https://docs.google.com/spreadsheets/d/1hfXCFTp_vLqPFjazM-Vo6-3o8J1td1zpnCJOeK1LBg0/edit#gid=511305173
Main sheet
Helper2 Sheet
The issue is that both sheets have unequal number of rows. (GTE in the error means greater than equal which is the conditional you used)
Easiest fix to eliminate the error is to have the same number of rows from both sheets. If your Main sheet have 30000 and Helper2 have 1000, then add 29000 to your Helper2 sheet.
But if you want to limit your ARRAYFORMULA and only compare until row 1000, then limit the range. Use the formula below:
=ARRAYFORMULA(IF(A2:A1000="","",IF($B$2:$B>=Helper2!$B$2:$B,1,0)))
Or use ARRAY_CONSTRAIN to limit the result of your ARRAYFORMULA
=ARRAY_CONSTRAIN(ARRAYFORMULA(IF(A2:A="","",IF($B$2:$B>=Helper2!$B$2:$B,1,0))), 999, 1)
Limits to 999 rows (since you started at row 2, 1000th row in sheets should be the 999th row of ARRAYFORMULA result)
EDIT:
Try this one:
=arrayformula(if(A2:A="","",if((B2:B <= vlookup(A2:A, Helper2!$A$2:$B, 2)) * (C2:C >= vlookup(A2:A, Helper2!$A$2:$B, 2)) > 0 = TRUE, 1, 0)))
Note:
Your data has issue with your Artem name, not sure why. Thus if Artem is found, blank is returned.
Tried changing all "Artem" to "John" and the formula works seamlessly.

Arrayformula + SUM + INDIRECT

I am trying to get the spreadsheet to use the value of L2 as a reference to calculate the cash value at the moment:
From the formula:
=arrayformula(IF(I2:I<>"",$L$2+SUM(INDIRECT("G2:G"&I2:I)),""))
But the lines below always return the same result, in this case 100.
I wonder where I am going wrong in creating this formula
Another detail:
I have made a chart ready to treat this cash growth over time, if you decide to help by looking straight at the spreadsheet, by re-adjusting the correct formula, please also take a look if the chart is working correctly according to the data and the movement of values, always the last row of columns G and H at the beginning of the graph
This is the expected result in Bank Column:
Capture the initial bankroll L2 and from it add the sequence of results achieved in each investment in COLUMN G
Link to spreadsheet
try:
=ARRAYFORMULA(QUERY(L2+MMULT(ARRAY_CONSTRAIN(SPLIT(REPT("0×",
ROW(INDIRECT("A1:A"&COUNTA(D2:D)+1))-1)&
TEXTJOIN("×", 1, INDEX(SORT({INDIRECT("G2:G"&COUNTA(D2:D)+1),
ROW(INDIRECT("G2:G"&COUNTA(D2:D)+1))}, 2, 0),,1)), "×"), 999^99,
COUNTA(D2:D)+1)*1, ROW(INDIRECT("G1:G"&COUNTA(D2:D)+1))^0),
"offset 1", 0))

Converting formula to ARRAYFORMULA issues with SUM and INDEX

I have a scoring spreadsheet for a competition I'm working on. Competitors' place/rank are converted into points towards the overall series based on a chart of corresponding values. For ties, the sum of the points covered by all of the tied places are split evenly among the tied competitors (i.e. 2-way tie for 3rd; if 3rd usually gets 10 points and 4th usually gets 8, these competitors will receive (10+8)/2 (2 being the # of tied competitors), so they each receive 9 points).
I have a formula which does this exact calculation:
=IFERROR(IF(ISBLANK($A4:$A),,SUM(INDEX(SeriesPoints, E4:E):INDEX(SeriesPoints, MIN(E4:E + COUNTIF(E$4:E, E4:E) - 1, ROWS(SeriesPoints)))) / COUNTIF(E$4:E, E4:E), 0))
Where 'SeriesPoints' is a 2 column array; column 1 is the places/ranks (1:125) and column 2 is their corresponding point values. Column 'E' is the competitors' rank from the competition.
I have been unable to convert this formula to an ARRAYFORMULA() so I can avoid dragging it down the entire sheet (possibly up to 1000+ competitors over the series).
I'm mildly proficient with MMULT(), so I understood that would be a good approach for switching out SUM(), however, I haven't been able to create a matrix of the values to be summed.
INDEX():INDEX() doesn't work with ARRAYFORMULA() so I've tried switching to VLOOKUP(). With VLOOKUP() I've been able to produce the start and end values of the range of values for a tie, but not the full list. For example, if there is a 3-way tie for 4th, I can produce the respective points for 4th and 6th (the bounds of the tie).
In an attempt to list out even just the numbers from 4:6, I've hit a wall converting what would be a simple ROW() or SEQUENCE() formula to a matrix/array.
The following formula produces an array of the upper and lower bounds of ties or the single place should there be no tie, although the single place gets repeated.
=ARRAYFORMULA(IF(COUNTIF(E$4:E,E4:E)=1,E4:E,{E4:E,E4:E+COUNTIF(E$4:E,E4:E)-1}))
I'm assuming if I can get VLOOKUP({#:#}) to fill properly, I'll be where I need to be.
From here, I feel confident in my abilities to wrap a VLOOKUP() for the actual point values, an MMULT() to sum across these rows for the total, then a simple division to produce the correct point value.
Spreadsheet: https://docs.google.com/spreadsheets/d/1lpNewR3p4i7ZHmlFGLlG1tLuxgO-6onSeH8mWTeclBw/edit?usp=sharing
Currently, my workspace is off to the right. The original formula is in F4 and my test codes are working on column G instead of E.
So for sample placements of 1,1,3,3,3,6,7,8 and sample points values of 1000, 850,738,663,633,603,573,550 I expect the output to be 925 for the two 1st place tied competitors, 678 for the tied 3rd places, 603 for 6th, 573 for 7th, and 550 for 8th.
I'd appreciate any and all help!
=ARRAYFORMULA(IFERROR(IFERROR(VLOOKUP(G4:G, QUERY({INDIRECT("G4:G"&counta(A4:A)+3),
VLOOKUP(ROW(INDIRECT("A1:A"&COUNTA(A4:A))), SeriesPoints, 2, 0)},
"select Col1,sum(Col2) group by Col1 label sum(Col2)''", 0), 2, 0))/
IFERROR(VLOOKUP(G4:G, QUERY(G4:G,
"select G,count(G) where G is not NULL group by G label count(G)''", 0), 2, 0))))

Conditional Formatting on a value in a range A:C which is closest to an input value y

I'm working on a spreadsheet which calculates my caloric intake. The sheet comprises two parts. One which is simple data which I input such as weight and calorie intake. And a second table showing the number of calories per pound required to either lose, maintain or gain.
What should my formula be under conditional formatting in order to highlight the value in list closes to 1500?
=IF($C4=ARRAY_CONSTRAIN(SORT(ARRAYFORMULA({$C$4:$C, ABS($C$4:$C-$F$4)}), 2, 1), 1, 1), 1)

Resources