I have the following query which is working fine. However I'd like to adde some additional logic if possible where if the there is a value in K3 then ideally the query would not use WHERE R = '"&$K2&"' in the query below but rather use WHERE D = '"&$K3&"'.
Here is a sample sheet. https://docs.google.com/spreadsheets/d/1Lt0RzwtuqIrxWYmWnYK4nh0JN2WdM0icpYF9oXGRfB8/edit?usp=sharing
QUERY(Transactions,"SELECT C,D,B,E WHERE R = '"&$K2&"' AND B > date '"&TEXT($C$2,"yyyy-mm-dd")&"' AND B <= date '"&TEXT($C$3,"yyyy-mm-dd")&"' ",1)