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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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