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

Win a FREE 3 Day Ticket to FabCon Vienna. Apply now

Reply
vanschex
Frequent Visitor

Grouping consecutive dates into date ranges for sick leave

I would really appreaciate any assistance in sloving my problem please. This is my current data table structure in Power BI:

 

EmployeeDATEShift Type
A1/01/2018Sick
A2/01/2018Rostered off
A3/01/2018Rostered off
A4/01/2018Sick
A5/01/2018Work
A6/01/2018Work
A7/01/2018Sick
A8/01/2018Work
A9/01/2018Sick
A10/01/2018Sick
A11/01/2018Sick

 

I want to determine number of continuous occurrences of sick leave but if rostered off and they have sick leave either side, then that is one occurrence. Expected result would be:

 

EmployeeDATE FROMDATE UNTIL
A1/01/20184/01/2018
A7/01/20187/01/2018
A9/01/2018

11/01/2018

 

Many thanks!

2 REPLIES 2
OwenAuger
Super User
Super User

@vanschex

Here's an example of how you could transform the data in Power Query.

Paste this into a blank query to see how it works.

The main steps are:

  1. Sort by Employee & Date
  2. Remove Rostered Off rows
  3. Group by Shift Type using GroupKind.Local, so each block of Sick/Work is grouped together, and add columns for the min/max date from each group
  4. Keep only Sick groups

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTLUNzDUNzIwtACygzOTs5VidSASRkgSQfnFJalFqSkK+WlpcAXGhBSY4DLaFEkiPL8IIWGGS8Icl1EWuHRY4tJhaIBTBjMsYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Employee = _t, DATE = _t, #"Shift Type" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee", type text}, {"DATE", type date}, {"Shift Type", type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Shift Type] <> "Rostered off")),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Employee", Order.Ascending}, {"DATE", Order.Ascending}}),
    Buffer = Table.Buffer(#"Sorted Rows"),
    #"Grouped Rows" = Table.Group(Buffer, {"Employee", "Shift Type"}, {{"DATE FROM", each List.Min([DATE]), type date}, {"DATE UNTIL", each List.Max([DATE]), type date}},GroupKind.Local),
    #"Filtered Rows1" = Table.SelectRows(#"Grouped Rows", each ([Shift Type] = "Sick")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows1",{"Shift Type"})
in
    #"Removed Columns"

Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Thanks so much Owen Auger! This achieves exactly what I'm after and a very elegant solution if my source file matches the sample file supplied. Unfortunately I am unable to make this work in my model as the determination of "Shift Type" is performed via calculated columns within a table which are not visible to a query created that references my source Power BI table. Is there an equivalent DAX solution that would achieve the same result?

 

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.