Forum Discussion

Ryan0096's avatar
Ryan0096
Frequent Visitor
2 years ago
Solved

Need date differences by status change when status changes

We have work IDs that can go through status 0-10  and it is not uncommon for it to go forward and backwards.  Trying to get the date changes between the sequences so min when it ws in status x and mi...
  • ronrsnfld's avatar
    ronrsnfld
    2 years ago

    Your updated example makes more sense, except for the missing Datediff entry on row 4 which I assume is a typo.  Try this:

    let
        Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Status", Int64.Type}, {"Date", type date}}),
    
    //Note use of GroupKind.Local
    //Add a column showing the minimum date for each status group
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ID", "Status"}, {
            {"all", (t)=> Table.AddColumn(t, "Min Date", each List.Min(t[Date])),     
            type table [ID=nullable number, Status=nullable number, Date=nullable date, Min Date = nullable date]}
            }, GroupKind.Local),
    
        #"Expanded all" = Table.ExpandTableColumn(#"Grouped Rows", "all", {"Date", "Min Date"}),
    
    //Shift the min date column for easier comparisons
        #"Shifted Min Date" = Table.FromColumns(
            Table.ToColumns(#"Expanded all") &
            {{null} & List.RemoveLastN(#"Expanded all"[Min Date])},
            type table [ID=nullable number, Status=nullable number, Date=nullable date, Min Date = nullable date, Shifted Min = nullable date]
            ),
    
    //Add the Datediff column at the change in status position
        #"Add Need"= Table.AddColumn(#"Shifted Min Date", "Datediff on Change", 
                each if Duration.Days([Min Date] - [Shifted Min])= 0 then null else Duration.Days([Min Date] - [Shifted Min]), Int64.Type),
        #"Removed Columns" = Table.RemoveColumns(#"Add Need",{"Shifted Min", "ID", "Min Date"})
    in
        #"Removed Columns"