Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
MBisceglia
Frequent Visitor

Dax to obtain count of consecutive days with non consecutive dates

Hi everyone, 

I have a table organized by Date + ID and I  would like to get in a 3rd column a count of consecutive days based on these 2 variables: 

 

DateIDConsecutiveDaysNum
1/1/2020521
1/2/2020522
1/3/2020523
1/4/2020524
1/8/2020521
1/9/2020522
1/1/20207891
1/2/20207892
1/3/20207893
1/15/20207891

 

Is there a DAX code for this? Any help will be really appreciated, thanks in advance!

 

Michelle

1 ACCEPTED SOLUTION
rfigtree
Resolver III
Resolver III

someone is bound to have a more elegant solution, but heres mine.

all calculated columns

 

isConsecutiveForward = 
var NextEntryDate = CALCULATE(min(TableX[Date]),
                    filter( ALLEXCEPT(TableX,TableX[ID]),
                            TableX[Date]  >EARLIER(TableX[Date])))
return if(NextEntryDate - TableX[Date]=1,TRUE(),FALSE())
isConsequtiveBackward = 
var PreviousEntryDate = CALCULATE(max(TableX[Date]),
                        filter(ALLEXCEPT(TableX,TableX[ID]),
                               TableX[Date] <EARLIER(TableX[Date])))
return if( TableX[Date]-PreviousEntryDate=1,TRUE(),FALSE())
ConsDays=
var StartOfSeq=CALCULATE(MAX(TableX[Date]),
               FILTER(ALLEXCEPT(TableX,TableX[ID]),
                      [isConsecutiveForward] && 
                      not([isConsecutiveBackward])&&
                      TableX[Date] < EARLIER(TableX[Date])))
var Seq=CALCULATE(COUNTROWS(TableX),
                  FILTER(ALLEXCEPT(TableX,TableX[ID]),
                        TableX[Date]<=EARLIER(TableX[Date])&&
                        TableX[Date] >=StartOfSeq))
return SWITCH(TRUE(),
           NOT([isConsecutiveBackward]||[isConsecutiveForward]),1,
           [isConsecutiveForward]&& not([isConsecutiveBackward]),1,
           Seq)

 

rfigtree_0-1615003117291.png

 

View solution in original post

2 REPLIES 2
MBisceglia
Frequent Visitor

It worked like a charm! Thanks rfigtree!

 
 
rfigtree
Resolver III
Resolver III

someone is bound to have a more elegant solution, but heres mine.

all calculated columns

 

isConsecutiveForward = 
var NextEntryDate = CALCULATE(min(TableX[Date]),
                    filter( ALLEXCEPT(TableX,TableX[ID]),
                            TableX[Date]  >EARLIER(TableX[Date])))
return if(NextEntryDate - TableX[Date]=1,TRUE(),FALSE())
isConsequtiveBackward = 
var PreviousEntryDate = CALCULATE(max(TableX[Date]),
                        filter(ALLEXCEPT(TableX,TableX[ID]),
                               TableX[Date] <EARLIER(TableX[Date])))
return if( TableX[Date]-PreviousEntryDate=1,TRUE(),FALSE())
ConsDays=
var StartOfSeq=CALCULATE(MAX(TableX[Date]),
               FILTER(ALLEXCEPT(TableX,TableX[ID]),
                      [isConsecutiveForward] && 
                      not([isConsecutiveBackward])&&
                      TableX[Date] < EARLIER(TableX[Date])))
var Seq=CALCULATE(COUNTROWS(TableX),
                  FILTER(ALLEXCEPT(TableX,TableX[ID]),
                        TableX[Date]<=EARLIER(TableX[Date])&&
                        TableX[Date] >=StartOfSeq))
return SWITCH(TRUE(),
           NOT([isConsecutiveBackward]||[isConsecutiveForward]),1,
           [isConsecutiveForward]&& not([isConsecutiveBackward]),1,
           Seq)

 

rfigtree_0-1615003117291.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors