<?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 Re: DAX - Understanding Var in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4417263#M175406</link>
    <description>&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Thanks for the reply from freginier&amp;nbsp; and Jihwan_Kim&amp;nbsp;, please allow me to provide another insight:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;&lt;SPAN&gt;Hi,&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="933442" data-lia-user-login="Stivik1981" class="lia-mention lia-mention-user"&gt;Stivik1981&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;Thanks for reaching out to the Microsoft fabric community forum.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;&lt;SPAN&gt;Yes, as freginier&amp;nbsp; and Jihwan_Kim mentioned, when you use var x = trxs[ImportDayNum], you create a variable x that holds the ImportDayNum value of the current row. Then, you use this variable in the FILTER&lt;/SPAN&gt;&lt;SPAN&gt;()&lt;/SPAN&gt;&lt;SPAN&gt; function to compare all rows' ImportDayNum values to x. This means you are comparing all rows' ImportDayNum values to the ImportDayNum value of the current row.&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;var x = trxs[ImportDayNum]
return COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] = x))&lt;/LI-CODE&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;This formula calculates the number of rows that have the same ImportDayNum value as the current row.&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;However, when you remove var and directly use trxs[ImportDayNum], the formula becomes:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] = trxs[ImportDayNum]))&lt;/LI-CODE&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;&lt;SPAN&gt;In this case, trxs[ImportDayNum] = trxs[ImportDayNum] is TRUE for every row because each row's ImportDayNum value is always equal to itself. This means the FILTER&lt;/SPAN&gt;&lt;SPAN&gt;()&lt;/SPAN&gt;&lt;SPAN&gt; function does not filter out any rows, resulting in the count of all rows in the table.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;Therefore, using var to create the variable x is crucial as it ensures that you are comparing the ImportDayNum value of the current row in the FILTER() function, rather than the ImportDayNum value of each row. Besides its use in the FILTER function, using var also improves code readability, aesthetics, and facilitates reuse.&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;Here are the relevant document screenshots:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;For more details, please refer to:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&lt;A href="https://learn.microsoft.com/en-us/dax/best-practices/dax-variables" target="_blank"&gt;&lt;SPAN&gt;Use variables to improve your DAX formulas - DAX | Microsoft Learn&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&lt;A href="https://learn.microsoft.com/en-us/dax/var-dax" target="_blank"&gt;&lt;SPAN&gt;VAR keyword (DAX) - DAX | Microsoft Learn&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;Of course, if you have any new discoveries or questions, please feel free to get in touch with us.&lt;BR /&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Best Regards,&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Leroy Lu&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;If this post &lt;EM&gt;&lt;STRONG&gt;helps&lt;/STRONG&gt;&lt;/EM&gt;, then please consider Accept it &lt;EM&gt;&lt;STRONG&gt;as the solution &lt;/STRONG&gt;&lt;/EM&gt;to help the other members find it more quickly.&lt;/P&gt;</description>
    <pubDate>Wed, 19 Feb 2025 08:07:17 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2025-02-19T08:07:17Z</dc:date>
    <item>
      <title>DAX - Understanding Var</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4414338#M175304</link>
      <description>&lt;P&gt;Hi Friends,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Newbie to DAX here and I'm trying to get my head around something:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a calculated column that I was experimenting with.&amp;nbsp; The formula is as follows:&lt;/P&gt;&lt;P&gt;&lt;!--  StartFragment   --&gt;&lt;SPAN class=""&gt;var&lt;/SPAN&gt; &lt;EM&gt;&lt;STRONG&gt;&lt;SPAN class=""&gt;x&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;SPAN class=""&gt; = trxs[ImportDayNum] &lt;/SPAN&gt;&lt;SPAN class=""&gt;return&lt;/SPAN&gt; &lt;SPAN class=""&gt;COUNTROWS&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;FILTER&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;ALL&lt;/SPAN&gt;&lt;SPAN class=""&gt;(trxs), trxs[ImportDayNum] =&lt;/SPAN&gt;&lt;STRONG&gt;&lt;EM&gt;&lt;SPAN class=""&gt;x&lt;/SPAN&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;SPAN class=""&gt;))&lt;/SPAN&gt;&lt;!--  EndFragment   --&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;Without getting into the weeds of it this formula actually works as intended.&amp;nbsp; My question is this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;If I &lt;EM&gt;remove&lt;/EM&gt; the Var, and simply code it this way:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;COUNTROWS&lt;/SPAN&gt;(&lt;SPAN class=""&gt;FILTER&lt;/SPAN&gt;(&lt;SPAN class=""&gt;ALL&lt;/SPAN&gt;(trxs), trxs[ImportDayNum] =&lt;STRONG&gt;&lt;EM&gt;trxs[ImportDayNum]&lt;/EM&gt;&lt;/STRONG&gt;)) --&amp;gt;&amp;nbsp; It sends back a whacky result!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;I'm totally confused!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;If the var IS the same thing as the long-coded form, why would I get two results?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;Please help!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 17:31:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4414338#M175304</guid>
      <dc:creator>Stivik1981</dc:creator>
      <dc:date>2025-02-17T17:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: DAX - Understanding Var</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4414964#M175321</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I think, if you want to write without VAR, please try using EARLIER DAX function.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://learn.microsoft.com/en-us/dax/earlier-function-dax?wt.mc_id=DP-MVP-5004989" target="_blank" rel="noopener"&gt;EARLIER function (DAX) - DAX | Microsoft Learn&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"Returns the current value of the specified column in an outer evaluation pass of the mentioned column.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;EARLIER is useful for nested calculations where you want to use a certain value as an input and produce calculations based on that input. In Microsoft Excel, you can do such calculations only within the context of the current row; however, in DAX you can store the value of the input and then make calculation using data from the entire table.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;EARLIER is mostly used in the context of calculated columns."&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;calculated column CC =
COUNTROWS (
    FILTER ( trxs, trxs[ImportDayNum] = EARLIER ( trxs[ImportDayNum] ) )
)
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2025 05:33:45 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4414964#M175321</guid>
      <dc:creator>Jihwan_Kim</dc:creator>
      <dc:date>2025-02-18T05:33:45Z</dc:date>
    </item>
    <item>
      <title>Re: DAX - Understanding Var</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4415634#M175340</link>
      <description>&lt;P&gt;Hi there!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Interesting question.. I believe this is the answer:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Without VAR: DAX compares each row’s ImportDayNum to itself, potentially leading to unintended evaluations or context mismatches.&lt;/LI&gt;&lt;LI&gt;With VAR: The stored value ensures consistency in the filter operation.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Hope this makes sense and that you understand a bit better.&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;&lt;P&gt;Zoe&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2025 10:17:49 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4415634#M175340</guid>
      <dc:creator>freginier</dc:creator>
      <dc:date>2025-02-18T10:17:49Z</dc:date>
    </item>
    <item>
      <title>Re: DAX - Understanding Var</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4417263#M175406</link>
      <description>&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Thanks for the reply from freginier&amp;nbsp; and Jihwan_Kim&amp;nbsp;, please allow me to provide another insight:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;&lt;SPAN&gt;Hi,&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="933442" data-lia-user-login="Stivik1981" class="lia-mention lia-mention-user"&gt;Stivik1981&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;Thanks for reaching out to the Microsoft fabric community forum.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;&lt;SPAN&gt;Yes, as freginier&amp;nbsp; and Jihwan_Kim mentioned, when you use var x = trxs[ImportDayNum], you create a variable x that holds the ImportDayNum value of the current row. Then, you use this variable in the FILTER&lt;/SPAN&gt;&lt;SPAN&gt;()&lt;/SPAN&gt;&lt;SPAN&gt; function to compare all rows' ImportDayNum values to x. This means you are comparing all rows' ImportDayNum values to the ImportDayNum value of the current row.&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;var x = trxs[ImportDayNum]
return COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] = x))&lt;/LI-CODE&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;This formula calculates the number of rows that have the same ImportDayNum value as the current row.&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;However, when you remove var and directly use trxs[ImportDayNum], the formula becomes:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] = trxs[ImportDayNum]))&lt;/LI-CODE&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;&lt;SPAN&gt;In this case, trxs[ImportDayNum] = trxs[ImportDayNum] is TRUE for every row because each row's ImportDayNum value is always equal to itself. This means the FILTER&lt;/SPAN&gt;&lt;SPAN&gt;()&lt;/SPAN&gt;&lt;SPAN&gt; function does not filter out any rows, resulting in the count of all rows in the table.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;Therefore, using var to create the variable x is crucial as it ensures that you are comparing the ImportDayNum value of the current row in the FILTER() function, rather than the ImportDayNum value of each row. Besides its use in the FILTER function, using var also improves code readability, aesthetics, and facilitates reuse.&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;Here are the relevant document screenshots:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;For more details, please refer to:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&lt;A href="https://learn.microsoft.com/en-us/dax/best-practices/dax-variables" target="_blank"&gt;&lt;SPAN&gt;Use variables to improve your DAX formulas - DAX | Microsoft Learn&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&lt;A href="https://learn.microsoft.com/en-us/dax/var-dax" target="_blank"&gt;&lt;SPAN&gt;VAR keyword (DAX) - DAX | Microsoft Learn&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;Of course, if you have any new discoveries or questions, please feel free to get in touch with us.&lt;BR /&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Best Regards,&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Leroy Lu&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;If this post &lt;EM&gt;&lt;STRONG&gt;helps&lt;/STRONG&gt;&lt;/EM&gt;, then please consider Accept it &lt;EM&gt;&lt;STRONG&gt;as the solution &lt;/STRONG&gt;&lt;/EM&gt;to help the other members find it more quickly.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 08:07:17 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Understanding-Var/m-p/4417263#M175406</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-02-19T08:07:17Z</dc:date>
    </item>
  </channel>
</rss>

