Solution
=IF($B2="weekday", IFNA(QUERY('Base Data'!$A:$G, "SELECT sum(E) WHERE B = '"& D$1& "' AND G = '"& $A2& "' LABEL sum(E) ''", 0)),)
Equivalent but line breaks added to assist visually:
=IF($B2="weekday",
IFNA(
QUERY('Base Data'!$A:$G,
"SELECT sum(E)
WHERE B = '"& D$1& "' AND G = '"& $A2& "'
LABEL sum(E) '' ", 0)),)
Explanation
The IFNA-wrapped QUERY function in your original formula is the IF function's value_if_true argument:
=IF(logical_test, IFNA(QUERY), value_if_false)
QUERY follows function follows the syntax:
=QUERY(data, query_text, [headers])
Excluding its data argument, 'Base Data'!$A:$G
, and headers, 0
, what remains is the query_text:
=QUERY('Base Data'!$A:$G, "SELECT sum(E)
WHERE B = '"& D$1& "' AND G = '"& $A2& "'", 0)
and your LABEL sum(E)
would go at the end of it
"select sum(E) where B = '"& D$1& "' and G = '"& $A2&"' LABEL sum(E) '' "
Additional Notes:
- In Sheets, IF function doesn't require a value if you want to return a blank, just include the comma without a value:
=IF(1=2, valu_if_true, )
- IFNA doesn't require a value or a comma if you want to return a blank:
=IFNA(#N/A)
QUERY('Base Data'!$A:$G, "select sum(E) where B = '"&D$1&"' and G = '"&$A2&"'",0)
and LABEL goes inside the"SELECT..."
before the end:QUERY('Base Data'!$A:$G, "select sum(E) where B = '"&D$1&"' and G = '"&$A2&"' LABEL sum(E) ''",0)
Remember to include a space before Label. If you still get an error post the error message and I can help further.