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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
WorkHard_
Frequent Visitor

Calculate consecutive days and reset the count each time there's a break

Spent a few days attempting this and there are a lot of variations online based on how the databased is stored.
Trying to calculate consecutive days for a dataset that lists one row per day for each employee. The dataset will list a date regardless if they worked or not and I catch that using the WorkedFlag. (if Date NOT blank, 1,0)

Employee NameDateWorkedFlagDesiredStreakGroupDesiredStreak
John8/1/2025103
John8/2/2025103
John8/3/2025103
John8/4/20250  
John8/5/2025112
John8/6/2025112
John8/7/20250  
John8/8/2025121
John8/9/20250  
John8/10/2025131
John8/11/20250  
John8/12/20250  


I believe I need to solve this in 2 stages.
First stage is to identify a streak group that will increment each time a break in the consecutive days occurs.
So for each employee, calculate a unique incrementing streak group.
This is the best attempt right now which keeps incrementing the streak group on break days for some reason.

StreakGroup =
VAR Emp = MainTable[Employee Name]
VAR CurrentDate = MainTable[Date]
VAR RowNum =
CALCULATE(
    COUNTROWS(MainTable),
    FILTER(
        MainTable,
        MainTable[Employee Name] = Emp &&
        MainTable[Date] <=CurrentDate
    )
)
VAR WorkCount =
CALCULATE(
    COUNTROWS(MainTable),
    FILTER(
        MainTable,
        MainTable[Employee Name] = Emp &&
        MainTable[Date] <= CurrentDate &&
        MainTable[WorkedFlag] = 1
    )
)
RETURN RowNum - WorkCount


Second stage is to actually count the consecutive days:

Streak = CALCULATE(COUNT(MainTable[Date]),FILTER(MainTable,
MainTable[Employee Name]=EARLIER(MainTable[Employee Name])&&
MainTable[StreakGroup]=EARLIER(MainTable[StreakGroup])))


Does that look correct or should I use another approach?

 
6 REPLIES 6
Aburar_123
Responsive Resident
Responsive Resident

Hi @WorkHard_ ,

Please try this Calculated column logic,

Desired Streak1 =
var prev_0_date = calculate(MAX('Table'[Date].[Date]),FILTER('Table','Table'[Date]<EARLIER('Table'[Date]) && 'Table'[WorkedFlag]=0))
var next_0_date = calculate(MIN('Table'[Date].[Date]),FILTER('Table','Table'[Date]>EARLIER('Table'[Date]) && 'Table'[WorkedFlag]=0))
return if('Table'[WorkedFlag]<>0,COUNTROWS(FILTER('Table','Table'[Date]>prev_0_date && 'Table'[Date]<next_0_date)))
 

Aburar_123_0-1755570844707.png

 

Thanks

v-hjannapu
Community Support
Community Support

Hi @WorkHard_,

Thank you  for reaching out to the Microsoft fabric community forum.

I would also take a moment to thank @Ashish_Mathur , for actively participating in the community forum and for the solutions you have been sharing in the community forum. Your contributions make a real difference.

 I also tried few steps on my side, but the results are not matching as expected. Can you please confirm where exactly you are getting stuck. Also, if you can share what exact output you are expecting vs what you are currently getting, it will help us guide you better.

Regards,
Harshitha.

Hi @WorkHard_,
I hope the above details help you fix the issue. If you still have any questions or need more help, feel free to reach out. We are always here to support you.


Regards,
Harshitha.

Ashish_Mathur
Super User
Super User

Hi,

Where are you stuck and what result are you expecting?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @Ashish_Mathur and @v-hjannapu.
The results expected are in the DesiredStreakGroup and DesiredStreak columns in the original post.

Current incorrect result using the above formulas is:

Employee NameDateWorkedFlagIncorrectStreakGroupIncorrectStreak
John8/1/2025103
John8/2/2025103
John8/3/2025103
John8/4/2025013
John8/5/2025113
John8/6/2025113
John8/7/2025022
John8/8/2025122
John8/9/2025032
John8/10/2025132
John8/11/2025041
John8/12/202505 


I'm stuck at calculating the consecutive days in the above sample data and reset the count each time there is a break. I tried the approach I posted there with the StreakGroup and Streak calculated columns but I'm unable to get the correct result.

The correct expected result is posted in the original post and converted to text below:

Employee NameDateWorkedFlagDesiredStreakGroupDesiredStreak
John8/1/2025103
John8/2/2025103
John8/3/2025103
John8/4/20250  
John8/5/2025112
John8/6/2025112
John8/7/20250  
John8/8/2025121
John8/9/20250  
John8/10/2025131
John8/11/20250  
John8/12/20250  

 



John worked consecutevely from 8/1/2025 to 8/3/2025, the result should be 3.
John did not work on 8/4/2025, the result should be blank.
John worked consecutevely from 8/5/2025 to 8/6/2025, the result should be 2.
John did not work on 8/7/2025, the result should be blank.
John worked on 8/8/2025, the result should be 1.
John did not work on 8/9/2025, the result should be blank.
John worked on 8/10/2025, the result should be 1.
John did not work on 8/11/2025, the result should be blank.
John did not work on 8/12/2025, the result should be blank.



Hi,

This M code in Power Query works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee Name", type text}, {"Date", type date}, {"WorkedFlag", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Employee Name", "WorkedFlag"}, {{"T", each List.Sum([WorkedFlag]), type nullable number}, {"A", each _}},GroupKind.Local),
    #"Expanded A" = Table.ExpandTableColumn(#"Grouped Rows", "A", {"Date"}, {"Date"})
in
    #"Expanded A"

Hope this helps.

Ashish_Mathur_0-1755560995605.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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