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
StevenF19
Frequent Visitor

First and Last Dates in Sequence

Hello,

 

I've got a dataset of employee timesheet data, with each row representing a single day of time submitted by a particular employee, with the column [ShiftDate] being the day that time is applicable for. I am looking to track consecutive days of time for employees and then grab the earliest and latest [ShiftDate] from that sequence. I've been able to create a [Sequence] column that shows the number of consecutive days at that point but haven't been able to progress further. How would I go about doing this?

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@StevenF19  you would need a additional column called Grouping which assigns the same value to consecutive dates and the Grouping increases when consecution breaks like following

smpa01_0-1637965601741.png

which you can generate by following PQ - both Grouping and Sequence

 

let
   Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjFV0lEyNNQ3MtM3MjAyVIrVQRU2xy5sgVXY2AAhDBI1wzQbWRhmdiwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [EmployeeNum = _t, ShiftDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EmployeeNum", Int64.Type}, {"ShiftDate", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"EmployeeNum"}, {{"ad", each _, type table [EmployeeNum=nullable number, ShiftDate=nullable date]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let x =[ad],
    ShiftDate=x[ShiftDate],
    Custom1 = List.Generate(
        ()=>[i=0,k=ShiftDate{i},j=1],
        each [i]<List.Count(ShiftDate),
        each [i=[i]+1,k=ShiftDate{i},j=if Duration.Days(k-[k])=1 then [j] else [j]+1], each [j]),
    Custom2 = Table.ColumnNames([ad]),
    Custom3 = Table.ToColumns([ad]),
    Custom4 = List.Combine({Custom2,{"Grouping"}}),
    Custom5 = Table.FromColumns(Custom3&{Custom1},Custom4)   
in
    Custom5),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"EmployeeNum", "ShiftDate", "Grouping"}, {"EmployeeNum", "ShiftDate", "Grouping"}),
    #"Grouped Rows1" = Table.Group(#"Expanded Custom", {"EmployeeNum", "Grouping"}, {{"ad", each _, type table [EmployeeNum=number, ShiftDate=date, Grouping=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows1", "Custom.1", each let x = [ad],
    y = Table.Sort(x,{{"ShiftDate", Order.Ascending}}),
    z = Table.AddIndexColumn(y, "Sequence", 1, 1, Int64.Type) in z),
    #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom1",{"Custom.1"}),
    #"Expanded Custom.1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Custom.1", {"EmployeeNum", "ShiftDate", "Grouping", "Sequence"}, {"EmployeeNum", "ShiftDate", "Grouping", "Sequence"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom.1",{{"EmployeeNum", Int64.Type}, {"ShiftDate", type date}, {"Grouping", Int64.Type}, {"Sequence", Int64.Type}})
in
    #"Changed Type1"

 

then you can write the following DAX measure to give you want you need

 

_maxDatebyEmpGrouping = CALCULATE(MAX('Table 1'[ShiftDate]),ALLEXCEPT('Table 1','Table 1'[EmployeeNum],'Table 1'[Grouping]))

_minDatebyEmpGrouping = CALCULATE(MIN('Table 1'[ShiftDate]),ALLEXCEPT('Table 1','Table 1'[EmployeeNum],'Table 1'[Grouping]))

 

 

smpa01_1-1637965748753.png

pbix is attached

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

4 REPLIES 4
smpa01
Super User
Super User

@StevenF19  you would need a additional column called Grouping which assigns the same value to consecutive dates and the Grouping increases when consecution breaks like following

smpa01_0-1637965601741.png

which you can generate by following PQ - both Grouping and Sequence

 

let
   Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjFV0lEyNNQ3MtM3MjAyVIrVQRU2xy5sgVXY2AAhDBI1wzQbWRhmdiwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [EmployeeNum = _t, ShiftDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EmployeeNum", Int64.Type}, {"ShiftDate", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"EmployeeNum"}, {{"ad", each _, type table [EmployeeNum=nullable number, ShiftDate=nullable date]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let x =[ad],
    ShiftDate=x[ShiftDate],
    Custom1 = List.Generate(
        ()=>[i=0,k=ShiftDate{i},j=1],
        each [i]<List.Count(ShiftDate),
        each [i=[i]+1,k=ShiftDate{i},j=if Duration.Days(k-[k])=1 then [j] else [j]+1], each [j]),
    Custom2 = Table.ColumnNames([ad]),
    Custom3 = Table.ToColumns([ad]),
    Custom4 = List.Combine({Custom2,{"Grouping"}}),
    Custom5 = Table.FromColumns(Custom3&{Custom1},Custom4)   
in
    Custom5),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"EmployeeNum", "ShiftDate", "Grouping"}, {"EmployeeNum", "ShiftDate", "Grouping"}),
    #"Grouped Rows1" = Table.Group(#"Expanded Custom", {"EmployeeNum", "Grouping"}, {{"ad", each _, type table [EmployeeNum=number, ShiftDate=date, Grouping=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows1", "Custom.1", each let x = [ad],
    y = Table.Sort(x,{{"ShiftDate", Order.Ascending}}),
    z = Table.AddIndexColumn(y, "Sequence", 1, 1, Int64.Type) in z),
    #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom1",{"Custom.1"}),
    #"Expanded Custom.1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Custom.1", {"EmployeeNum", "ShiftDate", "Grouping", "Sequence"}, {"EmployeeNum", "ShiftDate", "Grouping", "Sequence"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom.1",{{"EmployeeNum", Int64.Type}, {"ShiftDate", type date}, {"Grouping", Int64.Type}, {"Sequence", Int64.Type}})
in
    #"Changed Type1"

 

then you can write the following DAX measure to give you want you need

 

_maxDatebyEmpGrouping = CALCULATE(MAX('Table 1'[ShiftDate]),ALLEXCEPT('Table 1','Table 1'[EmployeeNum],'Table 1'[Grouping]))

_minDatebyEmpGrouping = CALCULATE(MIN('Table 1'[ShiftDate]),ALLEXCEPT('Table 1','Table 1'[EmployeeNum],'Table 1'[Grouping]))

 

 

smpa01_1-1637965748753.png

pbix is attached

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

@StevenF19  did you have a chance to try out the above?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
smpa01
Super User
Super User

@StevenF19  can you start by providing sample data please?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Sure, here's some sample data showing the data structure. Sequence is generated through a calculated column and if it's not needed for this calculation it can be removed.

 

EmployeeNumShiftDateSequence
1234511/26/2021   1
1234511/27/2021   2
1234511/28/2021   3
1234511/30/2021   1
2345611/26/2021   1
2345611/27/2021   2

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.