Forum Discussion

TeisL's avatar
TeisL
Frequent Visitor
4 years ago
Solved

Calculate days between dates on 2 different rows

Hi All,   I'm looking for a way to calculate the number of days between 2 dates on 2 different rows: I have values that end on BD and end on OD. I want the number of days between the MAX date of B...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi TeisL ,

     

    In summary, the steps are: Split columns-->Groupby -->Add Custom -->Expand and remove,

     

    Below is the whole M syntax:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fdBNCoBACAXgu8y6ePrsZ9xG+w4Q3f8aWVBMNAUuhA986romlaFd5tQkdRf0IKNPW/OQ3kEUkNvpgCiKEEqY1sj+aIDJLWcQRUdkUMukSyyWqwH1GyK9JtJBy3P8ujPHtvAXxCw7HjCesu0=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WKN = _t, #"Start OD Date" = _t, #"Stop BD Date" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"WKN", type text}, {"Start OD Date", type date}, {"Stop BD Date", type date}}),
        #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "WKN", "WKN - Copy"),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Duplicated Column", "WKN - Copy", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"WKN - Copy.1", "WKN - Copy.2"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"WKN - Copy.1", Int64.Type}, {"WKN - Copy.2", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type1", {"WKN - Copy.1"}, {{"Count", each _, type table [WKN=nullable text, Start OD Date=nullable date, Stop BD Date=nullable date, #"WKN - Copy.1"=nullable number, #"WKN - Copy.2"=nullable text]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [
    max=Record.Field( Table.Max([Count] ,"Stop BD Date"),"Stop BD Date"),
    min=Record.Field(Table.Min(Table.SelectRows([Count],each [Start OD Date] <>null ),"Start OD Date"),"Start OD Date"),
    diff=Duration.Days(max-min) 
    ][diff]),
        #"Expanded Count" = Table.ExpandTableColumn(#"Added Custom", "Count", {"WKN", "Start OD Date", "Stop BD Date"}, {"WKN", "Start OD Date", "Stop BD Date"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Count",{"WKN - Copy.1"})
    in
        #"Removed Columns"

     

    Best Regards,
    Eyelyn Qin
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.