<?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 Calculated Column for PRE/POST Sequence in Power BI in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Calculated-Column-for-PRE-POST-Sequence-in-Power-BI/m-p/4249586#M168258</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.&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;&lt;A href="https://learn.microsoft.com/en-us/dax/rank-function-dax?wt.mc_id=DP-MVP-5004989" target="_blank"&gt;RANK function (DAX) - DAX | Microsoft Learn&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;PRE_POST_Sequence =
VAR _PRESequenceTable =
    FILTER (
        SUMMARIZE (
            'ALL Depots',
            'ALL Depots'[Depot],
            'ALL Depots'[Transaction date],
            'ALL Depots'[PRE/POST]
        ),
        'ALL Depots'[PRE/POST] = "PRE"
    )
VAR _POSTSequenceTable =
    FILTER (
        SUMMARIZE (
            'ALL Depots',
            'ALL Depots'[Depot],
            'ALL Depots'[Transaction date],
            'ALL Depots'[PRE/POST]
        ),
        'ALL Depots'[PRE/POST] = "POST"
    )
VAR _PRERank =
    RANK (
        SKIP,
        _PRESequenceTable,
        ORDERBY ( 'ALL Depots'[Transaction date], DESC ),
        ,
        PARTITIONBY ( 'ALL Depots'[Depot] ),
        MATCHBY (
            'ALL Depots'[Depot],
            'ALL Depots'[Transaction date],
            'ALL Depots'[PRE/POST]
        )
    ) * -1
VAR _POSTRank =
    RANK (
        SKIP,
        _POSTSequenceTable,
        ORDERBY ( 'ALL Depots'[Transaction date], ASC ),
        ,
        PARTITIONBY ( 'ALL Depots'[Depot] ),
        MATCHBY (
            'ALL Depots'[Depot],
            'ALL Depots'[Transaction date],
            'ALL Depots'[PRE/POST]
        )
    )
RETURN
    _PRERank + _POSTRank
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 20 Oct 2024 13:17:13 GMT</pubDate>
    <dc:creator>Jihwan_Kim</dc:creator>
    <dc:date>2024-10-20T13:17:13Z</dc:date>
    <item>
      <title>DAX Calculated Column for PRE/POST Sequence in Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Calculated-Column-for-PRE-POST-Sequence-in-Power-BI/m-p/4249578#M168256</link>
      <description>&lt;P&gt;I'm working with a Power BI model that includes a table called "All Depots". This is a large, granular table that includes details by customer and product. However, for this particular problem, I'm focusing only on the "Depot", "PRE/POST", and "Transaction date" fields.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;## Objective&lt;BR /&gt;I need to create a calculated column that assigns a sequence number to PRE and POST periods for each depot. The sequence should work as follows:&lt;/P&gt;&lt;P&gt;- For PRE periods:&lt;BR /&gt;- The last (most recent) PRE date should be assigned -1&lt;BR /&gt;- The second-to-last PRE date should be assigned -2&lt;BR /&gt;- The third-to-last PRE date should be assigned -3&lt;BR /&gt;- And so on...&lt;/P&gt;&lt;P&gt;- For POST periods:&lt;BR /&gt;- The first (earliest) POST date should be assigned +1&lt;BR /&gt;- The second POST date should be assigned +2&lt;BR /&gt;- The third POST date should be assigned +3&lt;BR /&gt;- And so on...&lt;/P&gt;&lt;P&gt;- The sequence should reset for each depot&lt;/P&gt;&lt;P&gt;## Current Approach&lt;BR /&gt;I've tried several DAX formulas, including variations of RANKX and COUNTROWS, but I haven't been able to achieve the desired result. Here's an example of one approach I've tried:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;PRE_POST_Sequence =
VAR CurrentDepot = 'All Depots'[Depot]
VAR CurrentDate = 'All Depots'[Transaction date]
VAR CurrentPREPOST = 'All Depots'[PRE/POST]
VAR PRE_Sequence =
IF(CurrentPREPOST = "PRE",
-RANKX(
FILTER(ALL('All Depots'),
'All Depots'[Depot] = CurrentDepot &amp;amp;&amp;amp;
'All Depots'[PRE/POST] = "PRE"
),
'All Depots'[Transaction date],
,
DESC
),
BLANK()
)
VAR POST_Sequence =
IF(CurrentPREPOST = "POST",
RANKX(
FILTER(ALL('All Depots'),
'All Depots'[Depot] = CurrentDepot &amp;amp;&amp;amp;
'All Depots'[PRE/POST] = "POST"
),
'All Depots'[Transaction date],
,
ASC
),
BLANK()
)
RETURN
IF(ISBLANK(PRE_Sequence), POST_Sequence, PRE_Sequence)&lt;/LI-CODE&gt;&lt;P&gt;```&lt;/P&gt;&lt;P&gt;## Problem&lt;BR /&gt;The current formula isn't producing the expected results. It's either not sequencing correctly or not resetting for each depot as needed.&lt;/P&gt;&lt;P&gt;## Question&lt;BR /&gt;Can anyone suggest a DAX formula that would achieve the desired sequencing as described above? I'm open to completely different approaches if they can solve this problem more effectively.&lt;/P&gt;&lt;P&gt;## Additional Information&lt;BR /&gt;- The "All Depots" table contains multiple rows per day per depot.&lt;BR /&gt;- The "PRE/POST" field contains only "PRE" or "POST" values.&lt;BR /&gt;- The "Transaction date" is a date field.&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;Thank you in advance for any help or suggestions!&lt;/P&gt;</description>
      <pubDate>Sun, 20 Oct 2024 12:18:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Calculated-Column-for-PRE-POST-Sequence-in-Power-BI/m-p/4249578#M168256</guid>
      <dc:creator>Adnaniqb</dc:creator>
      <dc:date>2024-10-20T12:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Calculated Column for PRE/POST Sequence in Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Calculated-Column-for-PRE-POST-Sequence-in-Power-BI/m-p/4249586#M168258</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.&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;&lt;A href="https://learn.microsoft.com/en-us/dax/rank-function-dax?wt.mc_id=DP-MVP-5004989" target="_blank"&gt;RANK function (DAX) - DAX | Microsoft Learn&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;PRE_POST_Sequence =
VAR _PRESequenceTable =
    FILTER (
        SUMMARIZE (
            'ALL Depots',
            'ALL Depots'[Depot],
            'ALL Depots'[Transaction date],
            'ALL Depots'[PRE/POST]
        ),
        'ALL Depots'[PRE/POST] = "PRE"
    )
VAR _POSTSequenceTable =
    FILTER (
        SUMMARIZE (
            'ALL Depots',
            'ALL Depots'[Depot],
            'ALL Depots'[Transaction date],
            'ALL Depots'[PRE/POST]
        ),
        'ALL Depots'[PRE/POST] = "POST"
    )
VAR _PRERank =
    RANK (
        SKIP,
        _PRESequenceTable,
        ORDERBY ( 'ALL Depots'[Transaction date], DESC ),
        ,
        PARTITIONBY ( 'ALL Depots'[Depot] ),
        MATCHBY (
            'ALL Depots'[Depot],
            'ALL Depots'[Transaction date],
            'ALL Depots'[PRE/POST]
        )
    ) * -1
VAR _POSTRank =
    RANK (
        SKIP,
        _POSTSequenceTable,
        ORDERBY ( 'ALL Depots'[Transaction date], ASC ),
        ,
        PARTITIONBY ( 'ALL Depots'[Depot] ),
        MATCHBY (
            'ALL Depots'[Depot],
            'ALL Depots'[Transaction date],
            'ALL Depots'[PRE/POST]
        )
    )
RETURN
    _PRERank + _POSTRank
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Oct 2024 13:17:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Calculated-Column-for-PRE-POST-Sequence-in-Power-BI/m-p/4249586#M168258</guid>
      <dc:creator>Jihwan_Kim</dc:creator>
      <dc:date>2024-10-20T13:17:13Z</dc:date>
    </item>
  </channel>
</rss>

