# FAQ: FILTER(), LOOKUP(), MAXROW(), MINROW(), REF_ROWS(), and SELECT()

### Back-references within nested queries ([_THISROW-n])

Consider a complex App formula with ānestedā SELECT() expressions (SELECT() expressions within SELECT() expressions):

``````SELECT(
table[column1],
...
SELECT(
table[column2],
...
SELECT(
table[column3],
...
)
...
)
...
)
``````

Letās call the row for which this entire expression is evaluated as its App formula the Origin Row.

Within this entire expression, we can refer to column values of the Origin Row by dereferencing `[_THISROW]`. For instance, to get the value of the Customer Name column of the Origin Row, we can use `[_THISROW].[Customer Name]`.

Letās also name each of the nested queries within the entire expression:

• Query 1: `SELECT(table[column1], ...)` (outer-most query)
• Query 2: `SELECT(table[column2], ...)`
• Query 3: `SELECT(table[column3], ...)` (inner-most query)

Query 2 is nested inside Query 1. Within Query 2, column values of the row currently being examined by Query 1 can be accessed by dereferencing `[_THISROW-1]`. For instance, to get the value of the column1 column of the Query 1 row, we can use `[_THISROW-1].[column1]`. The `-1` in `_THISROW-1` refers to the query one step outside the current query.

Query 3 is nested inside Query 2. `[_THISROW-1]` refers to the query one step outside the current query, so within Query 3, `[_THISROW-1]` refers to the current row of Query 2. Query 2 is itself nested within Query 1, so Query 1 is two steps outside Query 3. `[_THISROW-2]` can be dereferenced to access the values of columns in the query two steps otside the current one: Query 1.

17 Likes

Iāve been trying to figure out a good use case, but I was struggling with the concept. But that chart makes it super easy. I hope you can add that image to the support docs at some point.

2 Likes

I hope to at some point. In the meantime, Iāve added a link to that reply to this FAQās index so itāll have some prominence here.

2 Likes

Come to think of it. Images similar to this would probably help in several of the support docs detailing some of the more complex stuff.

9 Likes

So to get the NEXT TO LAST or PENULTIMATE row would this work?

``````MAXROW(
"My Table",
"_ROWNUMBER-1",
([_THISROW]-1.[Thing] = [Thing])
)``````

Nope. Try:

``````INDEX(
ORDERBY(
FILTER(
"My Table",
([_THISROW].[Thing] = [Thing])
),
[_ROWNUMBER],
TRUE
),
2
)
``````
3 Likes

Hey @Steveā¦ Youāve expressed a complex thing with succinct elegance hereā¦ Iāve been struggling to grasp the ([_thisrow-n]) concept but I get it nowā¦ Cheersā¦

2 Likes

Thanks a lot for this @Steve

1 Like

Where should this expression be entered to have a unique list of rows?

Hi Steve,

I have a table with multiple columns. I wanted to have a unique list of rows for the following:

• list of unique values from [service towers] under [segment].
• list of unique values from [list transactions] under [service towers] under [ segment].

The list I hope to use in an interactive dashboard. Values will change after selecting [service towers] and [list transactions].

This is my first app and would appreciate all the guidance you can give.

Where should this code go? I tried entering this to slice but the result should be yes and no so it didnāt accept.

Is there any way to select maximum occurred value from the list of columns from List([col.1],[col.2],[col.3],[col.4],[col.5],ā¦)

Maybe column 1 Have apple, Column 2 have Orange, Column 3 have again Apple so display āAppleā as its occurred more times than orange ?

Not to my knowledge.

1 Like

Having similar values across several columns - especially to the point that you would want to count them - is a red flag indicator that you are probably not using the correct table design/structure for your app.

Appsheet is a row-based system, as are most data systems. It is best to use a normalized structure which simply put means you store the data as rows instead of columns so it can easily be processed as the system intended.

To expand on your example. Letās say your table has columns Fruit1, Fruit2, Fruit3, Fruit4, Fruit5ā¦etc. As soon as you have more fruit than columns, you have an issue. You would need to expand the table with more Fruit columns. But how many more? This is a non-normal form. You canāt know how many columns you need ahead of time. Couple that with the fact that there are no functions to help you count, sum, etc by columns and you will find that there is great difficulty building the app.

Instead, you would want to normalize the data, by creating a structure like so:

``````Item-Number    Fruit
1         apple
2         orange
3         apple
4         kumquat
5         tangerine
``````

This represents the same data, even the column numbers, but as rows.

By normalizing the data, you can add countless rows with no need to change the data structure. And you can then use the system functions to easily select rows for counting, summing ect. Life will be much better!!

I hope this helps!

2 Likes

Thanks a lot for your time.

I need to do like column only because I have few criterias and each column have 3 options that is selected from the table like you mentioned above. But for my requirement I need almost 11 column and each column Is different criteria like priority, price, item type etcā¦ but everything has a table that is listed and selected using select function.

For you reference. This is the table Iām using.

This is a normalized table. Why canāt you operate on this table as is? Where is it that you are using the ālikeā columns as suggested in this post above?

This table also looks normalized. Iām confused. Which columns relate to your original question shown above?

2 Likes

### How do I do VLOOKUP()?

In Excel, VLOOKUP() searches the first column in a range for a given search value and returns the value of another column in the same range from the row containing the matching value. The match can be exact or āapproximateā. An approximate match is the closest value less than or equal to the search value.

#### VLOOKUP() with exact match

The AppSheet equivalent to VLOOKUP() using exact match is LOOKUP():

``````LOOKUP(search-value, "table", "search-column", "result-column")
``````

where `search-value` is an expression that produces the value to find; `table` is the name of the table to search; `search-column` is the name of the column in which to look for the search value; and `result-column` is the name of the column whose value should be returned if the search value is found in the search column of that same row.

LOOKUP() returns at most only one value, from the first row in which it finds a match.

#### VLOOKUP() with approximate match

AppSheet has no direct equivalent for VLOOKUP() with approximate match, but this expression provides similar behavior:

``````LOOKUP(
ANY(
ORDERBY(
FILTER(
"table",
([search-column] <= search-value)
),
[search-column],
TRUE
)
),
"table",
"key-column",
"result-column"
)
``````

where `table` is the name of the table to search; `search-column` is the name of the column in which to look for the search value; `search-value` is an expression that produces the value to find; `key-column` is the name of the key column of the table; and `result-column` is the name of the column whose value should be returned if the value of the search column of that same row is less than or equal to the search term.

The expression above returns at most only one value, the value of the result column from the row with the highest search column value less than or equal to the search value.

Note that this expression is expensive and may hurt app performance.