<?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: Compare Previous Two Rows with a Current Row using DAX in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988498#M100433</link>
    <description>&lt;P&gt;Hi &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="489634" data-lia-user-login="ahsan005" class="lia-mention lia-mention-user"&gt;ahsan005&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are going by firstoccurence to establish an order and look for the previous rows, what happens when the datetime in firstoccurence is exactly the same? This happens several times in your sample data. One option would be to sort as required in PQ and add an index column, then base it all on that index instead of on&amp;nbsp;firstoccurence Pending that, try this for your column.&amp;nbsp;See it all at work in the attached file.&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;NewColumn =
VAR previous_ =
    CALCULATE (
        MAX ( Table1[firstoccurrence] ),
        Table1[firstoccurrence] &amp;lt; EARLIER ( Table1[firstoccurrence] ),
        ALLEXCEPT ( Table1, Table1[sitename] )
    )
VAR previous2Prev_ =
    CALCULATE (
        MAX ( Table1[firstoccurrence] ),
        Table1[firstoccurrence] &amp;lt; previous_,
        ALLEXCEPT ( Table1, Table1[sitename] )
    )
VAR catPrevious_ =
    CALCULATE (
        MAX ( Table1[category] ),
        Table1[firstoccurrence] = previous_,
        ALLEXCEPT ( Table1, Table1[sitename] )
    )
VAR catPrevious2Prev_ =
    CALCULATE (
        MAX ( Table1[category] ),
        Table1[firstoccurrence] = previous2Prev_,
        ALLEXCEPT ( Table1, Table1[sitename] )
    )
RETURN
    IF (
        Table1[category] = "Site Down",
        IF (
            catPrevious_ = "Low Voltage"
                &amp;amp;&amp;amp; catPrevious2Prev_ = "AC Mains Failure",
            "Yes",
            "No"
        )
    )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="height: 100px; width: 100%; border-collapse: collapse; border-style: groove; border-color: #0c1c49; background-color: #00675f;" border="0" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15px;"&gt;
&lt;TD width="15.961305925030231%" style="width: 92px; height: 15px;"&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84.03869407496977%" style="width: 485px; height: 15px; text-align: left;"&gt;&lt;FONT color="#FFFFFF"&gt;&lt;SPAN&gt;&lt;STRONG&gt;Please accept the solution when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT color="#FFFFFF"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt; &lt;/FONT&gt;&lt;BR /&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="4.5" color="#FF99CC"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Dec 2022 08:29:00 GMT</pubDate>
    <dc:creator>AlB</dc:creator>
    <dc:date>2022-12-26T08:29:00Z</dc:date>
    <item>
      <title>Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988412#M100429</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have to create a new column and am trying to compare rows in a single column when a particular value exists in any of the rows. For e.g.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If Table[category] = "Site Down", then check for Table[category row - 1] = "low voltage" &amp;amp;&amp;amp; Table[category row - 2] = "AC Mains Failure", "YES" otherwise "NO"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am unable to access the previous rows in DAX. I've tried using the EARLIER function but am unable to get the exact thing. This is easy to achieve in SQL using LAG() or NEIGHBOR() function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample data link is shared below. Please note that this data is first sorted using 'sitename; and then by 'first occurence' column since it is always expected that low voltage &amp;amp; ac mains failure will come before site down respectively.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A title="Sample Data" href="https://docs.google.com/spreadsheets/d/1nFIkjy_RBFW5VFGLbZND9AOBRYhLJfjP/edit?usp=share_link&amp;amp;ouid=103268240336716024452&amp;amp;rtpof=true&amp;amp;sd=true" target="_blank" rel="noopener"&gt;https://docs.google.com/spreadsheets/d/1nFIkjy_RBFW5VFGLbZND9AOBRYhLJfjP/edit?usp=share_link&amp;amp;ouid=103268240336716024452&amp;amp;rtpof=true&amp;amp;sd=true&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 26 Dec 2022 07:26:46 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988412#M100429</guid>
      <dc:creator>ahsan005</dc:creator>
      <dc:date>2022-12-26T07:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988498#M100433</link>
      <description>&lt;P&gt;Hi &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="489634" data-lia-user-login="ahsan005" class="lia-mention lia-mention-user"&gt;ahsan005&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are going by firstoccurence to establish an order and look for the previous rows, what happens when the datetime in firstoccurence is exactly the same? This happens several times in your sample data. One option would be to sort as required in PQ and add an index column, then base it all on that index instead of on&amp;nbsp;firstoccurence Pending that, try this for your column.&amp;nbsp;See it all at work in the attached file.&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;NewColumn =
VAR previous_ =
    CALCULATE (
        MAX ( Table1[firstoccurrence] ),
        Table1[firstoccurrence] &amp;lt; EARLIER ( Table1[firstoccurrence] ),
        ALLEXCEPT ( Table1, Table1[sitename] )
    )
VAR previous2Prev_ =
    CALCULATE (
        MAX ( Table1[firstoccurrence] ),
        Table1[firstoccurrence] &amp;lt; previous_,
        ALLEXCEPT ( Table1, Table1[sitename] )
    )
VAR catPrevious_ =
    CALCULATE (
        MAX ( Table1[category] ),
        Table1[firstoccurrence] = previous_,
        ALLEXCEPT ( Table1, Table1[sitename] )
    )
VAR catPrevious2Prev_ =
    CALCULATE (
        MAX ( Table1[category] ),
        Table1[firstoccurrence] = previous2Prev_,
        ALLEXCEPT ( Table1, Table1[sitename] )
    )
RETURN
    IF (
        Table1[category] = "Site Down",
        IF (
            catPrevious_ = "Low Voltage"
                &amp;amp;&amp;amp; catPrevious2Prev_ = "AC Mains Failure",
            "Yes",
            "No"
        )
    )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="height: 100px; width: 100%; border-collapse: collapse; border-style: groove; border-color: #0c1c49; background-color: #00675f;" border="0" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15px;"&gt;
&lt;TD width="15.961305925030231%" style="width: 92px; height: 15px;"&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84.03869407496977%" style="width: 485px; height: 15px; text-align: left;"&gt;&lt;FONT color="#FFFFFF"&gt;&lt;SPAN&gt;&lt;STRONG&gt;Please accept the solution when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT color="#FFFFFF"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt; &lt;/FONT&gt;&lt;BR /&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="4.5" color="#FF99CC"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Dec 2022 08:29:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988498#M100433</guid>
      <dc:creator>AlB</dc:creator>
      <dc:date>2022-12-26T08:29:00Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988499#M100434</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="489634" data-lia-user-login="ahsan005" class="lia-mention lia-mention-user"&gt;ahsan005&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;you would need to&amp;nbsp;&lt;BR /&gt;1) add an index column in Power Query&lt;/P&gt;&lt;P&gt;&lt;A href="https://learn.microsoft.com/en-us/power-query/add-index-column" target="_blank" rel="noopener"&gt;https://learn.microsoft.com/en-us/power-query/add-index-column&lt;/A&gt;&lt;/P&gt;&lt;P&gt;2) add a new column with DAX like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Tag = 
VAR _site = [sitename] 
VAR _index = [index]
VAR _table =
FILTER(data, [sitename]= _site)
VAR _lastcategory = 
MINX(
    FILTER( _table, [Index] =_index -1
    ),
    [category]
)
VAR _llastcategory =
MINX(
    FILTER( _table, [Index] =_index -2
    ),
    [category]
)
RETURN
IF(
    [category]="Site Down"
        &amp;amp;&amp;amp;_lastcategory="Low Voltage"
        &amp;amp;&amp;amp;_llastcategory="AC Mains Failure",
    "Yes", "No"
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i tried and it worked like this:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Dec 2022 08:24:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988499#M100434</guid>
      <dc:creator>FreemanZ</dc:creator>
      <dc:date>2022-12-26T08:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988512#M100436</link>
      <description>&lt;P&gt;Two additional comments:&lt;/P&gt;&lt;P&gt;1) Power BI is efficient handling columns but not that good handling rows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) EARLIER is to get the row context in the earlier set of iteration, not the context of the earlier row or the earlier iteration scanning in the current set of iteration.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Dec 2022 08:20:45 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2988512#M100436</guid>
      <dc:creator>FreemanZ</dc:creator>
      <dc:date>2022-12-26T08:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2989125#M100502</link>
      <description>&lt;P&gt;There are newer DAX functions that are similar to SQL's LAG. For example, &lt;A href="https://learn.microsoft.com/en-us/dax/offset-function-dax" target="_blank"&gt;OFFSET&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="php"&gt;IsSiteDown =
VAR Summary =
    SUMMARIZE (
        Table2,
        Table2[sitename],
        Table2[category],
        Table2[firstoccurrence]
    )
VAR Prev1Cat =
    MAXX (
        OFFSET (
            -1,
            Summary,
            ORDERBY ( Table2[firstoccurrence] ),
            PARTITIONBY ( Table2[sitename] )
        ),
        Table2[category]
    )
VAR Prev2Cat =
    MAXX (
        OFFSET (
            -2,
            Summary,
            ORDERBY ( Table2[firstoccurrence] ),
            PARTITIONBY ( Table2[sitename] )
        ),
        Table2[category]
    )
VAR Result =
    IF (
        Table2[category] = "Site Down"
            &amp;amp;&amp;amp; Prev1Cat = "Low Voltage"
            &amp;amp;&amp;amp; Prev2Cat = "AC Mains Failure",
        "Yes",
        "No"
    )
RETURN
    Result&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Dec 2022 17:59:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2989125#M100502</guid>
      <dc:creator>AlexisOlson</dc:creator>
      <dc:date>2022-12-26T17:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2994730#M100863</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="39298" data-lia-user-login="AlexisOlson" class="lia-mention lia-mention-user"&gt;AlexisOlson&lt;/a&gt;&amp;nbsp;Yes It works. Thanks a lot!&lt;/P&gt;&lt;P&gt;Could you please explain the use of MAXX? Could it be any other iterator?&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2022 10:49:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2994730#M100863</guid>
      <dc:creator>ahsan005</dc:creator>
      <dc:date>2022-12-30T10:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2994731#M100864</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="92178" data-lia-user-login="AlB" class="lia-mention lia-mention-user"&gt;AlB&lt;/a&gt;&amp;nbsp;thanks for your reply. it seems to work for small datasets but for the actual data (&amp;gt;50k+ rows) it crashed. However alxexis's answer seems to be the most comprehensive one!&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2022 10:52:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2994731#M100864</guid>
      <dc:creator>ahsan005</dc:creator>
      <dc:date>2022-12-30T10:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2994757#M100866</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="460868" data-lia-user-login="FreemanZ" class="lia-mention lia-mention-user"&gt;FreemanZ&lt;/a&gt;&amp;nbsp;thanks for your reply. Your solution seems to work when I add the index using the sitename &amp;amp; category column, however with all the other columns mentioned in the sample data file it is giving 'No' for all the rows. Could we specify a column when trying to add an index column?&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2022 11:24:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2994757#M100866</guid>
      <dc:creator>ahsan005</dc:creator>
      <dc:date>2022-12-30T11:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Previous Two Rows with a Current Row using DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2995160#M100904</link>
      <description>&lt;P&gt;OFFSET returns a row and I want a specific column from that row, so I selected it using MAXX. There isn't anything special about MAXX; you could use other iterators too. SELECTCOLUMNS would work too and is probably more intuitive.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2022 17:11:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Compare-Previous-Two-Rows-with-a-Current-Row-using-DAX/m-p/2995160#M100904</guid>
      <dc:creator>AlexisOlson</dc:creator>
      <dc:date>2022-12-30T17:11:52Z</dc:date>
    </item>
  </channel>
</rss>

