<?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 Count consecutive days in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3560498#M137233</link>
    <description>&lt;P&gt;Add a calendar table and&amp;nbsp;an index column to facilitate the calculation; but it's never for a novice as it leverages embedded table iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(2023/11/23 is marked as non-working day on purpose; therefore 2023/11/24 is calculated as consecutive day)&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 30 Nov 2023 09:24:02 GMT</pubDate>
    <dc:creator>ThxAlot</dc:creator>
    <dc:date>2023-11-30T09:24:02Z</dc:date>
    <item>
      <title>DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3559800#M137165</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I’m new to Dax.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each ID I would like to add 1 column name &lt;STRONG&gt;Count&lt;/STRONG&gt;.&lt;STRONG&gt;&amp;nbsp; &lt;/STRONG&gt;This would count consecutive days and reset to 1 if not consecutive. &amp;nbsp;My data do not have weekends date.&amp;nbsp; So if the same ID continue to the new/next weekday, I would want to keep counting just like the data below for 11/16/23, 11/17/23, 11/20/23, 11/21/23 and 11/22/23.&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;&lt;P&gt;Any help is greatly appreciated!&amp;nbsp; Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 22:28:23 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3559800#M137165</guid>
      <dc:creator>siumui</dc:creator>
      <dc:date>2023-11-29T22:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3560140#M137197</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="291020" data-lia-user-login="siumui" class="lia-mention lia-mention-user"&gt;siumui&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example of how Count could be computed in a calculated column (PBIX attached).&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Count =
-- Summarize DATE &amp;amp; ID combinations for current ID
VAR SummaryDateID =
    CALCULATETABLE (
        SUMMARIZE ( Data, Data[DATE], Data[ID] ),
        ALLEXCEPT ( Data, Data[ID] )
    ) -- Get current Date
VAR CurrentDate = Data[DATE] -- Get earliest Date for current ID
VAR MinDatePerID =
    MINX ( SummaryDateID, 'Data'[DATE] ) -- @RunID uniquely identifies the current run
-- It is defined as the difference between
--   1. The workday count from MinDatePerID and current row's date; and
--   2. The "rank" of a given date 
VAR AddRunID =
    ADDCOLUMNS (
        SummaryDateID,
        "@RunID",
            VAR WorkdayIndex =
                NETWORKDAYS ( MinDatePerID, Data[DATE] )
            VAR DateRank =
                RANK (
                    DENSE,
                    SummaryDateID,
                    ORDERBY ( Data[DATE], ASC ),
                    DEFAULT,
                    PARTITIONBY ( Data[ID] )
                )
            RETURN
                WorkdayIndex - DateRank
    ) -- Retrieve the RunID for the current row's Date.
VAR CurrentRunID =
    SELECTCOLUMNS ( FILTER ( AddRunID, 'Data'[DATE] = CurrentDate ), [@RunID] ) -- Retrieve the rows for the current run.
VAR CurrentRun =
    FILTER ( AddRunID, [@RunID] = CurrentRunID ) -- Compute the rank of the current row's Date within the current run
VAR Result =
    RANK ( DENSE, CurrentRun, ORDERBY ( Data[DATE], ASC ) )
RETURN
    Result&lt;/LI-CODE&gt;
&lt;P&gt;There could be some simplifications possible but at least this is a working calculation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 03:34:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3560140#M137197</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2023-11-30T03:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3560269#M137203</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="291020" data-lia-user-login="siumui" class="lia-mention lia-mention-user"&gt;siumui&lt;/a&gt;&amp;nbsp;，&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;not sure if i fully get you, try to&lt;/P&gt;
&lt;P&gt;1) add an index column, preferably with Power Query&lt;/P&gt;
&lt;P&gt;how:&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/power-query/add-index-column" target="_blank"&gt;https://learn.microsoft.com/en-us/power-query/add-index-column&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2)&amp;nbsp;add a calculated column like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;count2 = 
VAR _table =
ADDCOLUMNS(
    data,
    "col",
    VAR _date = [date]
    VAR _datepre =
    MAXX(
        FILTER(
            CALENDAR(MIN(data[date])-3, MAX(data[date])),
            WEEKDAY([date], 2) IN {1, 2,3, 4, 5}
                &amp;amp;&amp;amp;[date]&amp;lt;_date
        ),
        [date]    
    )
    VAR _dateofindexpre =
    MAXX(
        FILTER(
            data,
            data[index] = EARLIER(data[index]) -1
        ),
        data[date]
    )
    VAR _result =
    IF(    
        _dateofindexpre IN {_datepre, _date -1},
        0,
        1
    )
    RETURN _result
)
VAR _segstartdate =
MAXX(
    FILTER(
        _table,
        data[id]=EARLIER(data[id])
            &amp;amp;&amp;amp;data[date]&amp;lt;=EARLIER(data[date])
            &amp;amp;&amp;amp;[col]=1
    ),
    data[date]
)
VAR _result = DATEDIFF(_segstartdate, [date], DAY)+1
RETURN _result&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it worked like:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 06:11:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3560269#M137203</guid>
      <dc:creator>FreemanZ</dc:creator>
      <dc:date>2023-11-30T06:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3560498#M137233</link>
      <description>&lt;P&gt;Add a calendar table and&amp;nbsp;an index column to facilitate the calculation; but it's never for a novice as it leverages embedded table iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(2023/11/23 is marked as non-working day on purpose; therefore 2023/11/24 is calculated as consecutive day)&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 09:24:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3560498#M137233</guid>
      <dc:creator>ThxAlot</dc:creator>
      <dc:date>2023-11-30T09:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3561723#M137287</link>
      <description>&lt;P&gt;Hi ThxAlot,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help!&amp;nbsp; I download your file but I'm able to open it.&amp;nbsp; It says "...is incompatible with your current version of Microsoft Power BI Desktop.&amp;nbsp; Please install the latest version and try opening the document again."&lt;BR /&gt;&lt;BR /&gt;If you don't mind can you please post the codes for creating the calendar table?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 18:58:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3561723#M137287</guid>
      <dc:creator>siumui</dc:creator>
      <dc:date>2023-11-30T18:58:43Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3562008#M137298</link>
      <description>&lt;P&gt;PQ code for tables: _CALENDAR, ATTENDANCE&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;// _CALENDAR
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdErDoAwFETRvVST0Jm2fCQCFIqParr/bYAjJNce8fJuptbg6NRLvUIXruNeQ+s+NGEizC9uy37+taAOdGAknAhnQkVU4QMyM5YpoxZUTBO2aeInZmRjn3k1nM2JD2OeeTkPzBhoXM84X/ry2gM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Working Day" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Working Day", type logical}})
in
    #"Changed Type"

// ATTENDANCE
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc67DcAgDAbhXVwjwW8jh8yC2H+NFCgv6dqvuZvTpKqs3jysWM+0VTYegN4IReiEnXAQnoBBdcWNkh7sgO/8F0XohOOH6wI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, id = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"id", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type)
in
    #"Added Index"&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DAX for calculated column&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Consecutive = 
VAR __dt = ATTENDANCE[date]
RETURN
    COUNTROWS(
        FILTER(
            ATTENDANCE,
            VAR __dt_ = ATTENDANCE[date]
            RETURN
                ATTENDANCE[id] = EARLIER( ATTENDANCE[id] )
                    &amp;amp;&amp;amp; ATTENDANCE[date] &amp;lt;= __dt
                    &amp;amp;&amp;amp; EARLIER( ATTENDANCE[Index] ) - ATTENDANCE[index] + 1
                        = COUNTROWS(
                            FILTER(
                                _CALENDAR,
                                __dt_ &amp;lt;= _CALENDAR[Date] &amp;amp;&amp;amp; _CALENDAR[Date] &amp;lt;= __dt
                                    &amp;amp;&amp;amp; _CALENDAR[Working Day]
                            )
                        )
        )
    )&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 30 Nov 2023 23:55:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3562008#M137298</guid>
      <dc:creator>ThxAlot</dc:creator>
      <dc:date>2023-11-30T23:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3567255#M137519</link>
      <description>&lt;P&gt;Hi FreemanZ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help.&amp;nbsp; I'm greatly appreciate!&amp;nbsp; It turns out I need to count consecutive business days.&amp;nbsp; ThnxAlot codes work perfect, counting consecutive business days.&lt;BR /&gt;&lt;BR /&gt;Thank you FreemanZ!!!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 23:12:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3567255#M137519</guid>
      <dc:creator>siumui</dc:creator>
      <dc:date>2023-12-04T23:12:00Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3567260#M137520</link>
      <description>&lt;P&gt;Hi ThxAlot,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Saying thank you very much to you is simply not enough!&amp;nbsp; I truly appreciate and being grateful for your help.&amp;nbsp; It took me a while to create a dynamic calendar with working day and so forth.&amp;nbsp;&amp;nbsp;Your codes works like a charm!&amp;nbsp; It turns out I need to count consecutive business days and your codes did it!!!&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for everything!&amp;nbsp; I have marked your codes as accepted solution.&amp;nbsp; Have a wonderful day!!&amp;nbsp; I hope you will continue to help others who need help just like me, getting stuck working on for a couple days and hitting dead end!&amp;nbsp; And you save the day!!!&amp;nbsp; THANK YOU!!!!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 23:19:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3567260#M137520</guid>
      <dc:creator>siumui</dc:creator>
      <dc:date>2023-12-04T23:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: DAX Count consecutive days</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3567264#M137521</link>
      <description>&lt;P&gt;Hi OwenAuger,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for taking the time to help me!&amp;nbsp; I'm greatly appreciate it!&amp;nbsp; Everyone who post a .pbix in here helping me, I cannot open it due to different version.&amp;nbsp; I have not work with rank before and this is a chance for me to work with rank from you codes.&amp;nbsp; Your codes will teach me more and I get to learn new things!&amp;nbsp; Thank you for everything.&amp;nbsp; Have a wonderful day!!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 23:24:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-Count-consecutive-days/m-p/3567264#M137521</guid>
      <dc:creator>siumui</dc:creator>
      <dc:date>2023-12-04T23:24:53Z</dc:date>
    </item>
  </channel>
</rss>

