'If query is empty return "no data"

I'm using Google Sheets and my database looks like this:

enter image description here

I have a query formula that changes dynamically based on the month:

enter image description here

my query:

=QUERY('database',"SELECT B,C WHERE A = '"&A1&"'")

At the beginning of the months I don't have data, so I want to display something like this: enter image description here

I want the column userID permanent in all month, but the column name to display "NO DATA"

I already tried with IFNA or IFERROR but that won't work, because if there's no data, the query is still successful because it displays the column names.



Solution 1:[1]

You can try this longer combination of several formulas:

=arrayformula(
     query({A2:C;split(E1&"_"&unique(filter(B2:B,B2:B<>""))&"_NO DATA", "_")},
     "select Col2, max(Col3) where Col1 = '"&E1&"' 
                             group by Col2 
                             order by Col2 label max(Col3) ''", 0)
)

Where:

  • A2:C is the dictionary
  • B2:B is the column of userIDs
  • E1 is the Month

Outputs:

output output1 output3 output2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1