On the Quirks of INDEX()

I’ve been working on a self-updating bi-weekly deliverables calendar / dashboard in Google Spreadsheets for my office the past few days.  Our Traffic Manager can simply add projects and their various deliverables to other sheets which serve as databases. Then another sheet, which one of our animator a painstakingly organized for aesthetics, will search those databases to pull deliveries to their correct dates and color-coding based on other information associated with those projects.  This sheet updates itself based on the current day and week.

Recently, a switch I made from INDEX(FILTER(SORT(…))) to INDEX(QUERY(…)) for minimization purposes resulted in some peculiar behavior.

But first a preface as to why I am using INDEX for array formulas that are perfectly capable of expanding themselves down multiple rows.  Our animator has used the spreadsheet as a grid to help align elements, merging some cells and leaving some as borders.  This means that the cells no longer behave like a sequential spreadsheet.  For example, merged cells behave as only their top left cell.  Net result is that I need to write a specific formula for each identified locus of data.  Somewhat more time-consuming but not un-doable.

Anyway, so I was using INDEX() to return whole rows from my QUERY() via INDEX(QUERY([array], “select Col[#],Col[#] where …”),1).  This worked fine when the QUERY() pulled more than one row; however, when the QUERY() only pulled a single row, INDEX() only pulled the first entry where I wanted the whole first row, and where it would have pulled the non-existent second r0w, it pulled the single entry from the second column.

I cannot confirm my suspicion, but I surmise that QUERY(), as written by the programmer returns a one dimensional array when there is only one row of results and a two dimensional array when there are more.  This inconsistency means that INDEX(), when passed only a single number, pulls that index from an array, resulting in a single element from a one-dimensional array, and a whole row (or sub-array) in a two-dimensional array.

Unfortunately, to a naive user on the front-end who expects that number of their INDEX() function to always refer to a row, this is confusing and un-beneficial. I have not figured out how to solve this problem yet, and instead have broken up my QUERY() to multiple ones that only query single columns, thus guaranteeing that the dimensions of returned arrays are always 1xN.  It has maximized rather than minimized my formulas, but is more robust for the time being.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s