3

My data looks like this:

+---------------+-----+-----+------+-----+-----+
| Serial Number | LSL | LCL | DATA | UCL | USL |
+---------------+-----+-----+------+-----+-----+
|             1 |   1 |   3 | 2.3  |   7 |   9 |
|             2 |   1 |   3 | 3.1  |   7 |   9 |
|             3 |   1 |   3 | 2.7  |   7 |   9 |
|             4 |   1 |   3 | 4.9  |   7 |   9 |
|             5 |   1 |   3 | 5    |   7 |   9 |
|             6 |   1 |   3 | 3    |   7 |   9 |
|             7 |   1 |   3 | 10   |   7 |   9 |
|             8 |   1 |   3 | 7.8  |   7 |   9 |
|             9 |   1 |   3 |      |   7 |   9 |
|            10 |   1 |   3 | 6.8  |   7 |   9 |
|            11 |   1 |   3 | 10   |   7 |   9 |
|            12 |   1 |   3 | 3.9  |   7 |   9 |
|            13 |   1 |   3 | 11.3 |   7 |   9 |
|            14 |   1 |   3 |      |   7 |   9 |
|            15 |   1 |   3 |      |   7 |   9 |
|            16 |   1 |   3 |      |   7 |   9 |
|            17 |   1 |   3 |      |   7 |   9 |
|            18 |   1 |   3 |      |   7 |   9 |
|            19 |   1 |   3 |      |   7 |   9 |
|            20 |   1 |   3 |      |   7 |   9 |
+---------------+-----+-----+------+-----+-----+

I want to query last 7 rows data where the DATA column is not empty. Trying to achieve something like this:

+----+---+---+------+---+---+
|  7 | 1 | 3 | 10   | 7 | 9 |
|  8 | 1 | 3 | 7.8  | 7 | 9 |
|  9 | 1 | 3 |      | 7 | 9 |
| 10 | 1 | 3 | 6.8  | 7 | 9 |
| 11 | 1 | 3 | 10   | 7 | 9 |
| 12 | 1 | 3 | 3.9  | 7 | 9 |
| 13 | 1 | 3 | 11.3 | 7 | 9 |
+----+---+---+------+---+---+

But currently, I am only able to get the last 7 rows data which looks like this:

+---------------+-----+-----+------+-----+-----+
| Serial Number | LSL | LCL | DATA | UCL | USL |
+---------------+-----+-----+------+-----+-----+
|            14 |   1 |   3 |      |   7 |   9 |
|            15 |   1 |   3 |      |   7 |   9 |
|            16 |   1 |   3 |      |   7 |   9 |
|            17 |   1 |   3 |      |   7 |   9 |
|            18 |   1 |   3 |      |   7 |   9 |
|            19 |   1 |   3 |      |   7 |   9 |
|            20 |   1 |   3 |      |   7 |   9 |
+---------------+-----+-----+------+-----+-----+

The formula I used is:

=SORT(QUERY(Sheet1!A7:F,"order by A desc limit 7"),1,1)

This formula does not incorporate the condition that the last row of DATA column must not be empty. Is there a way to achieve what I am looking for?

CalamitousCode
  • 1,324
  • 1
  • 14
  • 21
Sai Ram
  • 43
  • 1
  • 9

2 Answers2

3

Assuming your serial numbers are consecutive and sorted as such.

=QUERY(A:F,"Select * where A >= "&ARRAYFORMULA(INDEX(SORT(A2:F,1,false),MATCH(true,ISNUMBER(INDEX(SORT(A2:F,1,false),,4)),0),1))-6&" limit 7")

Breakdown:

=QUERY(A:F,"Select * where A >= "

//index used to find the first serial number with a number in the data column
&ARRAYFORMULA(INDEX(

//reverse order
SORT(A2:F,1,false),

//find first number in data column of reversed data
MATCH(true,ISNUMBER(

//get fourth column (data column) to check for numbers
INDEX(SORT(A2:F,1,false),,4)

//minus 6 so you can get the 6 rows above and the row found
),0),1))-6

//get the first 7 rows from the serial number that matches.
&" limit 7")

Spreadsheet screenshot

EDIT

After our conversation:

If your first column is a date and your dates are consecutive with no duplicates, you can use this:

=QUERY(A:F,"Select * where A >= date '"&TEXT(INDEX(SORT(A2:F,1,false),MATCH(true,ISNUMBER(INDEX(SORT(A2:F,1,false),,4)),0),1)-6,"yyyy-mm-dd")&"' limit 7")

Breakdown:

=QUERY(A:F,"Select * where A >= date
//date tells query that it's looking for a date value

'"&TEXT(INDEX(SORT(A2:F,1,false),MATCH(true,ISNUMBER(INDEX(SORT(A2:F,1,false),,4)),0),1)-6,

"yyyy-mm-dd")&"' limit 7"))
//text formats the date in the way that query requires: yyyy-mm-dd

Second spreadsheet screenshot

CalamitousCode
  • 1,324
  • 1
  • 14
  • 21
  • Thank you very much. It works. I made a small modification to the above formula =QUERY(A2:F,"Select * where A >= "&ARRAYFORMULA(INDEX(SORT(A2:F,1,false),MATCH(true,ISNUMBER(INDEX(SORT(A2:F,1,false),,4)),0),1))-6&" limit 7") this is to make it work even if the table will have data which keeps getting added in the future. Thank you very much once again – Sai Ram Dec 21 '18 at 08:57
  • 1
    Of course. Great point. I will edit my answer. Glad I could help! – CalamitousCode Dec 21 '18 at 09:00
  • can this formula be used if my 1st column in a date format?. currently, I tried but facing error – Sai Ram Dec 21 '18 at 18:21
  • 1
    Are there any text values in A2:F? If not, try wrapping in VALUE(A2:F). – CalamitousCode Dec 21 '18 at 20:01
  • u mean like this =QUERY(VALUE(A2:F),"Select * where A >= "&ARRAYFORMULA(INDEX(SORT(A2:F,1,false),MATCH(true,ISNUMBER(INDEX(SORT(A2:F,1,false),,4)),0),1))-6&" limit 7") My current Date format - 11/29/2018. There is no Text Values in A2:F – Sai Ram Dec 22 '18 at 04:39
  • 1
    Try around the references inside SORT also. I'm not in front of a computer so I can't check. – CalamitousCode Dec 22 '18 at 04:41
  • I tried something like this =QUERY(VALUE(A2:F),"Select * where A >= "&ARRAYFORMULA(INDEX(SORT(VALUE(A2:F),1,false),MATCH(true,ISNUMBER(INDEX(SORT(VALUE(A2:F),1,false),,4)),0),1))-6&" limit 7") got an error message - An array value could not be found – Sai Ram Dec 22 '18 at 04:47
  • 1
    Wrap the whole formula in ARRAYFORMULA() – CalamitousCode Dec 22 '18 at 04:48
  • this was the formula i used =ARRAYFORMULA(QUERY(VALUE(A8:F),"Select * where A >= "&ARRAYFORMULA(INDEX(SORT(VALUE(A8:F),1,false),MATCH(true,ISNUMBER(INDEX(SORT(VALUE(A8:F),1,false),,4)),0),1))-6&" limit 7")) and i got this error - Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: A – Sai Ram Dec 22 '18 at 04:56
  • 1
    Change 'where A' to 'where Col1'. – CalamitousCode Dec 22 '18 at 04:57
  • I used =ARRAYFORMULA(QUERY(VALUE(A8:F),"Select * where Col1 >= "&ARRAYFORMULA(INDEX(SORT(VALUE(A8:F),1,false),MATCH(true,ISNUMBER(INDEX(SORT(VALUE(A8:F),1,false),,4)),0),1))-6&" limit 7")) now i was able to get the output but my data column values are all 0 – Sai Ram Dec 22 '18 at 05:10
  • you are a lifesaver. Can't thank you enough. It works. Thank you so much – Sai Ram Dec 22 '18 at 06:07
  • Glad to help! Please consider upvoting one of my helpful comments. :) – CalamitousCode Dec 22 '18 at 06:08
0

Based on description rather than example:

=query(query(A:F,"where D is not NULL order by A desc limit 7"),"order by Col1")

("last 7 rows data where the DATA column is not empty.")

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • 1
    this formula omits the null in the middle. I want to include that. only last row cannot be null value – Sai Ram Dec 21 '18 at 18:20