Google Spreadsheet filter - filter

I'm trying to filter a list of employee by department in google spreadsheet
=filter(A:C,!B2:B6=D2)
my sample data is
Name Department age
asd dep1 12
fds dep2 2
plo dep3 3
sdfsdf dep1 56
shg dep2 98
now I already got the filter part working.. my question is after filtering. how can I tell GSpreadsheet to only display specific column values... like use department as filter condition but only return name and age?
eventually I also want to move my result to a different sheet.
My first choice of approach for this issue was to use google macro like thingy and just hide the rows and comlumns not needed if you have suggestions for this please don't hesitate to provide.

The easiest way to filter is the [Data-Filter], otherwise [View-List] is also able to filter. Using a formula you will be displaying the results in a different column, usually on a different page.
My suggestion to omit column B would be to make two separate column filters like this:
=filter(A:A,B:B=D2)
=filter(C:C,B:B=D2)
The exclamation is only necessary if you are on another sheet, you may also be trying to do something like this: filter(A2:A,B2:B="dep1") the real power of the formula is where you want to manipulate the value before your comparison like this: filter(A:A,right(B:B,1)="1") otherwise I'm not sure what you are referring to, other than Google Apps Scripts I am not aware of any functionality very similar to VBA.
If this did not answer your question can you be a little bit more specific?

I would use the QUERY function here:
=QUERY(A:C, "select A,C where (B = 'dep1')")
This will return with two columns (Name and Age - that is "A" and "C" respectively). The filter condition is provided in the WHERE clause.
For moving the result to another sheet, I would do it another way around: Create another sheet, and then write:
=QUERY(employees!A:C, "select A,C where (B = 'dep1')")
Assuming that your original worksheet's name is "employees".
I hope it helped.

Related

Google Sheet Query: Select misses data when there are different data type in a column?

I have a table like this:
a
b
c
1
2
abc
2
3
4.00
note c2 is text while c3 is a number.
When I do
=QUERY(A1:C,"select *")
The result is like
a
b
c
1
2
2
3
4.00
The "text" in C2 has been missed. You can see the live sheet here:
https://docs.google.com/spreadsheets/d/1UOiP1JILUwgyYUsmy5RzQrpGj7opvPEXE46B3xfvHoQ/edit?usp=sharing
How to deal with this issue?
QUERY is very useful, but it has a main limitation: only can handle one kind of data per column. The other data is left as blank. There are usually ways to try to overcome this from inside the QUERY, but I've found them unfruitful. What you can do is just to use:
={A:C}
You can work with filters by its own, but as a step-by-step to adapt the main features of query: If you need to add conditions, use LAMBDA INDEX and FILTER
For example, to check where A is not null:
=LAMBDA(quer,FILTER(quer,INDEX(quer,,1)<>""))({A:C}) --> with INDEX(quer,,1), I've accesed the first column
Where B is more than one cell and less than other:
=LAMBDA(quer,FILTER(quer,INDEX(quer,,2)>D1,INDEX(quer,,2)<D2))({A:C})
For sorting and limiting an amount of items, use SORTN. For example, you want to sort by 3rd column and limit to 5 higher values in that column:
=LAMBDA(quer,SORTN(FILTER(quer,INDEX(quer,,1)<>""),5,1,3,0))({A:C})
Or, to limit to 5 elements without sorting use ARRAY_CONSTRAIN:
=ARRAY_CONSTRAIN(LAMBDA(quer,FILTER(quer,INDEX(quer,,1)<>""))({A:C}),5)
There are other options, you can use REGEXMATCH and other options, and emulate QUERYs functions without missing data. Let me know!
shenkwen,
If you are comfortable with adding an Google App Script in your sheet to give you a custom function, I have a QUERY replacement function that supports all standard SQL SELECT syntax. I don't analyze the column data to try and force to one type based on which is the most common data in the column - so this is not an issue.
The custom function code - is one file and is at:
https://github.com/demmings/gsSQL/tree/main/dist
After you save, you have a new function from your sheet. In your example, the syntax would be
=gsSQL("select a,b,c from testTable", {{"testTable", "F150:H152", 60, true}})
If your data is on a separate tab called 'testTable'(or whatever you want), the second parameter is not required.
I have typed in your example data into my test sheet (see line 150)
https://docs.google.com/spreadsheets/d/1Zmyk7a7u0xvICrxen-c0CdpssrLTkHwYx6XL00Tb1ws/edit?usp=sharing

Google Sheets vlookup based on multiple criteria with wildcards

I'm trying to extend a gs vlookup formula with a 2nd criteria.
This formula works fine with 1 criteria in column A.
=ArrayFormula(IFNA(VLOOKUP(""&A2:A&"",sheet2!C:D,2,FALSE), ""))
But I would like to extend the formula with a 2nd (OR) criteria .
That sould somehow look like this:
=ArrayFormula(IFNA(VLOOKUP(""&A2:A&"" OR ""&B2:B&"",sheet2!C:D,2,FALSE), ""))
(The fomula shoud check if one of the 2 criterias (in column A or B) matches with column C on sheet2 and return the value from D on sheet2)
Is this even possible with a fomula?
This formula seems to do what you're looking for, placed in C2:
=ArrayFormula(
IFNA( VLOOKUP(A2:A,Sheet2!C:D,2,0),
IFNA(VLOOKUP(B2:B,Sheet2!C:D,2,0),"no match")))
I think there are several other ways of doing this, but I went with what you'd started with.
I haven't added error checking yet, for blank rows, etc., to keep it straightforward.

Returning the last value from the FILTER function in google sheets

I'm using Google Sheets and would like to get the last value in column when it is filtered based on the values in a separate column as shown in the screenshot:
I'd like to get the last value from column A, where the value in column B matches that specified in cell D1.
I've managed to do this with the following:
=INDEX(FILTER($A:$A,$B,$B=$D$1),COUNTA(FILTER($A:$A,$B:$B-$D$1)),1)
This works but it seems unnecessary to have the second FILTER and COUNTA as it makes it harder to understand. Is there no way I can just return the last value from the FILTER function?
Since posting this I've found another way that's more concise, but I have to confess I don't actually understand how it works:
=ArrayFormula(LOOKUP(2,1/($B:$B=$D$1),$A:$A))
Here you will know about sort and array_Constrain:
=array_constrain(filter(sort({A1:A,row(A1:A)},2,false),B1:B=D1),1,1)
or you can use query:
=query(filter({A1:A,row(A1:A)},B1:B=D1),"Select Col1 order by Col2 desc limit 1")
or you can use indirect:
=indirect("A" & max(filter(row(A:A),B:B=D1)))
I know this is a way that I do that sometimes. it takes advantage of the VLOOKUP(....TRUE) [default] option.
=VLOOKUP(9^99,FILTER({ROW(A:A),A:A},B:B=D1),2)

Does Spotfire provide any simple way of creating an "Other" category to group entries within a filter?

Right now, I am using a filtering scheme which only looks at the data of the 5 or 6 most common entries in the 'Clinic' field. But, there are a handful of other possibilities which might account for a few rows each. They are too inconsequential to include on their own (I am using pie charts and bar charts), but I would like these rows to be accounted for. For this reason, I would like to create an "Other" category which groups these entries together. What is the best way of doing this? I know I can create a calculated column that groups everything aside from the top 5 or 6 in an other category, but I thought there might be a way to keep working with the original column and achieve the same result.
Unfortunately not. In 6.5.x you will have to write a case statement that will specify everything that is not most common to other.
In 7.0.x you can go to insert binned column. Add the bottom you can use values to create a bin. Add the values you want to the bin and call them "Other". Of course if you look at the column created like this, it is a case statement. But it is a whole lot faster than writing it yourself.
Following phiver I came out with this solution in Spotfire 6.5.2:
Add a calculated column
With something like this:
If(DenseRank(Count([Formation]) OVER ([Formation]),"desc")<10, DenseRank(Count([Formation]) OVER ([Formation]),"desc") & " " & [Formation], "10 Other")
Hope that helps.

Dynamic Sheet Name in Query in Google Spreashsheet

In Google spreadsheet I want to query data in another sheet but the problem is that the name of sheet is present in a cell. So is there a way in QUERY function to dynamically mention sheet name. Basically I am trying to do something like this but with dynamic sheet name:
=QUERY('2012'!A2:F;"select C, sum(F) where A='December' group by C order by sum(F) desc")
I tried to do this but I get Parse Error:
=QUERY(INDIRECT("Overview!L5")!A2:F;"select C, sum(F) where A='December' group by C order by sum(F) desc")
In which Overview!L5 is the cell with sheet name to query. I also tried to concatenate quotes around INDIRECT but that didnt work either.
I think it is quite evident what I am trying to do from the query i.e. get sum of values in cells grouped by values in other cells.
the INDIRECT looks to be the problem.
Try like this:
=query(INDIRECT(A1&"!A5:A10"),"select Col1")
i.e. if Cell A1 contains "food" the above is the same as:
=query(food!A5:A10,"select A")
and the same as:
=query(INDIRECT("food!A5:A10"),"select *")
**Note: the indirect uses "Col1" etc and not "A" because it does not pass the col letters.
Also ... The google groups forum might be a good place to look for spreadsheet formula answers. productforums.google.com/forum/#!categories/docs/spreadsheets
Easiest way to use dynamic structures in a query is to not include functions inside query but prepare strings in separate cells, for instance A1 for address B1 for arguments and then just QUERY(A1;B1)

Resources