google-sheetsgoogle-sheets-formula

Get the last non-blank value in a row in Google Sheets


I need to find the last value in row 20 of my Google Sheet spreadsheet. More data is added from time to time.

The formula I use for this is getting too long and I would need an easier way to get the same result without having to edit the formula each time a new column is inserted:

        =If(Q20 ="",
              If(P20 ="",
               If(O20 ="",
                If(N20 ="",
                 If(M20 ="",
                  If(L20 ="",
                   If(K20 ="",
                    If(J20 ="",
                     If(I20 ="",
                      If(H20 ="",
                       If(G20 ="",
                        If(F20 ="", "",
                           F20)
                        ,G20)
                       ,H20)
                      ,I20)
                     ,J20)
                    ,K20)
                   ,L20) 
                  ,M20)
                 ,N20) 
                ,O20)
               ,P20)
              ,Q20)

Solution

  • There are many ways to get the last non-blank value in a column. The fairly recent choosecols() and torow() functions let you do this with ease:

    =choosecols(torow(F20:Q20, 1), -1)
    

    That won't work in the sample spreadsheet shared in the question, though, because the formulas therein output zero-length strings "" instead of truly blank values through formulas like =IF(O19 ="","", O19+N20).

    Zero-length strings are not considered blank in Google Sheets. The easiest way to fix that is to use omit the "" bit, like this:

    =if(O19 = "", , O19 + N20)
    

    If those zero-length strings cannot be replaced with truly blank values for some reason, use filter(), like this:

    =choosecols(filter(F20:Q20, len(F20:Q20)), -1)
    

    Before these functions became available, the same had to be done with sort():

    =+sort(transpose(F20:Q20), transpose(column(F20:Q20) * sign(len(F20:Q20))), false)
    

    See choosecols() and torow().