EDIT
(following OP's shared demo sheet)
The formula to use would be:
=SORT(SORTN(QUERY(FORMULARIO!A4:Q, " select G,E,K,M,N,L,O,Q where not upper(J) = 'PAGÓ' and A > datetime '"&TEXTO(AHORA()-3, "yyyy-mm-dd HH:mm:ss")&"' and E is not null and O is not null and Q contains '@' and not Q matches '"&TEXTJOIN("|", 1, QUERY(FORMULARIO!J4:Q, "select Q where upper(J) = 'PAGÓ' ", 0))&"'", 1),9^9,2,2,1),1,1)
What was changed:
- We wrapped your original query with the
SORTN
function to find and exclude all duplicate emails from column Q
except the most recent one based on the Timestamp from column A
.
- We -one more time- wrapped the results with the
SORT
function to have our final results sorted by the Timestamp.
Pro tip
An extra change was also made within the main query by changing
and A > date '"&TEXT(HOY()-3, "yyyy-mm-dd")&"'
to
and A > datetime '"&TEXT(AHORA()-3, "yyyy-mm-dd HH:mm:ss")&"'
So we changed TODAY
to NOW
.
By doing this we count dates making use of both the date and time present in a timestamp instead of just the date.
(There is still room for minor improvements/alterations but out of the scope of this question.)
Original answer
Taking for granted that your formula works as expected (cannot check it without a test sheet) but produces multiple rows you can use the limit
clause in the end of your formula.
=QUERY(FORMULARIO!A4:Q, " select G,E,K,M,N,L,O,Q where not upper(J) = 'PAGÓ' and A > date '"&TEXTO(HOY()-3, "yyyy-mm-dd")&"' and E is not null and O is not null and Q contains '@' and not Q matches '"&TEXTJOIN("|", 1, QUERY(FORMULARIO!J4:Q, "select Q where upper(J) = 'PAGÓ' ", 0))&"' limit 1", 0)