title: IF statement in Google Sheet QUERY
body: <p>I am creating a Google Sheet that sucks in values from Analytics and allows a user to go in, select various landing pages from a dropdown list, and source of traffic and see various stats relating to that source/landing page.</p>
  
  <p>It's all working apart from I want to have a 'all' selection in the sources dropdown.</p>
  
  <p>A simplified version of the formula I am using to generate the results is:</p>
  
  <pre><code>=IFERROR(QUERY('Sheet1'!$A$16:$M$6969,"select sum(E) where (A) contains '" &amp; $B$4 &amp; "' AND B = " &amp; MONTH(B$6) &amp; " AND C = " &amp; YEAR(B$6) &amp; " AND D = '" &amp; $B$3 &amp; "' label sum(E) ''",1),0)
  </code></pre>
  
  <p><code>A</code> is the landing page code, i.e. <code>landing-page</code></p>
  
  <p><code>B</code> and <code>C</code> just get a date from a header row</p>
  
  <p><code>D</code> is the one I am struggling with. At the moment <code>B3</code> is a dropdown containing <code>cpc</code>, <code>organic</code>, <code>direct</code>, <code>all</code>. If I select anything but <code>all</code> it works, but I need to write some sort of condition so if <code>all</code> is selected then <code>AND D = '" &amp; $B$3 &amp; "'</code> isn't used.</p>
  
  <p>I don't seem to be able to put an IF statement within a QUERY so unsure how to proceed!</p>