title: DAX How to return a table based on a condition - workaround for IF to return a table
body: <p>How to return DAX table based on a condition? The IF function cannot return a table in DAX.</p>
  <pre><code>IF( 1=1, table_1, table_2 )
  </code></pre>
  <p>It raises an error: <em>The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.</em></p>
  <p>I would like to use a slicer to choose between multiple tables, which table later can be used for the alternating dynamic filter context in CALCULATE.</p>
  <pre><code>CALCULATE( [Measure], 
     IF( Condition, 
         table_1,
         table_2
     )
  )
  </code></pre>
  <p>To make the problem more challenging I would like the table_1 and table_2 to have different set of columns. So combination of UNION and FILTER function won't do.</p>
  <pre><code>UNION(
     FILTER( table_1,     condition ),
     FILTER( table_2, NOT condition)
  )
  </code></pre>
  <p>As a possible approach, we might pick up a measure based on a slicer choice:</p>
  <pre><code>IF( Slicer_Selection_Condition, 
        [M1], // Measure with filter set 1
        [M2]  // Measure with filter set 2
     )
  </code></pre>
  <p>But I do not want to go this way because it multiplies the number of required measures for each slicer combination.</p>
  <p>If we could surpass the IF limitation, we could very usefully apply it. Imagine, we have a slicer to choose a measure among [Quantity], [Value], [Cost]. And we also have another slicer to choose the filter set. We could handle it with a one measure:</p>
  <pre><code>CALCULATE(
     SWITCH( Measure_Slicer, 1, [Quantity], 2, [Value],  [Cost] ), // measure choice
     SWITCH( Filter_Slicer,  1, table_1,    2, table_2,  table_3 ) // filter choice
  )
  </code></pre>
  <hr />
  <p>Here is a table to recreate problem:</p>
  <pre><code>Table = 
  DATATABLE (
      &quot;Color&quot;, STRING,
      &quot;Shape&quot;, STRING,
      &quot;Quantity&quot;, INTEGER,
      &quot;Value&quot;,    INTEGER,
      {
          { &quot;Red&quot;   , &quot;Circle&quot;  , 1, 10 },
          { &quot;Red&quot;   , &quot;Triangle&quot;, 1, 10 },
          { &quot;Blue&quot;  , &quot;Circle&quot;  , 1, 10 },
          { &quot;Blue&quot;  , &quot;Triangle&quot;, 1, 10 },
          { &quot;Yellow&quot;, &quot;Square&quot;  , 1, 10 }
      }
  )
  </code></pre>
  <p>And measures:</p>
  <pre><code>M_Quantity = SUM( 'Table'[Quantity] )
  </code></pre>
  <pre><code>M_Value    = SUM( 'Table'[Value] )
  </code></pre>
  <pre><code>Desired Measure = 
  VAR Measure_Slicer = 1   // Either 1 OR 2
  VAR Filter_Slicer  = 1   // Either 1 OR 2
  
  VAR table_1 = SUMMARIZE( 'Table', 'Table'[Color] )
  VAR table_2 = SUMMARIZE( 'Table', 'Table'[Color], 'Table'[Shape] )
  
  RETURN
  CALCULATE(
     SWITCH( Measure_Slicer, 1, [M_Quantity], [M_Value]), // Measure choice
     SWITCH( Filter_Slicer,  1, table_1     , table_2  )  // Filter choice
  )
  </code></pre>