<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Using SQL declared variables into Power BI in Desktop</title>
    <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802381#M1428811</link>
    <description>&lt;P&gt;I have multiple SQL reports that declare more than one variable.&amp;nbsp; The SQL query below has only one variable utilizing dates, but most have multiple variables being declared&amp;nbsp; I would like to integrate these queries into Power BI so that the slicer whether it be date or something else show results that meet the criteria shown with the declared variable within the query.&amp;nbsp; I have reviewed multiple websites and watched many videos and for some reason can't get this to work utilizing managed parameters.&amp;nbsp; This is one of the few concepts of power bi that have yet to figure out. More than likely I am making this more difficult than it needs to be. Any thoughts on how I can get this to work in POwer BI? Thanks in advance for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--DECLARE @StartDate AS DATETIME DECLARE @EndDate AS DATETIME SET @StartDate = '8/4/2024' SET @EndDate = '8/10/2024'&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;SELECT&lt;BR /&gt;ih.ord_hdrnumber as OrderNum&lt;BR /&gt;, ih.ivh_deliverydate as Delivery&lt;BR /&gt;, orig.cty_name as Origin&lt;BR /&gt;, orig.cty_state as OSt&lt;BR /&gt;, orig.cty_region2 as OReg&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_name else legdest.cty_name end,'Temple') as Dest&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_state else legdest.cty_state end, 'TX') as DSt&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_region2 else legdest.cty_region2 end, 'TX-TEM') as DReg&lt;BR /&gt;, l.lgh_split_flag as SplitFlag&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 200 then 200 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMilesOG&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMiles&lt;BR /&gt;, (select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= @StartDate and ih2.ivh_deliverydate &amp;lt; @EndDate+1 and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5 order by ih2.ivh_deliverydate desc) as Rate&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end&lt;BR /&gt;*&lt;BR /&gt;(select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= @StartDate and ih2.ivh_deliverydate &amp;lt; @EndDate+1 and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5) as BillMTMi&lt;BR /&gt;, (select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= @StartDate+3 and ih3.ivh_deliverydate &amp;lt; @EndDate+1) as FSCRate&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end&lt;BR /&gt;*&lt;BR /&gt;(select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= @StartDate+3 and ih3.ivh_deliverydate &amp;lt; @EndDate+1) as BillMTFSC&lt;BR /&gt;, ih.ivh_totalcharge as BHGrossAmt&lt;BR /&gt;, (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number) as LastStopID&lt;BR /&gt;, isnull(s.stp_lgh_mileage,25) as LastStopMiles&lt;BR /&gt;, .75 as RPM&lt;BR /&gt;, .75 * s.stp_lgh_mileage as ReduceBHAmount&lt;BR /&gt;, case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge else ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)) end as BHTotal&lt;BR /&gt;, case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge*-1 else ((ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)))*.8)*-1 end as BHCredit&lt;BR /&gt;, IH.IVH_BILLTO&lt;BR /&gt;FROM invoiceheader ih (NOLOCK)&lt;BR /&gt;INNER JOIN city orig (NOLOCK) ON orig.cty_code = ih.ivh_origincity&lt;BR /&gt;INNER JOIN city dest (NOLOCK) ON dest.cty_code = ih.ivh_destcity&lt;BR /&gt;LEFT JOIN legheader l (NOLOCK) ON l.ord_hdrnumber = ih.ord_hdrnumber&lt;BR /&gt;LEFT JOIN city legdest (NOLOCK) ON legdest.cty_code = l.lgh_endcity&lt;BR /&gt;LEFT JOIN stops s (NOLOCK) ON s.mov_number = ih.mov_number and s.stp_mfh_sequence = (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number)&lt;BR /&gt;WHERE ih.ivh_revtype3 = 'SC-BH'&lt;BR /&gt;AND ih.ivh_billto NOT LIKE 'STATEM%'&lt;BR /&gt;AND ih.ivh_deliverydate &amp;gt;= @StartDate and ih.ivh_deliverydate &amp;lt; @EndDate+1&lt;BR /&gt;AND (l.lgh_split_flag IN ('N','S') OR l.lgh_split_flag is null)&lt;BR /&gt;AND ih.ivh_invoicenumber = (SELECT max(ih2.ivh_invoicenumber) FROM invoiceheader ih2 (NOLOCK) WHERE ih2.ord_hdrnumber = ih.ord_hdrnumber and right(ivh_invoicenumber,1) &amp;lt;&amp;gt; 'B')&lt;BR /&gt;ORDER BY MTMilesOG desc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I tried to change the M-code to reflect the date parameter I get the below message:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expression.Error: We cannot apply operator &amp;amp; to types Text and Date.&lt;BR /&gt;Details:&lt;BR /&gt;Operator=&amp;amp;&lt;BR /&gt;Left=SELECT&lt;BR /&gt;ih.ord_hdrnumber as OrderNum&lt;BR /&gt;, CAST(ih.ivh_deliverydate as Date) as Delivery&lt;BR /&gt;, orig.cty_name as Origin&lt;BR /&gt;, orig.cty_state as OSt&lt;BR /&gt;, orig.cty_region2 as OReg&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_name else legdest.cty_name end,'Temple') as Dest&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_state else legdest.cty_state end, 'TX') as DSt&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_region2 else legdest.cty_region2 end, 'TX-TEM') as DReg&lt;BR /&gt;, l.lgh_split_flag as SplitFlag&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 200 then 200 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMilesOG&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_lo...&lt;BR /&gt;Right=8/19/2025&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Aug 2025 13:28:05 GMT</pubDate>
    <dc:creator>cheid_4838</dc:creator>
    <dc:date>2025-08-20T13:28:05Z</dc:date>
    <item>
      <title>Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802381#M1428811</link>
      <description>&lt;P&gt;I have multiple SQL reports that declare more than one variable.&amp;nbsp; The SQL query below has only one variable utilizing dates, but most have multiple variables being declared&amp;nbsp; I would like to integrate these queries into Power BI so that the slicer whether it be date or something else show results that meet the criteria shown with the declared variable within the query.&amp;nbsp; I have reviewed multiple websites and watched many videos and for some reason can't get this to work utilizing managed parameters.&amp;nbsp; This is one of the few concepts of power bi that have yet to figure out. More than likely I am making this more difficult than it needs to be. Any thoughts on how I can get this to work in POwer BI? Thanks in advance for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--DECLARE @StartDate AS DATETIME DECLARE @EndDate AS DATETIME SET @StartDate = '8/4/2024' SET @EndDate = '8/10/2024'&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;SELECT&lt;BR /&gt;ih.ord_hdrnumber as OrderNum&lt;BR /&gt;, ih.ivh_deliverydate as Delivery&lt;BR /&gt;, orig.cty_name as Origin&lt;BR /&gt;, orig.cty_state as OSt&lt;BR /&gt;, orig.cty_region2 as OReg&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_name else legdest.cty_name end,'Temple') as Dest&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_state else legdest.cty_state end, 'TX') as DSt&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_region2 else legdest.cty_region2 end, 'TX-TEM') as DReg&lt;BR /&gt;, l.lgh_split_flag as SplitFlag&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 200 then 200 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMilesOG&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMiles&lt;BR /&gt;, (select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= @StartDate and ih2.ivh_deliverydate &amp;lt; @EndDate+1 and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5 order by ih2.ivh_deliverydate desc) as Rate&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end&lt;BR /&gt;*&lt;BR /&gt;(select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= @StartDate and ih2.ivh_deliverydate &amp;lt; @EndDate+1 and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5) as BillMTMi&lt;BR /&gt;, (select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= @StartDate+3 and ih3.ivh_deliverydate &amp;lt; @EndDate+1) as FSCRate&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end&lt;BR /&gt;*&lt;BR /&gt;(select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= @StartDate+3 and ih3.ivh_deliverydate &amp;lt; @EndDate+1) as BillMTFSC&lt;BR /&gt;, ih.ivh_totalcharge as BHGrossAmt&lt;BR /&gt;, (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number) as LastStopID&lt;BR /&gt;, isnull(s.stp_lgh_mileage,25) as LastStopMiles&lt;BR /&gt;, .75 as RPM&lt;BR /&gt;, .75 * s.stp_lgh_mileage as ReduceBHAmount&lt;BR /&gt;, case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge else ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)) end as BHTotal&lt;BR /&gt;, case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge*-1 else ((ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)))*.8)*-1 end as BHCredit&lt;BR /&gt;, IH.IVH_BILLTO&lt;BR /&gt;FROM invoiceheader ih (NOLOCK)&lt;BR /&gt;INNER JOIN city orig (NOLOCK) ON orig.cty_code = ih.ivh_origincity&lt;BR /&gt;INNER JOIN city dest (NOLOCK) ON dest.cty_code = ih.ivh_destcity&lt;BR /&gt;LEFT JOIN legheader l (NOLOCK) ON l.ord_hdrnumber = ih.ord_hdrnumber&lt;BR /&gt;LEFT JOIN city legdest (NOLOCK) ON legdest.cty_code = l.lgh_endcity&lt;BR /&gt;LEFT JOIN stops s (NOLOCK) ON s.mov_number = ih.mov_number and s.stp_mfh_sequence = (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number)&lt;BR /&gt;WHERE ih.ivh_revtype3 = 'SC-BH'&lt;BR /&gt;AND ih.ivh_billto NOT LIKE 'STATEM%'&lt;BR /&gt;AND ih.ivh_deliverydate &amp;gt;= @StartDate and ih.ivh_deliverydate &amp;lt; @EndDate+1&lt;BR /&gt;AND (l.lgh_split_flag IN ('N','S') OR l.lgh_split_flag is null)&lt;BR /&gt;AND ih.ivh_invoicenumber = (SELECT max(ih2.ivh_invoicenumber) FROM invoiceheader ih2 (NOLOCK) WHERE ih2.ord_hdrnumber = ih.ord_hdrnumber and right(ivh_invoicenumber,1) &amp;lt;&amp;gt; 'B')&lt;BR /&gt;ORDER BY MTMilesOG desc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I tried to change the M-code to reflect the date parameter I get the below message:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expression.Error: We cannot apply operator &amp;amp; to types Text and Date.&lt;BR /&gt;Details:&lt;BR /&gt;Operator=&amp;amp;&lt;BR /&gt;Left=SELECT&lt;BR /&gt;ih.ord_hdrnumber as OrderNum&lt;BR /&gt;, CAST(ih.ivh_deliverydate as Date) as Delivery&lt;BR /&gt;, orig.cty_name as Origin&lt;BR /&gt;, orig.cty_state as OSt&lt;BR /&gt;, orig.cty_region2 as OReg&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_name else legdest.cty_name end,'Temple') as Dest&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_state else legdest.cty_state end, 'TX') as DSt&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_region2 else legdest.cty_region2 end, 'TX-TEM') as DReg&lt;BR /&gt;, l.lgh_split_flag as SplitFlag&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 200 then 200 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMilesOG&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_lo...&lt;BR /&gt;Right=8/19/2025&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 13:28:05 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802381#M1428811</guid>
      <dc:creator>cheid_4838</dc:creator>
      <dc:date>2025-08-20T13:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802556#M1428852</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="677608" data-lia-user-login="cheid_4838" class="lia-mention lia-mention-user"&gt;cheid_4838&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Looks like the error is due to trying concate with a text and date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this:&lt;/P&gt;&lt;P&gt;let&lt;BR /&gt;// Define Start and End dates as Power Query parameters or fixed values&lt;BR /&gt;StartDate = Date.ToText(#date(2024, 8, 4), "MM/dd/yyyy"),&lt;BR /&gt;EndDate = Date.ToText(#date(2024, 8, 10), "MM/dd/yyyy"),&lt;/P&gt;&lt;P&gt;// Construct the full SQL query string with declared variables&lt;BR /&gt;SqlQuery =&lt;BR /&gt;"DECLARE @StartDate AS DATETIME; " &amp;amp;&lt;BR /&gt;"DECLARE @EndDate AS DATETIME; " &amp;amp;&lt;BR /&gt;"SET @StartDate = '" &amp;amp; StartDate &amp;amp; "'; " &amp;amp;&lt;BR /&gt;"SET @EndDate = '" &amp;amp; EndDate &amp;amp; "'; " &amp;amp;&lt;/P&gt;&lt;P&gt;"SELECT " &amp;amp;&lt;BR /&gt;"ih.ord_hdrnumber as OrderNum, " &amp;amp;&lt;BR /&gt;"CAST(ih.ivh_deliverydate as Date) as Delivery, " &amp;amp;&lt;BR /&gt;"orig.cty_name as Origin, " &amp;amp;&lt;BR /&gt;"orig.cty_state as OSt, " &amp;amp;&lt;BR /&gt;"orig.cty_region2 as OReg, " &amp;amp;&lt;BR /&gt;"ISNULL(CASE WHEN l.lgh_split_flag = 'N' THEN dest.cty_name ELSE legdest.cty_name END,'Temple') as Dest, " &amp;amp;&lt;BR /&gt;"ISNULL(CASE WHEN l.lgh_split_flag = 'N' THEN dest.cty_state ELSE legdest.cty_state END, 'TX') as DSt, " &amp;amp;&lt;BR /&gt;"ISNULL(CASE WHEN l.lgh_split_flag = 'N' THEN dest.cty_region2 ELSE legdest.cty_region2 END, 'TX-TEM') as DReg, " &amp;amp;&lt;BR /&gt;"l.lgh_split_flag as SplitFlag, " &amp;amp;&lt;BR /&gt;"CASE WHEN ISNULL((SELECT SUM(ISNULL(stp_lgh_mileage, 0)) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus IN ('MT','BT')),0) &amp;gt; 200 " &amp;amp;&lt;BR /&gt;"THEN 200 ELSE ISNULL((SELECT SUM(ISNULL(stp_lgh_mileage, 0)) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus IN ('MT','BT')),0) END as MTMilesOG, " &amp;amp;&lt;BR /&gt;"/* Add remaining columns here as in your full query, properly concatenated with &amp;amp; and enclosed in quotes */ " &amp;amp;&lt;BR /&gt;"ih.ivh_totalcharge as BHGrossAmt, " &amp;amp;&lt;BR /&gt;"(SELECT MAX(stp_mfh_sequence) FROM stops s (NOLOCK) WHERE s.mov_number = ih.mov_number) as LastStopID " &amp;amp;&lt;BR /&gt;"FROM invoiceheader ih (NOLOCK) " &amp;amp;&lt;BR /&gt;"INNER JOIN city orig (NOLOCK) ON orig.cty_code = ih.ivh_origincity " &amp;amp;&lt;BR /&gt;"INNER JOIN city dest (NOLOCK) ON dest.cty_code = ih.ivh_destcity " &amp;amp;&lt;BR /&gt;"LEFT JOIN legheader l (NOLOCK) ON l.ord_hdrnumber = ih.ord_hdrnumber " &amp;amp;&lt;BR /&gt;"LEFT JOIN city legdest (NOLOCK) ON legdest.cty_code = l.lgh_endcity " &amp;amp;&lt;BR /&gt;"LEFT JOIN stops s (NOLOCK) ON s.mov_number = ih.mov_number AND s.stp_mfh_sequence = (SELECT MAX(stp_mfh_sequence) FROM stops s (NOLOCK) WHERE s.mov_number = ih.mov_number) " &amp;amp;&lt;BR /&gt;"WHERE ih.ivh_revtype3 = 'SC-BH' " &amp;amp;&lt;BR /&gt;"AND ih.ivh_billto NOT LIKE 'STATEM%' " &amp;amp;&lt;BR /&gt;"AND ih.ivh_deliverydate &amp;gt;= @StartDate " &amp;amp;&lt;BR /&gt;"AND ih.ivh_deliverydate &amp;lt; DATEADD(day, 1, @EndDate) " &amp;amp;&lt;BR /&gt;"AND (l.lgh_split_flag IN ('N','S') OR l.lgh_split_flag IS NULL) " &amp;amp;&lt;BR /&gt;"AND ih.ivh_invoicenumber = (SELECT MAX(ih2.ivh_invoicenumber) FROM invoiceheader ih2 (NOLOCK) WHERE ih2.ord_hdrnumber = ih.ord_hdrnumber AND RIGHT(ivh_invoicenumber, 1) &amp;lt;&amp;gt; 'B') " &amp;amp;&lt;BR /&gt;"ORDER BY MTMilesOG DESC"&lt;BR /&gt;,&lt;/P&gt;&lt;P&gt;// Connect to SQL Server Database (change Server and Database names)&lt;BR /&gt;Source = Sql.Database("your_server_name", "your_database_name", [Query=SqlQuery])&lt;BR /&gt;in&lt;BR /&gt;Source&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 15:25:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802556#M1428852</guid>
      <dc:creator>MohamedFowzan1</dc:creator>
      <dc:date>2025-08-20T15:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802563#M1428855</link>
      <description>&lt;H2&gt;How to Use Multiple SQL Variables with Power BI Filters (Without Losing Your Mind)&lt;/H2&gt;&lt;H3&gt;1. &lt;span class="lia-unicode-emoji" title=":cross_mark:"&gt;❌&lt;/span&gt; Stop Using&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;DECLARE&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;SET&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Like It’s 1999&lt;/H3&gt;&lt;P&gt;Look, Power BI isn’t your SQL Server mate who tolerates your old-school habits. If you try to slap in a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;DECLARE @StartDate&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;or&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;SET @EndDate = '2024-08-10', Power BI will throw a tantrum and spit out errors like a toddler denied sweets.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Instead&lt;/STRONG&gt;, use Power BI’s own parameters — they’re like polite little boxes that hold your values and don’t complain.&lt;/P&gt;&lt;HR /&gt;&lt;H3&gt;2. 🧱 Build Your Parameters Like a Pro&lt;/H3&gt;&lt;P&gt;In Power BI Desktop:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Go to&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Transform Data&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;→&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Manage Parameters&lt;/STRONG&gt;.&lt;/LI&gt;&lt;LI&gt;Create parameters like:&lt;UL&gt;&lt;LI&gt;StartDate&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(type: Date)&lt;/LI&gt;&lt;LI&gt;EndDate&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(type: Date)&lt;/LI&gt;&lt;LI&gt;Anything else you fancy —&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Customer,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Region,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Favourite Biscuit, whatever.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;These are your new best friends. Treat them well.&lt;/P&gt;&lt;HR /&gt;&lt;H3&gt;3. 🧙‍&lt;span class="lia-unicode-emoji" title=":male_sign:"&gt;♂️&lt;/span&gt; Use the Parameters in Power Query (a.k.a. the M Code Dungeon)&lt;/H3&gt;&lt;P&gt;Now, here’s where the magic happens. You need to&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;build your SQL query dynamically&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;using M code (look down).Think of it like crafting a spell — but instead of summoning dragons, you’re summoning data.&lt;/P&gt;&lt;P&gt;let&lt;BR /&gt;StartDate = Date.ToText(Parametros[StartDate], "yyyy-MM-dd"),&lt;BR /&gt;EndDate = Date.ToText(Parametros[EndDate], "yyyy-MM-dd"),&lt;BR /&gt;ConsultaSQL = "&lt;BR /&gt;SELECT * FROM YourTable&lt;BR /&gt;WHERE DeliveryDate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "'&lt;BR /&gt;AND DeliveryDate &amp;lt; DATEADD(day, 1, '" &amp;amp; EndDate &amp;amp; "')"&lt;BR /&gt;in&lt;BR /&gt;Sql.Database("YourServer", "YourDatabase", [Query=ConsultaSQL])&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;H3&gt;&lt;SPAN&gt;4.&amp;nbsp;&lt;/SPAN&gt;Avoid&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;NOLOCK&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and Subquery Chaos&lt;/H3&gt;&lt;P&gt;Yes, I know —&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;NOLOCK&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;feels like a shortcut. But in Power BI, it’s more like trying to sneak into a nightclub with a fake ID. It might work, but it’s dodgy and could get you kicked out.&lt;/P&gt;&lt;P&gt;If your query looks like spaghetti code, consider moving the heavy lifting to a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;SQL view&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and let Power BI just do the filtering. Keep it lean, keep it clean.&lt;/P&gt;&lt;HR /&gt;&lt;H3&gt;5. 🧠 Advanced Trick: Custom Functions&lt;/H3&gt;&lt;P&gt;Feeling brave? You can create a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Power Query function&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;that takes in parameters and builds your SQL on the fly. It’s like building your own robot butler — posh, powerful, and slightly over-engineered.&lt;/P&gt;&lt;P&gt;Perfect if you’ve got loads of variables and want to keep things tidy.&lt;/P&gt;&lt;HR /&gt;&lt;H3&gt;🧠 Final Thought&lt;/H3&gt;&lt;P&gt;You’re not alone — loads of budding analysts overcomplicate this stuff. The trick is to&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;ditch the old SQL habits&lt;/STRONG&gt;, embrace Power BI’s way of doing things, and keep your queries clean like your nan’s kitchen.&lt;/P&gt;&lt;HR /&gt;&lt;H3&gt;&lt;span class="lia-unicode-emoji" title=":raising_hands:"&gt;🙌&lt;/span&gt; Fancy Giving Me a Kudos?&lt;/H3&gt;&lt;P&gt;If this guide helped you stop yelling at Power BI and start making it behave, I’d be well chuffed if you gave it a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Kudos&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;on the Microsoft forums. It helps others find the fix and makes me look clever. Win-win, innit? Cheers, legend! &lt;span class="lia-unicode-emoji" title=":clinking_beer_mugs:"&gt;🍻&lt;/span&gt;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 20 Aug 2025 15:30:33 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802563#M1428855</guid>
      <dc:creator>CPCARDOSO</dc:creator>
      <dc:date>2025-08-20T15:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802595#M1428864</link>
      <description>&lt;P&gt;Thanks for the response. The delcare statements were not meant for importing into power bi. They came from an SSRS report that I am working on converting to Power BI. I will let you know if I get it to work.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 15:58:15 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802595#M1428864</guid>
      <dc:creator>cheid_4838</dc:creator>
      <dc:date>2025-08-20T15:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802778#M1428911</link>
      <description>&lt;P&gt;I got the query to accept the parameter using the below M-code.&amp;nbsp; I have two questions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; The parameters are showing up in my facts table and also two seperate queries (Start/End Dates).&amp;nbsp; Does that sound correct?&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2.&amp;nbsp; I was expecting to see a see a start and end table in the model view, but it's not there.&amp;nbsp; Why would the two parameters not show up as tables in model view?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;let&lt;BR /&gt;// Define Start and End dates as Power Query parameters or fixed values&lt;BR /&gt;StartDate = Date.ToText(#date(2024, 8, 4), "MM/dd/yyyy"),&lt;BR /&gt;EndDate = Date.ToText(#date(2024, 8, 10), "MM/dd/yyyy"),&lt;BR /&gt;Source = Sql.Database("VLCCI-TMWSQL03", "TMWSuite_Transcorr", [Query="SELECT#(lf) ih.ord_hdrnumber as OrderNum#(lf), CAST(ih.ivh_deliverydate as Date) as Delivery#(lf), orig.cty_name as Origin#(lf), orig.cty_state as OSt#(lf), orig.cty_region2 as OReg#(lf), ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_name else legdest.cty_name end,'Temple') as Dest#(lf), ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_state else legdest.cty_state end, 'TX') as DSt#(lf), ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_region2 else legdest.cty_region2 end, 'TX-TEM') as DReg#(lf), l.lgh_split_flag as SplitFlag#(lf), case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 200 then 200 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMilesOG#(lf), case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMiles#(lf), (select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "' and ih2.ivh_deliverydate &amp;lt; '" &amp;amp; EndDate &amp;amp; "' and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5 order by CAST(ih2.ivh_deliverydate as Date) desc) as Rate#(lf), case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end #(lf) *#(lf) (select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "' and ih2.ivh_deliverydate &amp;lt; '" &amp;amp; EndDate &amp;amp; "' and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5) as BillMTMi#(lf), (select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "' and ih3.ivh_deliverydate &amp;lt; '" &amp;amp; EndDate &amp;amp; "') as FSCRate#(lf), case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end#(lf) *#(lf) (select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "' and ih3.ivh_deliverydate &amp;lt; '" &amp;amp; EndDate &amp;amp; "') as BillMTFSC#(lf), ih.ivh_totalcharge as BHGrossAmt#(lf), (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number) as LastStopID#(lf), isnull(s.stp_lgh_mileage,25) as LastStopMiles#(lf), .75 as RPM#(lf), .75 * s.stp_lgh_mileage as ReduceBHAmount#(lf), case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge else ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)) end as BHTotal#(lf), case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge*-1 else ((ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)))*.8)*-1 end as BHCredit#(lf), IH.IVH_BILLTO#(lf)FROM invoiceheader ih (NOLOCK)#(lf)INNER JOIN city orig (NOLOCK) ON orig.cty_code = ih.ivh_origincity#(lf)INNER JOIN city dest (NOLOCK) ON dest.cty_code = ih.ivh_destcity#(lf)LEFT JOIN legheader l (NOLOCK) ON l.ord_hdrnumber = ih.ord_hdrnumber#(lf)LEFT JOIN city legdest (NOLOCK) ON legdest.cty_code = l.lgh_endcity#(lf)LEFT JOIN stops s (NOLOCK) ON s.mov_number = ih.mov_number and s.stp_mfh_sequence = (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number) #(lf)WHERE ih.ivh_revtype3 = 'SC-BH'#(lf)AND ih.ivh_billto NOT LIKE 'STATEM%'#(lf)AND ih.ivh_deliverydate&amp;gt;= '" &amp;amp; StartDate &amp;amp; "' and ih.ivh_deliverydate&amp;lt; '" &amp;amp; EndDate &amp;amp; "' #(lf)AND (l.lgh_split_flag IN ('N','S') OR l.lgh_split_flag is null)#(lf)AND ih.ivh_invoicenumber = (SELECT max(ih2.ivh_invoicenumber) FROM invoiceheader ih2 (NOLOCK) WHERE ih2.ord_hdrnumber = ih.ord_hdrnumber and right(ivh_invoicenumber,1) &amp;lt;&amp;gt; 'B')#(lf)ORDER BY MTMilesOG desc"]),&lt;BR /&gt;#"Changed Type" = Table.TransformColumnTypes(Source,{{"Delivery", type date}})&lt;BR /&gt;in&lt;BR /&gt;#"Changed Type"&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 20:23:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4802778#M1428911</guid>
      <dc:creator>cheid_4838</dc:creator>
      <dc:date>2025-08-20T20:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4803577#M1429100</link>
      <description>&lt;P&gt;When you create parameters in Power BI using Power Query, they behave like mini queries — so yes, they’ll show up in the Queries pane and might sneak into your data model if you’ve accidentally left “Enable Load” turned on. If you see them in your facts table, it’s likely because they were loaded as queries. To fix that, just right-click on each parameter (like&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;StartDate&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;EndDate) in Power Query and untick “Enable Load” — that’ll keep them out of your report visuals and model clutter.&lt;/P&gt;&lt;P&gt;Now, if you’re wondering why these parameters don’t show up in the model view as tables, it’s because they’re not tables at all — they’re just single values (a.k.a. scalars). Power BI’s model view only shows proper tables and relationships, not these behind-the-scenes helpers. Think of parameters like salt in a recipe: absolutely essential, but you don’t see it sitting on the plate.&lt;BR /&gt;&lt;BR /&gt;Look that:&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/power-bi/connect-data/refresh-include-in-report-refresh" target="_blank"&gt;Managing query refresh - Power BI | Microsoft Learn&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Aug 2025 10:59:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4803577#M1429100</guid>
      <dc:creator>CPCARDOSO</dc:creator>
      <dc:date>2025-08-21T10:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4803904#M1429181</link>
      <description>&lt;P&gt;I am getting closer to getting this to work.&amp;nbsp; Now the problem I am having is that Bind to Parameter is not showing up after I follow all instructions in this link (&lt;A href="https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-dynamic-m-query-parameters#considerations-and-limitations" target="_blank"&gt;https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-dynamic-m-query-parameters#considerations-and-limitations&lt;/A&gt;).&amp;nbsp; Below is the current M-code that includes the parameters I created.&amp;nbsp; Am I doing something wrong?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;let&lt;/P&gt;&lt;P&gt;StartDate = Date.ToText(#date(2024, 8, 4), "MM/dd/yyyy"),&lt;/P&gt;&lt;P&gt;EndDate = Date.ToText(#date(2024, 8, 10), "MM/dd/yyyy"),&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Source =&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sql.Database("VLCCI-TMWSQL03", "TMWSuite_Transcorr",&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Query="SELECT#(lf)&amp;nbsp; ih.ord_hdrnumber as OrderNum#(lf), CAST(ih.ivh_deliverydate as Date) as Delivery#(lf), orig.cty_name as Origin#(lf), orig.cty_state as OSt#(lf), orig.cty_region2 as OReg#(lf), ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_name else legdest.cty_name end,'Temple') as Dest#(lf), ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_state else legdest.cty_state end, 'TX') as DSt#(lf), ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_region2 else legdest.cty_region2 end, 'TX-TEM') as DReg#(lf), l.lgh_split_flag as SplitFlag#(lf), case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 200 then 200 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMilesOG#(lf), case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMiles#(lf), (select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "'&amp;nbsp; and ih2.ivh_deliverydate &amp;lt; '" &amp;amp; EndDate &amp;amp; "'&amp;nbsp; and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5 order by CAST(ih2.ivh_deliverydate as Date) desc) as Rate#(lf), case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end #(lf)&amp;nbsp; *#(lf)&amp;nbsp; (select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "'&amp;nbsp; and ih2.ivh_deliverydate &amp;lt; '" &amp;amp; EndDate &amp;amp; "'&amp;nbsp; and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5) as BillMTMi#(lf), (select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "'&amp;nbsp;&amp;nbsp; and ih3.ivh_deliverydate &amp;lt; '" &amp;amp; EndDate &amp;amp; "' ) as FSCRate#(lf), case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end#(lf)&amp;nbsp; *#(lf)&amp;nbsp; (select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= '" &amp;amp; StartDate &amp;amp; "'&amp;nbsp;&amp;nbsp; and ih3.ivh_deliverydate &amp;lt; '" &amp;amp; EndDate &amp;amp; "' ) as BillMTFSC#(lf), ih.ivh_totalcharge as BHGrossAmt#(lf), (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number) as LastStopID#(lf), isnull(s.stp_lgh_mileage,25) as LastStopMiles#(lf), .75 as RPM#(lf), .75 * s.stp_lgh_mileage as ReduceBHAmount#(lf), case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge else ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)) end as BHTotal#(lf), case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge*-1 else ((ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)))*.8)*-1 end as BHCredit#(lf), IH.IVH_BILLTO#(lf)FROM invoiceheader ih (NOLOCK)#(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; INNER JOIN city orig (NOLOCK) ON orig.cty_code = ih.ivh_origincity#(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; INNER JOIN city dest (NOLOCK) ON dest.cty_code = ih.ivh_destcity#(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LEFT JOIN legheader l (NOLOCK) ON l.ord_hdrnumber = ih.ord_hdrnumber#(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LEFT JOIN city legdest (NOLOCK) ON legdest.cty_code = l.lgh_endcity#(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LEFT JOIN stops s (NOLOCK) ON s.mov_number = ih.mov_number and s.stp_mfh_sequence = (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number) #(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ih.ivh_revtype3 = 'SC-BH'#(lf)AND ih.ivh_billto NOT LIKE 'STATEM%'#(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND ih.ivh_deliverydate&amp;gt;= '" &amp;amp; StartDate &amp;amp; "'&amp;nbsp;&amp;nbsp; and ih.ivh_deliverydate&amp;lt; '" &amp;amp; EndDate &amp;amp; "'&amp;nbsp; #(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND (l.lgh_split_flag IN ('N','S') OR l.lgh_split_flag is null)#(lf)AND ih.ivh_invoicenumber = (SELECT max(ih2.ivh_invoicenumber) FROM invoiceheader ih2 (NOLOCK) WHERE ih2.ord_hdrnumber = ih.ord_hdrnumber and right(ivh_invoicenumber,1) &amp;lt;&amp;gt; 'B')#(lf)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ORDER BY MTMilesOG desc"])&lt;/P&gt;&lt;P&gt;in&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Source&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Aug 2025 15:05:23 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4803904#M1429181</guid>
      <dc:creator>cheid_4838</dc:creator>
      <dc:date>2025-08-21T15:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4803926#M1429188</link>
      <description>&lt;P&gt;I think the reason for not seeing "Bind to Parameters" is because I am not using DirectQuery Mode.&amp;nbsp; When I load the source SQL query in DirectQuery Mode I get a message that "This query contains transportations that can't be used for DirectQuery.&amp;nbsp; I am not that familiar with DirectQuery. Everything I do is imported.&amp;nbsp; What about this logic doesn't DirectQuery like?&amp;nbsp; Is there something I should be looking for?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SELECT&lt;BR /&gt;ih.ord_hdrnumber as OrderNum&lt;BR /&gt;, ih.ivh_deliverydate as Delivery&lt;BR /&gt;, orig.cty_name as Origin&lt;BR /&gt;, orig.cty_state as OSt&lt;BR /&gt;, orig.cty_region2 as OReg&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_name else legdest.cty_name end,'Temple') as Dest&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_state else legdest.cty_state end, 'TX') as DSt&lt;BR /&gt;, ISNULL(case when l.lgh_split_flag = 'N' then dest.cty_region2 else legdest.cty_region2 end, 'TX-TEM') as DReg&lt;BR /&gt;, l.lgh_split_flag as SplitFlag&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 200 then 200 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMilesOG&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end as MTMiles&lt;BR /&gt;, (select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= '08-01-2025' and ih2.ivh_deliverydate &amp;lt; '08-20-2025' and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5 order by CAST(ih2.ivh_deliverydate as Date) desc) as Rate&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end&lt;BR /&gt;*&lt;BR /&gt;(select top 1 ih2.ivh_rate from invoiceheader ih2 (nolock) where ih2.ivh_billto = 'STATEM' and ih2.ivh_deliverydate &amp;gt;= '08-01-2025' and ih2.ivh_deliverydate &amp;lt; '08-20-2025' and ih2.ivh_rate &amp;gt; 1.2 and ih2.ivh_rate &amp;lt; 2.5) as BillMTMi&lt;BR /&gt;, (select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= '08-01-2025' and ih3.ivh_deliverydate &amp;lt; '08-20-2025') as FSCRate&lt;BR /&gt;, case when ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) &amp;gt; 100 then 100 else ISNULL((SELECT sum( ISNULL( stp_lgh_mileage, 0 ) ) FROM stops s1 (NOLOCK) WHERE ih.ord_hdrnumber = s1.ord_hdrnumber AND s1.stp_loadstatus in ('MT','BT') ),0) end&lt;BR /&gt;*&lt;BR /&gt;(select avg(id.ivd_rate) from invoicedetail id (nolock) inner join invoiceheader ih3 (nolock) on ih3.ivh_hdrnumber = id.ivh_hdrnumber where id.cht_itemcode like 'FSCMI%' and ih3.ivh_billto = 'STATEM' and ih3.ivh_deliverydate &amp;gt;= '08-01-2025' and ih3.ivh_deliverydate &amp;lt; '08-20-2025') as BillMTFSC&lt;BR /&gt;, ih.ivh_totalcharge as BHGrossAmt&lt;BR /&gt;, (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number) as LastStopID&lt;BR /&gt;, isnull(s.stp_lgh_mileage,25) as LastStopMiles&lt;BR /&gt;, .75 as RPM&lt;BR /&gt;, .75 * s.stp_lgh_mileage as ReduceBHAmount&lt;BR /&gt;, case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge else ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)) end as BHTotal&lt;BR /&gt;, case when orig.cty_name = 'Brownwood' and dest.cty_region2 = 'TX-DAL' then ih.ivh_totalcharge*-1 else ((ih.ivh_totalcharge - (.75 * isnull(s.stp_lgh_mileage,25)))*.8)*-1 end as BHCredit&lt;BR /&gt;, IH.IVH_BILLTO&lt;BR /&gt;FROM invoiceheader ih (NOLOCK)&lt;BR /&gt;INNER JOIN city orig (NOLOCK) ON orig.cty_code = ih.ivh_origincity&lt;BR /&gt;INNER JOIN city dest (NOLOCK) ON dest.cty_code = ih.ivh_destcity&lt;BR /&gt;LEFT JOIN legheader l (NOLOCK) ON l.ord_hdrnumber = ih.ord_hdrnumber&lt;BR /&gt;LEFT JOIN city legdest (NOLOCK) ON legdest.cty_code = l.lgh_endcity&lt;BR /&gt;LEFT JOIN stops s (NOLOCK) ON s.mov_number = ih.mov_number and s.stp_mfh_sequence = (select max(stp_mfh_sequence) from stops s (nolock) where s.mov_number = ih.mov_number)&lt;BR /&gt;WHERE ih.ivh_revtype3 = 'SC-BH'&lt;BR /&gt;AND ih.ivh_billto NOT LIKE 'STATEM%'&lt;BR /&gt;AND ih.ivh_deliverydate&amp;gt;= '08-01-2025' and ih.ivh_deliverydate&amp;lt; '08-20-2025'&lt;BR /&gt;AND (l.lgh_split_flag IN ('N','S') OR l.lgh_split_flag is null)&lt;BR /&gt;AND ih.ivh_invoicenumber = (SELECT max(ih2.ivh_invoicenumber) FROM invoiceheader ih2 (NOLOCK) WHERE ih2.ord_hdrnumber = ih.ord_hdrnumber and right(ivh_invoicenumber,1) &amp;lt;&amp;gt; 'B')&lt;BR /&gt;ORDER BY MTMilesOG desc&lt;/P&gt;</description>
      <pubDate>Thu, 21 Aug 2025 15:23:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4803926#M1429188</guid>
      <dc:creator>cheid_4838</dc:creator>
      <dc:date>2025-08-21T15:23:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4806098#M1429689</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="677608" data-lia-user-login="cheid_4838" class="lia-mention lia-mention-user"&gt;cheid_4838&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried to reproduce your scenarios on my local machine and got an error related to an &lt;CODE data-start="172" data-end="182"&gt;ORDER BY&lt;/CODE&gt; clause. So, I asked some AI tools to rewrite the query suitable for Power BI, and they gave me this query. It looks like they just removed the &lt;CODE data-start="326" data-end="336"&gt;ORDER BY&lt;/CODE&gt; clause, but anyway, I have copied the full code here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SELECT 
    -- Order Information
    ih.ord_hdrnumber AS OrderNum,
    ih.ivh_deliverydate AS Delivery,
    
    -- Origin Information
    orig.cty_name AS Origin,
    orig.cty_state AS OSt,
    orig.cty_region2 AS OReg,
    
    -- Destination Information (with split flag logic)
    ISNULL(
        CASE 
            WHEN l.lgh_split_flag = 'N' 
            THEN dest.cty_name 
            ELSE legdest.cty_name 
        END, 
        'Temple'
    ) AS Dest,
    
    ISNULL(
        CASE 
            WHEN l.lgh_split_flag = 'N' 
            THEN dest.cty_state 
            ELSE legdest.cty_state 
        END, 
        'TX'
    ) AS DSt,
    
    ISNULL(
        CASE 
            WHEN l.lgh_split_flag = 'N' 
            THEN dest.cty_region2 
            ELSE legdest.cty_region2 
        END, 
        'TX-TEM'
    ) AS DReg,
    
    -- Split Flag
    l.lgh_split_flag AS SplitFlag,
    
    -- MT Miles Original (capped at 200)
    CASE 
        WHEN ISNULL(
            (SELECT SUM(ISNULL(stp_lgh_mileage, 0)) 
             FROM stops s1 (NOLOCK) 
             WHERE ih.ord_hdrnumber = s1.ord_hdrnumber 
               AND s1.stp_loadstatus IN ('MT', 'BT')), 0
        ) &amp;gt; 200 
        THEN 200 
        ELSE ISNULL(
            (SELECT SUM(ISNULL(stp_lgh_mileage, 0)) 
             FROM stops s1 (NOLOCK) 
             WHERE ih.ord_hdrnumber = s1.ord_hdrnumber 
               AND s1.stp_loadstatus IN ('MT', 'BT')), 0
        )
    END AS MTMilesOG,
    
    -- MT Miles (capped at 100)
    CASE 
        WHEN ISNULL(
            (SELECT SUM(ISNULL(stp_lgh_mileage, 0)) 
             FROM stops s1 (NOLOCK) 
             WHERE ih.ord_hdrnumber = s1.ord_hdrnumber 
               AND s1.stp_loadstatus IN ('MT', 'BT')), 0
        ) &amp;gt; 100 
        THEN 100 
        ELSE ISNULL(
            (SELECT SUM(ISNULL(stp_lgh_mileage, 0)) 
             FROM stops s1 (NOLOCK) 
             WHERE ih.ord_hdrnumber = s1.ord_hdrnumber 
               AND s1.stp_loadstatus IN ('MT', 'BT')), 0
        )
    END AS MTMiles,
    
    -- Rate from STATEM invoices
    (SELECT TOP 1 ih2.ivh_rate 
     FROM invoiceheader ih2 (NOLOCK) 
     WHERE ih2.ivh_billto = 'STATEM' 
       AND ih2.ivh_deliverydate &amp;gt;= '08-01-2025' 
       AND ih2.ivh_deliverydate &amp;lt; '08-20-2025' 
       AND ih2.ivh_rate &amp;gt; 1.2 
       AND ih2.ivh_rate &amp;lt; 2.5 
     ORDER BY CAST(ih2.ivh_deliverydate AS DATE) DESC
    ) AS Rate,
    
    -- Billing MT Miles calculation
    CASE 
        WHEN ISNULL(
            (SELECT SUM(ISNULL(stp_lgh_mileage, 0)) 
             FROM stops s1 (NOLOCK) 
             WHERE ih.ord_hdrnumber = s1.ord_hdrnumber 
               AND s1.stp_loadstatus IN ('MT', 'BT')), 0
        ) &amp;gt; 100 
        THEN 100 
        ELSE ISNULL(
            (SELECT SUM(ISNULL(stp_lgh_mileage, 0)) 
             FROM stops s1 (NOLOCK) 
             WHERE ih.ord_hdrnumber = s1.ord_hdrnumber 
               AND s1.stp_loadstatus IN ('MT', 'BT')), 0
        )
    END * 
    (SELECT TOP 1 ih2.ivh_rate 
     FROM invoiceheader ih2 (NOLOCK) 
     WHERE ih2.ivh_billto = 'STATEM' 
       AND ih2.ivh_deliverydate &amp;gt;= '08-01-2025' 
       AND ih2.ivh_deliverydate &amp;lt; '08-20-2025' 
       AND ih2.ivh_rate &amp;gt; 1.2 
       AND ih2.ivh_rate &amp;lt; 2.5
    ) AS BillMTMi,
    
    -- FSC Rate
    (SELECT AVG(id.ivd_rate) 
     FROM invoicedetail id (NOLOCK) 
     INNER JOIN invoiceheader ih3 (NOLOCK) ON ih3.ivh_hdrnumber = id.ivh_hdrnumber 
     WHERE id.cht_itemcode LIKE 'FSCMI%' 
       AND ih3.ivh_billto = 'STATEM' 
       AND ih3.ivh_deliverydate &amp;gt;= '08-01-2025' 
       AND ih3.ivh_deliverydate &amp;lt; '08-20-2025'
    ) AS FSCRate,
    
    -- Billing MT FSC calculation
    CASE 
        WHEN ISNULL(
            (SELECT SUM(ISNULL(stp_lgh_mileage, 0)) 
             FROM stops s1 (NOLOCK) 
             WHERE ih.ord_hdrnumber = s1.ord_hdrnumber 
               AND s1.stp_loadstatus IN ('MT', 'BT')), 0
        ) &amp;gt; 100 
        THEN 100 
        ELSE ISNULL(
            (SELECT SUM(ISNULL(stp_lgh_mileage, 0)) 
             FROM stops s1 (NOLOCK) 
             WHERE ih.ord_hdrnumber = s1.ord_hdrnumber 
               AND s1.stp_loadstatus IN ('MT', 'BT')), 0
        )
    END * 
    (SELECT AVG(id.ivd_rate) 
     FROM invoicedetail id (NOLOCK) 
     INNER JOIN invoiceheader ih3 (NOLOCK) ON ih3.ivh_hdrnumber = id.ivh_hdrnumber 
     WHERE id.cht_itemcode LIKE 'FSCMI%' 
       AND ih3.ivh_billto = 'STATEM' 
       AND ih3.ivh_deliverydate &amp;gt;= '08-01-2025' 
       AND ih3.ivh_deliverydate &amp;lt; '08-20-2025'
    ) AS BillMTFSC,
    
    -- Billing Information
    ih.ivh_totalcharge AS BHGrossAmt,
    
    -- Stop Information
    (SELECT MAX(stp_mfh_sequence) 
     FROM stops s (NOLOCK) 
     WHERE s.mov_number = ih.mov_number
    ) AS LastStopID,
    
    ISNULL(s.stp_lgh_mileage, 25) AS LastStopMiles,
    
    -- Revenue Calculations
    0.75 AS RPM,
    0.75 * s.stp_lgh_mileage AS ReduceBHAmount,
    
    CASE 
        WHEN orig.cty_name = 'Brownwood' 
         AND dest.cty_region2 = 'TX-DAL' 
        THEN ih.ivh_totalcharge 
        ELSE ih.ivh_totalcharge - (0.75 * ISNULL(s.stp_lgh_mileage, 25)) 
    END AS BHTotal,
    
    CASE 
        WHEN orig.cty_name = 'Brownwood' 
         AND dest.cty_region2 = 'TX-DAL' 
        THEN ih.ivh_totalcharge * -1 
        ELSE ((ih.ivh_totalcharge - (0.75 * ISNULL(s.stp_lgh_mileage, 25))) * 0.8) * -1 
    END AS BHCredit,
    
    ih.ivh_billto

FROM invoiceheader ih (NOLOCK)
    
    -- Join with origin city
    INNER JOIN city orig (NOLOCK) 
        ON orig.cty_code = ih.ivh_origincity
    
    -- Join with destination city
    INNER JOIN city dest (NOLOCK) 
        ON dest.cty_code = ih.ivh_destcity
    
    -- Optional join with leg header
    LEFT JOIN legheader l (NOLOCK) 
        ON l.ord_hdrnumber = ih.ord_hdrnumber
    
    -- Optional join with leg destination
    LEFT JOIN city legdest (NOLOCK) 
        ON legdest.cty_code = l.lgh_endcity
    
    -- Optional join with last stop
    LEFT JOIN stops s (NOLOCK) 
        ON s.mov_number = ih.mov_number 
        AND s.stp_mfh_sequence = (
            SELECT MAX(stp_mfh_sequence) 
            FROM stops s (NOLOCK) 
            WHERE s.mov_number = ih.mov_number
        )

WHERE 
    -- Filter conditions
    ih.ivh_revtype3 = 'SC-BH'
    AND ih.ivh_billto NOT LIKE 'STATEM%'
    AND ih.ivh_deliverydate &amp;gt;= '08-01-2025' 
    AND ih.ivh_deliverydate &amp;lt; '08-20-2025'
    AND (l.lgh_split_flag IN ('N', 'S') OR l.lgh_split_flag IS NULL)
    AND ih.ivh_invoicenumber = (
        SELECT MAX(ih2.ivh_invoicenumber) 
        FROM invoiceheader ih2 (NOLOCK) 
        WHERE ih2.ord_hdrnumber = ih.ord_hdrnumber 
          AND RIGHT(ivh_invoicenumber, 1) &amp;lt;&amp;gt; 'B'
    )

-- ORDER BY MTMilesOG DESC -- Removed for Power BI compatibility&lt;/LI-CODE&gt;
&lt;P&gt;It looks like just removing the &lt;CODE data-start="85" data-end="95"&gt;ORDER BY&lt;/CODE&gt; clause makes DirectQuery mode work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Aug 2025 03:29:05 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4806098#M1429689</guid>
      <dc:creator>kushanNa</dc:creator>
      <dc:date>2025-08-25T03:29:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using SQL declared variables into Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4806150#M1429706</link>
      <description>&lt;P&gt;Thankyou,&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="695049" data-lia-user-login="MohamedFowzan1" class="lia-mention lia-mention-user"&gt;MohamedFowzan1&lt;/a&gt;,&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="603758" data-lia-user-login="CPCARDOSO" class="lia-mention lia-mention-user"&gt;CPCARDOSO&lt;/a&gt;,&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="792702" data-lia-user-login="kushanNa" class="lia-mention lia-mention-user"&gt;kushanNa&lt;/a&gt;&amp;nbsp;for your responses.&lt;BR /&gt;&lt;BR /&gt;Hi cheid_4838,&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Based on my understanding, in Import mode parameters can only be applied during refresh and cannot be bound to slicers. Consequently, the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Bind to Parameter&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;option does not appear. In DirectQuery mode, dynamic M parameters are supported; however, the current SQL query contains unsupported constructs such as correlated subqueries and ORDER BY in subqueries. These constructs break query folding, preventing DirectQuery from executing the query in its present form.&lt;/P&gt;
&lt;P&gt;If you remain in Import mode, parameters will work only at refresh time and will not be dynamic. If you require slicer driven parameters, switch to DirectQuery and simplify the SQL by using a database view or stored procedure that accepts parameters. This will enable query folding and allow binding slicers to parameters.&lt;/P&gt;
&lt;P&gt;Additionally, please refer to the following links:&lt;BR /&gt;&lt;A href="https://learn.microsoft.com/en-us/powerquery-m/value-nativequery" target="_blank"&gt;Value.NativeQuery - PowerQuery M | Microsoft Learn&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-dynamic-m-query-parameters" target="_blank"&gt;Dynamic M query parameters in Power BI Desktop - Power BI | Microsoft Learn&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;We hope this information helps to resolve the issue. Should you have any further queries, please feel free to contact the Microsoft Fabric community.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Aug 2025 04:26:33 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Desktop/Using-SQL-declared-variables-into-Power-BI/m-p/4806150#M1429706</guid>
      <dc:creator>v-pnaroju-msft</dc:creator>
      <dc:date>2025-08-25T04:26:33Z</dc:date>
    </item>
  </channel>
</rss>

