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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Mike1983
Regular Visitor

How to count consecutive date ranges in Power Query M?

Hello! Hopefully someone can help me!

 

I have a table of absence start and end dates (300k rows). Where absences are consecutive (previous end date is the day before the next start date), I want to capture the first of the consecutive dates in that series as Consecutive Abs Start. It could be any number of absences in a consecutive series. Please see the desired result below. I want to do this in PowerQuery M (instead of PowerBI DAX front end) so I can use the result further in my transformations.

 

If anyone can help me that would be great! Thanks.  Mike

 

 

PersonAbs StartAbs EndConsecutive Abs Start (desired result)
2000000101.01.202402.01.202401.01.2024
2000000115.01.202417.01.202415.01.2024
2000000118.01.202419.01.202415.01.2024
2000000120.01.202420.01.202415.01.2024
2000000121.01.202421.01.202415.01.2024
2000000122.01.202422.01.202415.01.2024
2000000123.01.202423.01.202415.01.2024
2000000101.02.202401.02.202401.02.2024
2000000115.02.202415.02.202415.02.2024
2000000116.02.202416.02.202415.02.2024
2000000101.03.202401.03.202401.03.2024

 

 

 

1 ACCEPTED SOLUTION
dufoq3
Community Champion
Community Champion

Hi @Mike1983,

 

Result:

dufoq3_0-1712143918501.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddDRCoAgDIXhd/FaYjuW1bOI7/8atgg7jSbe/PDBdK0lyH005SS6XBeC1QIzev4w3YjpHrKD2RkxCDEKz/htFJ6BWfgFFGYlYrYQzIW88bOQySg8q8xqxGxO4aFP9D4A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t, #"Abs Start" = _t, #"Abs End" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Abs Start", type date}, {"Abs End", type date}}, "sk-SK"),
    #"Added Index1" = Table.AddIndexColumn(#"Changed Type", "IndexHelper", 0, 1, Int64.Type),
    #"Added Dates" = Table.AddColumn(#"Added Index1", "Dates", each List.Dates([Abs Start], Duration.TotalDays([Abs End] - [Abs Start])+1, #duration(1,0,0,0)), type list),
    #"Expanded Dates" = Table.ExpandListColumn(#"Added Dates", "Dates"),
    #"Added Index" = Table.AddIndexColumn(#"Expanded Dates", "Index", 0, 1, Int64.Type),
    #"Grouped Rows" = Table.Group(#"Added Index",
        {"Person", "Dates", "Index"}, { {"All", each _, type table}, {" Consecutive Abs Start", each List.Min([Dates]), type date}},
        GroupKind.Local,
        (s,c)=> Byte.From(c[Index]-s[Index] <> Duration.Days(c[Dates]-s[Dates]))),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Abs Start", "Abs End", "IndexHelper"}, {"Abs Start", "Abs End", "IndexHelper"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded All", {"IndexHelper"}),
    #"Removed Columns" = Table.RemoveColumns(#"Removed Duplicates",{"IndexHelper", "Dates", "Index"})
in
    #"Removed Columns"

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

3 REPLIES 3
dufoq3
Community Champion
Community Champion

Hi @Mike1983,

 

Result:

dufoq3_0-1712143918501.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddDRCoAgDIXhd/FaYjuW1bOI7/8atgg7jSbe/PDBdK0lyH005SS6XBeC1QIzev4w3YjpHrKD2RkxCDEKz/htFJ6BWfgFFGYlYrYQzIW88bOQySg8q8xqxGxO4aFP9D4A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t, #"Abs Start" = _t, #"Abs End" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Abs Start", type date}, {"Abs End", type date}}, "sk-SK"),
    #"Added Index1" = Table.AddIndexColumn(#"Changed Type", "IndexHelper", 0, 1, Int64.Type),
    #"Added Dates" = Table.AddColumn(#"Added Index1", "Dates", each List.Dates([Abs Start], Duration.TotalDays([Abs End] - [Abs Start])+1, #duration(1,0,0,0)), type list),
    #"Expanded Dates" = Table.ExpandListColumn(#"Added Dates", "Dates"),
    #"Added Index" = Table.AddIndexColumn(#"Expanded Dates", "Index", 0, 1, Int64.Type),
    #"Grouped Rows" = Table.Group(#"Added Index",
        {"Person", "Dates", "Index"}, { {"All", each _, type table}, {" Consecutive Abs Start", each List.Min([Dates]), type date}},
        GroupKind.Local,
        (s,c)=> Byte.From(c[Index]-s[Index] <> Duration.Days(c[Dates]-s[Dates]))),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Abs Start", "Abs End", "IndexHelper"}, {"Abs Start", "Abs End", "IndexHelper"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded All", {"IndexHelper"}),
    #"Removed Columns" = Table.RemoveColumns(#"Removed Duplicates",{"IndexHelper", "Dates", "Index"})
in
    #"Removed Columns"

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Fantastic, thank you. I got there in the end but I used about 10 times as many steps, this is certainly much more elegant. 

dufoq3
Community Champion
Community Champion

You're welcome Mike. Enjoy. Don't forget that your data needs to be sorted by Abs Start.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors