INDEX(FILTER(SORT not solving - Need an alternative? - sorting

Purpose: I'm trying to fetch the value from column c in the first sheet, where column a matches to column a in current sheet. If there are more than one matches in column a, fetch the most recent entry, according to the date in column b. If the most recent cell in column c was left blank, go back and fetch from the most recent matching row in which column c contains a value.
See the sample sheet, it's pretty clear.
I tried: =INDEX(FILTER(SORT('VISIT LOG'!C2:C,'VISIT LOG'!B2:B,FALSE),'VISIT LOG'!A2:A=A2,'VISIT LOG'!C2:C<>""),1,1)
But it's not working.
Any help will be appreciated!

you can try:
=INDEX(IF(LEN(A2:A),IFNA(VLOOKUP(A2:A,SORT(LAMBDA(z,FILTER(z,INDEX(z,,3)<>""))('Visit log'!A2:C),2,0),3,)),))

You can try with QUERY and MAP:
=MAP(A2:A,C2:C,LAMBDA(ax,cx,IF(ax="","",QUERY('Visit log'!A2:C,"Select C where C is not null AND A = '"&ax&"' order by B desc limit 1"))))

Related

How do I sort a data range but only return one column? (Sheets)

I have a data range in Google Sheets where I want to sort the data by column B, but only return column A. If it matters, column A is a string, column B is integers.
Using =SORT(A1:B10,2,FALSE) returns both columns A and B, sorted by column B...but I only want it to return column A.
I've also tried:
=QUERY((SORT(A1:B10,2,FALSE)),"select *") <- does exactly the same as sort, tried just for testing
=QUERY((SORT(A1:B10,2,FALSE)),"select col1") <- #value error
=QUERY((SORT(A1:B10,2,FALSE)),"select A") <- #value error (also tried "select A:A" and "select A1:A10")
=QUERY((SORT(A1:B10,2,FALSE)),"select Stat") <- #value error
I've also tried all of the above, but starting with =QUERY(A1:B10,SORT(...
Am I using QUERY wrong? Is SORT not what I want? I could just use SORT in a hidden part of the sheet, then reference the column I want but that feels cheaty, I want to know if there's a way to do what I want to do.
You can set in the first part the column you want to be returned, then the column you want to be sorted with, and then if it's ascending or not (you can then add other columns, obviously. They don't need to be included nor contiguous, but of the same size). Try this:
=SORT(A1:A10,B1:B10,FALSE)
use:
=INDEX(SORT(A1:A10,2,),,1)

Google Sheets: When there are dupe values in a column, determine which value should be included by comparing corresponding dates in a date column

I have a dataset with an ID column and Edit Date column. There are some duplicate IDs but the Edit Dates will be different for each dupe ID. I created 2 helper columns to help me determine if I need to include the row of data in a separate analysis I'm working on. I included Notes in the sample dataset, with a short description of each column.
I want to include any rows that:
Have Count value = 1
or
Have Count value >1 but the Edit Date value for its corresponding ID is the lowest value of all Edit Dates listed in all rows this value is in
I don't want to include any rows that:
Have Count value >1 and Edit Date value that not the lowest Edit Date for the corresponding ID
So far all I have is the first part...I'm having trouble figuring out how to go about the comparison when Count >1, so any help would be greatly appreciated.
This is all I have so far...
=IF(C2:C=1,"YES",...?)
Thank you in advance!
try:
=ARRAYFORMULA(IF(A2:A="",,VLOOKUP(A2:A&B2:B,
{SORT(A2:A&B2:B, B2:B, 1), IF(COUNTIFS(SORT(A2:A, B2:B, 1), SORT(A2:A, B2:B, 1),
ROW(A2:A), "<="&ROW(A2:A))=1, "yes", "no")}, 2, 0)))

Return unique row using a query formula

I have a data as shown in the spreadsheet (Here is the link to sheet)
Where I need to get the uniques when both columns B and C considered, The col B is a date type
=QUERY(UNIQUE(FILTER(A:E,C:C="Daniel Workman")))
The expected result is indicated in yellow color
use:
=FILTER(A9:E18, COUNTIFS(B9:B18&C9:C18, B9:B18&C9:C18)=1, C9:C18="Daniel Wokman")
and:
=FILTER(A9:E18, COUNTIFS(B9:B18&C9:C18, B9:B18&C9:C18)=1, C9:C18="Morgan Freeman")

Using XPath to find rows where a specific column has value

I'm having trouble using XPath to find a row in a table where a specific column contains a value. The table has 10 columns where 2 of them will show Yes|No but I'm only interested in finding the value in one of the columns (the 4th one). My initial attempt was this:
//table[#id='myTable']/tbody/tr/td[text() = 'Yes']
but it finds it rows from both columns. I thought I could try something like this but it's not a valid expression:
//table[#id='myTable']/tbody/tr/td[4]/text()='Yes'
Any suggestions? Thanks.
You can try this way :
//table[#id='myTable']/tbody/tr[td[4][. = 'Yes']]
The XPath return row (tr) having the forth td child value equals "Yes".

how can I group sum and count with sequel ORM and postgresl?

This is too tough for me guys. It's for Jeremy!
I have two tables (although I can also envision needing to join a third table) and I want to sum one field and count rows, in the same, table while joining with another table and return the result in json format.
First of all, the data type field that needs to be summed, is numeric(10,2) and the data is inserted as params['amount'].to_f.
The tables are expense_projects which has the name of the project and the company id and expense_items which has the company_id, item and amount (to mention just the critical columns) - the "company_id" columns are disambiguated.
So, the following code:
expense_items = DB[:expense_projects].left_join(:expense_items, :expense_project_id => :project_id).where(:project_company_id => company_id).to_a.to_json
works fine but when I add
expense_total = expense_items.sum(:amount).to_f.to_json
I get an error message which says
TypeError - no implicit conversion of Symbol into Integer:
so, the first question is why and how can this be fixed?
Then I want to join the two tables and get all the project names form the left (first table) and sum amount and count items in the second table. I have tried
DB[:expense_projects].left_join(:expense_items, :expense_items_company_id => expense_projects_company_id).count(:item).sum(:amount).to_json
and variations of this, all of which fails.
I would like a result which gets all the project names (even if there are no expense entries and returns something like:
project item_count item_amount
pr 1 7 34.87
pr 2 0 0
and so on. How can this be achieved with one query returning the result in json format?
Many thanks, guys.
Figured it out, I hope this helps somebody else:
DB[:expense_projects___p].where(:project_company_id=>user_company_id).
left_join(:expense_items___i, :expense_project_id=>:project_id).
select_group(:p__project_name).
select_more{count(:i__item_id)}.
select_more{sum(:i__amount)}.to_a.to_json

Resources