<?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 How can DAX compare a column to itself in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910148#M41292</link>
    <description>&lt;P&gt;I came across this formula:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Previous Month Sales =&lt;BR /&gt;&amp;nbsp; &amp;nbsp; CALCULATE(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [Sum of Sales],&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FILTER(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ALL('Date'[Calendar Month]),&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;MAX('Date'[Calendar Month]) = EDATE('Date'[Calendar Month], 1)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; )&lt;BR /&gt;&amp;nbsp; &amp;nbsp; )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My understanding is that the ALL function removes all the implicit filters on the column&amp;nbsp;'Date'[Calendar Month]. DAX then iterates over that column to see whether each row satisfies the condition&amp;nbsp;MAX('Date'[Calendar Month]) = EDATE('Date'[Calendar Month], 1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is: how does the boolean expression in the FILTER function work? In particular, what are the values returned by&amp;nbsp;MAX('Date'[Calendar Month]) and EDATE('Date'[Calendar Month], 1)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Say the first row of&amp;nbsp;'Date'[Calendar Month] is January 2013, then&amp;nbsp;MAX('Date'[Calendar Month]) would return January 2013 whilst&amp;nbsp;EDATE('Date'[Calendar Month], 1) would return February 2013. Is that right? But if so, no row in the&amp;nbsp;'Date'[Calendar Month] satisfies the equality comparison, and the Previous Month Sales would be zero?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
    <pubDate>Sun, 20 Jun 2021 01:01:51 GMT</pubDate>
    <dc:creator>nemo</dc:creator>
    <dc:date>2021-06-20T01:01:51Z</dc:date>
    <item>
      <title>How can DAX compare a column to itself</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910148#M41292</link>
      <description>&lt;P&gt;I came across this formula:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Previous Month Sales =&lt;BR /&gt;&amp;nbsp; &amp;nbsp; CALCULATE(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [Sum of Sales],&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FILTER(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ALL('Date'[Calendar Month]),&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;MAX('Date'[Calendar Month]) = EDATE('Date'[Calendar Month], 1)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; )&lt;BR /&gt;&amp;nbsp; &amp;nbsp; )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My understanding is that the ALL function removes all the implicit filters on the column&amp;nbsp;'Date'[Calendar Month]. DAX then iterates over that column to see whether each row satisfies the condition&amp;nbsp;MAX('Date'[Calendar Month]) = EDATE('Date'[Calendar Month], 1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is: how does the boolean expression in the FILTER function work? In particular, what are the values returned by&amp;nbsp;MAX('Date'[Calendar Month]) and EDATE('Date'[Calendar Month], 1)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Say the first row of&amp;nbsp;'Date'[Calendar Month] is January 2013, then&amp;nbsp;MAX('Date'[Calendar Month]) would return January 2013 whilst&amp;nbsp;EDATE('Date'[Calendar Month], 1) would return February 2013. Is that right? But if so, no row in the&amp;nbsp;'Date'[Calendar Month] satisfies the equality comparison, and the Previous Month Sales would be zero?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jun 2021 01:01:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910148#M41292</guid>
      <dc:creator>nemo</dc:creator>
      <dc:date>2021-06-20T01:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: How can DAX compare a column to itself</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910276#M41302</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="308234" data-lia-user-login="nemo" class="lia-mention lia-mention-user"&gt;nemo&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Let's say you are executing this measure against Month-Year in a Table visual. &lt;SPAN&gt;MAX('Date'[Calendar Month]) refers to the current month in each row but you have used EDATE which expects a date to shift my given number of months and supplied a Month-year.&amp;nbsp;&lt;BR /&gt;You can modify your measure as :&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Previous Month Sales =
    CALCULATE(
        [Sum of Sales],
        FILTER(
            ALL('Date'),
            MAX('Date'[Date]) = EDATE('Date'[Date], 1)
        )
    )&lt;/LI-CODE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;I suggest you utilized other standard methods with :&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://docs.microsoft.com/en-us/dax/dateadd-function-dax" target="_blank"&gt;https://docs.microsoft.com/en-us/dax/dateadd-function-dax&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://docs.microsoft.com/en-us/dax/previousmonth-function-dax" target="_blank"&gt;https://docs.microsoft.com/en-us/dax/previousmonth-function-dax&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jun 2021 10:54:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910276#M41302</guid>
      <dc:creator>Fowmy</dc:creator>
      <dc:date>2021-06-20T10:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can DAX compare a column to itself</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910449#M41306</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[Previous Month Sales] =
CALCULATE(
    [Sum of Sales],
    FILTER(
        ALL( 'Date'[Calendar Month] ),
        MAX( 'Date'[Calendar Month] )
            = EDATE(
                'Date'[Calendar Month],
                1
            )
    )
)

// is equivalent to

[Previous Month Sales] =
var MaxMonthVisibleInCurrentContext = MAX( 'Date'[Calendar Month] )
return
CALCULATE(
    [Sum of Sales],
    FILTER(
        ALL( 'Date'[Calendar Month] ),
        var CurrentlyIteratedMonth = 'Date'[Calendar Month]
        return
        // EDATE works only on real dates, so
        // I assume that 'Date'[Calendar Month] is
        // for instance the first day of the month
        // for each month.
        EDATE( CurrentlyIteratedMonth, 1 )
            = MaxMonthVisibleInCurrentContext
    )
)

// ALL does not remove any filters on any column,
// even more so any implicit filters, but IGNORES
// any EXPLICIT filters on the column and returns
// all the values as they would be seen 
// if there were no filtering on the table whatsoever,
// be it implicit or explicit. This is very different
// from removing filters.

// What the measure tries to achieve is dependent on
// the model and especially on the structure of the
// Date table. If there's a Date column in the table
// then it should be used under EDATE, not Calendar Month
// which should be an integer or a name of the month.
// It could, of course, also be the first day of the month
// and it would also work but I doubt this is the setup here.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jun 2021 18:28:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910449#M41306</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-06-20T18:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: How can DAX compare a column to itself</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910805#M41309</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/a&gt;,&amp;nbsp;thank you for the unexpected answer. Without your detailed explanation, I don't think I could understand what's going on behind the formula of the calculated measure in question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also thank you for pointing out my technically incorrect phrase "removes the all the implicit filters". Next time I'll remember to use the term "ignore" instead of "remove"! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To ensure I've understood you correctly, this is my interpreation on how the calcuation takes place.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Assume there is a visual table with a row header 1-Feb-2018. Then&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var MaxMonthVisibleInCurrentContext = MAX( 'Date'[Calendar Month] ) = "1-Feb-2013"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That value of 1-Feb-2013 is then used to figure out what is the value for CurrentlyIteratedMonth. That is,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;EDATE( CurrentlyIteratedMonth, 1 )
            = MaxMonthVisibleInCurrentContext
            = "1-Feb-2013"
// Therefore:
CurrentlyIteratedMonth = "1-Jan-2013"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The derived value 1-Jan-2013 is then used for the measure [Sum of Sales], which achieves the desired outome (returns the previous month sales for the current context).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If my interpretation above is correct, then I think the syntax of the formula is counterintuitive. Because&amp;nbsp;&lt;SPAN&gt;MAX('Date'[Calendar Month]) is a known value (implied by the current context) whilst the&amp;nbsp;EDATE('Date'[Calendar Month], 1) is a variable dependent on&amp;nbsp;MAX('Date'[Calendar Month]), then MAX('Date'[Calendar Month]) should have been in the right-hand side of the boolean filter expression, that is, it should have been EDATE('Date'[Calendar Month], 1) = MAX('Date'[Calendar Month]) instead of&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;EDATE('Date'[Calendar Month], 1)&amp;nbsp; =&amp;nbsp;MAX('Date'[Calendar Month]).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll wait for your confirmation of my clarification before accepting your (great) answer as the solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Warmest regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS: I can see that you're very smart to make the right assumption that&amp;nbsp;&lt;SPAN&gt;'Date'[Calendar Month] is the first date of the month&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;. Just a quick add-on question: could you please recommend some books that I could read to achieve a deep understanding of DAX like yours? Thanks&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 02:39:23 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910805#M41309</guid>
      <dc:creator>nemo</dc:creator>
      <dc:date>2021-06-21T02:39:23Z</dc:date>
    </item>
    <item>
      <title>Re: How can DAX compare a column to itself</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910835#M41311</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="52518" data-lia-user-login="Fowmy" class="lia-mention lia-mention-user"&gt;Fowmy&lt;/a&gt;&amp;nbsp;thanks for the suggestions. Much appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 02:41:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1910835#M41311</guid>
      <dc:creator>nemo</dc:creator>
      <dc:date>2021-06-21T02:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: How can DAX compare a column to itself</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1911782#M41320</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="308234" data-lia-user-login="nemo" class="lia-mention lia-mention-user"&gt;nemo&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, your understanding is correct. And yes, DAX formulas are counterintuitive to those who start their journey with this language. Because this language is different from anything one has seen before; it's not object-oriented and it's functional. To really understand it one has to be intimately familar with the notion of CONTEXTS and their interaction with other features of the language.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;// A much better formulation of your measure
// is this one:

[Previous Month Sales] =
var MaxDateVisibleInCurrentContext =
    MAX( 'Date'[Calendar Month] )
var DateOneMonthAfter =
    EDATE( MaxDateVisibleInCurrentContext, 1)
var Result =
    CALCULATE(
        [Sum of Sales],
        'Date'[Calendar Month] = DateOneMonthAfter
    )
return
    Rasult
    
// This, in fact, is the standard way
// that it should be written. Don't be fooled
// by the boolean condition in the second
// argument of CALCULATE. All filters in CALCULATE
// are ALWAYS tables. What you see above is just
// syntactic sugar. The code above has at least
// 2 advantages over the other ones:
// 1) It's more readable and understandable at
//    a glance and
// 2) it has the potential to be faster since
//    such conditions like "T[Col] = Value" under
//    CALCULATE have been optimized internally.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As for the book... There's only one book to rule them all: "The Definitive Guide to DAX" by Alberto Ferrari and Marco Russo. Buy it, read it at least 3-4 times (you'll have to do it anyway, even without my nudging) and practise a lot. I use this site/these forums to actually test my knowledge and I tend to pick up the problems that others leave without answers. This is how I've hardened my DAX (and in fact Power BI) skills. Also, I always keep up to date with articles written and recorded by The Italians (Alberto and Marco). I frequently visit &lt;A href="http://www.sqlbi.com" target="_blank" rel="noopener"&gt;www.sqlbi.com&amp;nbsp;&lt;/A&gt;since it's the corner of the Internet where all the knowledge of DAX ultimately resides.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By the way, it doesn't matter how you write boolean conditions that contain "=". "a = b" is totally equivalent to "b = a". As far as I remember, this is an axiom of logic.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 09:29:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1911782#M41320</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-06-21T09:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: How can DAX compare a column to itself</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1912166#M41338</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/a&gt;,&amp;nbsp;another valuable advice. Thanks so much for sharing your knowledge.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, totally agreed that the new version of [Previous Month Sales] is much better to read and understand.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure syntactic sugar is a good idea because it would fool people (yes, I mistakenly thought the second argument of CALCULATE was a boolean condition, thanks for pointing that out) and obscure the logic (thus making DAX formulas counterintuitive).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, you're right: if A = B then B = A. But when I said it should have been A = B instead of B = A, I was thinking of &lt;STRONG&gt;assigning&lt;/STRONG&gt; a known value B to a variable A rather than &lt;STRONG&gt;comparing&lt;/STRONG&gt; between two values A and B.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 11:55:56 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-can-DAX-compare-a-column-to-itself/m-p/1912166#M41338</guid>
      <dc:creator>nemo</dc:creator>
      <dc:date>2021-06-21T11:55:56Z</dc:date>
    </item>
  </channel>
</rss>

