Index formula on multiple columns - google-sheets

Is it possible to use this index formula on multiple columns?
=index(if(Sheet1!B5:B<>"",,Sheet1!A5:A))
[Sample Table]
For the table above, what if we want to add Column C? (If column B and C have data on Sheet 1 - remove them, and if 1 is missing in either column(B or C), it will appear.

The desired results shown in the screenshot suggest that you want the matching data without inserting blank rows in between. To get that, use filter(), like this:
=filter(Sheet1!A5:A, isblank(Sheet1!B5:B)
To add another criterion, and combine the criteria with OR, use the + operator, like this:
=filter(Sheet1!A5:A, isblank(Sheet1!B5:B) + isblank(Sheet1!C5:C))
To get an AND condition, simply add criteria as their own parameters, or use the * operator. See Boolean arithmetic.

FILTER() with MMULT() may give you desired result.
=FILTER(Sheet1!A4:A,MMULT(INDEX(--(Sheet1!B4:C="x")),{1;1})<2)
Here MMULT(INDEX(--(Sheet1!B4:C="x")),{1;1}) will create a vertical array having count how many x do you have in each row. If you have x in both column of each row then MMULT() will return result 2. If you have one x then will return 1 and if there is no x then 0. Then we will filter that vertical array having values less than 2.
MMULT() reference.

Related

Automatically fill sum column

I have a table of items with its buying and selling rates. Against each transaction, I wanted to show the item qty currently available and the current cost. Here is a screenshot of the table.
For the columns of Qty and Rate, I have used the formulas as shown below:
=SUMPRODUCT(C$2:C-F$2:F,B$2:B=B2,A$2:A<=A2)
=SUMPRODUCT(I$2:I/J2,B$2:B=B2,A$2:A<=A2)
Is it possible to convert SUMPRODUCT formulas as an array formula (returning array) so that it automatically fills whenever a row is added? Something like filling a column using FILTER or QUERY?
Regards,
Pravin Kumar.
I am making an assumption that this is your goal:
To automatically fill the quantity remaining column with the result of the difference between the two values, if and only if, there are two values provided.
A solution to this objective is:
=arrayformula(if(and(C2:C<>””, F2:F<>””), C2:C-F2:F, “”))
This produces a formula that subtracts C from F only if both C and F have values, and for all rows that have values in both C and F. If one of the rows does not have a value, the result will yield “” (blank). This formula should be posted at the top of the column where you want these results to start. In your screenshot example this would be cell J2. NOTE: 0 is still a value, and also that an array formula will not overwrite manually input data, so once you paste that function in J2, you will have to clear the cells below in order for it to auto populate.

How to print different values to column based on value in another column and other conditions met?

I have a spreadsheet with several columns and I want to return a different value based on the value in Column A and if any of the other columns show a true or 1.
For example
If column A has the value "A" and any column B-N is either TRUE or 1 then I want to return "Good" to column O
If column A has the value "B" and any column B-N is either TRUE or 1 then I want to return "Best" to column O
Link to spreadsheet: https://docs.google.com/spreadsheets/d/12k9usKsOgrOUhtW5WBvfY7WB5hbfnTMSPgXtrqcRFjM/edit?usp=sharing
Try this in cell O2 (where your sample sheets only has values of TRUE, FALSE, Y or N in B2:N):
=arrayformula((trim(transpose(query(transpose(iferror(regexreplace(regexreplace(text(B2:N,),"(FALSE)|(N)",),"(Y)|(TRUE)",if(A2:A="A","Good",if(A2:A="B","Best",))),)),,columns(B2:N))))))
Alternative where values in B:N are either 0 or 1:
=arrayformula((trim(transpose(query(transpose(iferror(substitute(substitute(B2:N,0,),1,ifs(A2:A="A","Good",A2:A="B","Best")),)),,columns(B2:N))))))
B2:N is the range of cells to process.
The inner SUBSTITUTE clears all 0 values.
The outer SUBSTITUTE swaps 1 values for a test to see if values in A contain "A" or "B".
IFS does the test and returns either "Good" or "Best".
IFERROR hides any #N/A values down the sheet where the rows are empty.
TRANSPOSE transposes the data for QUERY.
QUERY is used to collapse empty cells (vertically). columns(B2:N) is used in the header part of QUERY. This is a quirk of QUERY, where the header number >= the columns of data, QUERY does the collapse.
TRANSPOSE reverts the dataset to the previous orientation.
TRIM removes leading or trailing space.
ARRAYFORMULA allows the formula to automatically cascade down the sheet, rather than you needing to drag the formula down (like with =IF(AND({B2:N2}>0, A2="A"),"Good", "")).
So, you already have this partial solution. To get a second condition to be met before printing something to your cell, just add an AND() with a new COUNTIF(), comparing A column with A, and then at the else argument, repeat your original IF(), just changing A for B and the output for each case. I will look like this:
=IF(AND(countif(A2;"A");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Good";
IF(AND(countif(A2;"B");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Best";"BAD"))
To use it on every row, just autofill the column O. The row numbers will change accordingly and work on its own.
If you need a new if() statement for a third or fourth case, just repeat it, nesting one IF() inside the other, leaving room for a default error message at the end.

VLOOKUP: Return multiple INDEX values in single VLOOKUP and SUM results

I filter a dynamic range of Columns as an INDEX in an VLOOKUP, based on a cell reference. In this case I would get three columns back. Afterwards I pick one of them (INDEX 1 in example).
=VLOOKUP($A2, Sheet1!$A$3:$W, index(filter(arrayformula(column(Sheet1!$A$1:$1)),Sheet1!$A$1:$1=E$1),,1),false)
Since I have to return INDEX 2 and INDEX 3 as well and SUM them up afterwards, I pretty much repeat this formula per INDEX (3 times in this example)
=VLOOKUP($A2, Sheet1!$A$3:$W, index(filter(arrayformula(column(Sheet1!$A$1:$1)),Sheet1!$A$1:$1=E$1),,1),false) +
=VLOOKUP($A2, Sheet1!$A$3:$W, index(filter(arrayformula(column(Sheet1!$A$1:$1)),Sheet1!$A$1:$1=E$1),,2),false) +
=VLOOKUP($A2, Sheet1!$A$3:$W, index(filter(arrayformula(column(Sheet1!$A$1:$1)),Sheet1!$A$1:$1=E$1),,3),false)
I am wondering if there is a way to get the SUM of INDEX 1-3 but with just one formula. Here a picture to help understanding my setup:
I am looking for every Column with GroupX as a reference, return Index1 and Index2 and SUM the values afterwards. How can I do that without repeating the formula 3 times and type in the INDEX manually.
Remove INDEX and just use FILTER and it will return multiple columns meeting the condition. Also, you can remove IFERROR here since only valid columns are returned, thus there should be a value always, no #REF values anymore.
Formula E2:
=divide(
sum(arrayformula(switch(VLOOKUP($A2, Sheet1!$A$3:$J, filter(column(Sheet1!$A$1:$1),Sheet1!$A$1:$1=E$1),false),"done",1,"wip",0.5,"open",0)))
,countif(Sheet1!$A$1:$1,E$1))
Formula E7:
=divide(
sum(arrayformula(switch(VLOOKUP($A7, Sheet2!$A$3:$J, filter(column(Sheet2!$A$1:$1),Sheet2!$A$1:$1=E$1),false),"done",1,"wip",0.5,"open",0)))
,countif(Sheet2!$A$1:$1,E$1))
Output:
Note:
IDs ending in B, I used Sheet2, and just copy pasted to rows 7-11
Pardon for the background colors, it wasn't formatted properly like yours.

How to use a function as a criteria in COUNTIF

I have a bunch of columns that holds scores like 3-1 1-4 1-0 2-2.
I would like to count all columns that are winning, ie. left number is higher than right one.
I have this formula to know if a column is winning : =LEFT(H2; FIND("-"; H2)-1)-RIGHT(H2; FIND("-"; H2)-1)
What I want to do now is to use this formula within COUNTIF, something along the line :
=COUNTIF(A1:A10; "LEFT(CURRENT_COLUMN; FIND("-"; CURRENT_COLUMN)-1)-RIGHT(CURRENT_COLUMN; FIND("-"; CURRENT_COLUMN)-1)")
Is there any way I can do it with a single formula ?
Complex filtering like this can be done with filter, and then the results can be counted with counta. Example:
=counta(filter(A:A, LEFT(A:A, FIND("-", A:A)-1) > RIGHT(A:A, FIND("-", A:A)-1)))
The first argument of filter is the range to be filtered; the second is a formula based on that range (or another range with the same row count) which returns True or False. The rows where the formula evaluates to True are returned.

How to use INDEX() inside ARRAYFORMULA()?

I am trying to use the INDEX() formula inside an ARRAYFORMULA(). As a simple (non-sense) example, with 4 elements in column A, I expected that the following array formula entered in B1 would display all four elements from A in column B:
=ARRAYFORMULA(INDEX($A$1:$A$4,ROW($A$1:$A$4)))
However, this only fills field B1 with a the value found in A1.
When I enter
=ARRAYFORMULA(ROW($A$1:$A$4))
in B1, then I do see all numbers 1 to 4 appear in column B. Why does my first array formula not expand similar like the second one does?
The INDEX function is one that does not support "iteration" over an array if an array is used as one of its arguments. There is no documentation of this that I know of; it simply is what it is. So the second argument will always default to the first element of the array, which is ROW(A1).
One clumsy workaround to achieve what you require relies on a second adjacent column existing next to the source data* (although it is unimportant what values are actually in that second column):
=ArrayFormula(HLOOKUP(IF(ROW($A$1:$A$4);$A$1);$A$1:$B$4;ROW($A$1:$A$4);0))
or indeed something like:
=ArrayFormula(HLOOKUP(IF({3;2;4;1};$A$1);$A$1:$B$4;{3;2;4;1};0))
edit 2015-06-09
* This is no longer a requirement in the newest version of Sheets; the second argument in the HLOOKUP can just be $A$1:$A$4.
Here is a tip for using vlookup with an array, so that even if the columns are moved later on the formula will still work correctly....
In general, configure the vlookup so that it's reading only 2 columns and returning the second. This can be done by inputting only the 2 columns required, rather than a range and column index.
Example:
Replace the following formula which would fail if columns are moved
=arrayformula( vlookup(C:C, booking!$A:$E ,5 ,false) )
with this formula which will continue to work even if columns are moved
=arrayformula( vlookup(C:C, {booking!$A:$A,booking!$E:$E} ,2 ,false) )
Note, you can also simulate the index function using vlookup.
Example:
Column R:R contains the row index numbers for looking up data in column booking!$A:$A
=arrayformula(vlookup(R:R ,arrayformula({row(booking!$A:$A), booking!$A:$A}),2 , false))
It's a nested array, so it can be helpful to test in stages, eg just the inner part for one example, eg return entry in row 10:
=vlookup(10 ,arrayformula({row(booking!$A:$A), booking!$A:$A}),2 , false)

Resources