databasesortinggoogle-sheets

Google Sheets adding a date in the array used for multiple criterion


In a Google Sheets database I have a date for each record.

So I like to add a date as a multiple criterion in the external array/table and possibly I need to use the operator <,>, <=,>=.

This is an example of the created sheet: https://docs.google.com/spreadsheets/d/1WvL1SD3uwGzEwWyepT2Gn0mALRVJjOvaoPcRpFvjYxE/edit?usp=sharing

I tried to use <01/01/2024 in the array/table but it doesn't work. I understand this is not completely clear. I meant before 01/01/2024. It should be 2023....or before.

I tried as well to copy the date from an external cell with "<" &A2 again without any result.


Solution

  • To summarize, you are using dsum() and have criteria that looks like <01/01/2024 in cell F3. You are asking how to make that criteria change as you edit the date in cell H2.

    To do that, get the date in cell H2 as text in the format it appears there, and add a < prefix, like this:

    ="<" & to_text(H2)
    

    That will work with dsum(), but be aware that it will not work the same way when embedded in a formula, because the & operator gets "raw" values, which in this case means the dateserial value in the cell. The dateserial for the date 1/1/2024 is 45292, and <45292 is probably not what you want, hence to_text().

    Further, in a formula context, =A2 < 01/01/2024 means "is cell A2 less than 0.0004940711462?" rather than "is cell A2 less than the date 1 January 2024?". One divided by one is one, and one divided by 1024 is 0.0004940711462.

    In a formula context, you can compare a value to the date 1 January 2024 like this:

    =A2 < value("2024-01-01")
    

    Dates often cause trouble because their format varies, and the spreadsheet locale makes a big difference. A date like 1/2/2024 may mean 2 January or 1 February depending on whether the spreadsheet is in the US locale or the UK locale, for example. In a German locale, 2 January would be entered as 2.1.2024. In the Netherlands, it would be entered as 2-1-2024. And so on.

    A value like 31/12/2024 means 31 December in the UK locale, but the same value will get interpreted as a text string in the US locale, and will thus not be usable as a numeric date. And vice versa. A value like 12/31/2024 means 31 December in the US locale, but the same value will get interpreted as a text string in the UK locale. If you suspect that the values you are comparing are not numeric dates but text strings that just look like dates, you can find whether the values are numeric with the isnumber() function.

    The best practice to avoid all that trouble is to use the ISO8601 standard date format yyyy-MM-dd consistently everywhere. It works the same way in any locale.

    For a detailed explanation, see Working with date and time values in Google Sheets.