Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Create start and end date columns from rows

Hi! I'm new to power query and am stuck. I was able to manually create a table that works as needed in power bi but am having trouble automating its creation based on user inputs in an excel table. I...
  • jgeddes's avatar
    2 years ago

    One approach would be as follows.

    1. Add a column that returns the 'Start Date' for all rows that have a Milestone that does not end with 'End'
    2. then add a column that returns the 'Start Date' for rows that do not end in 'Start'
    3. on the second created column 'Fill Up' to fill in the null values
    4. remove the 'Start' or 'End' from any rows in the 'MIlestone' column that have those as ending words
    5. filter out null values from the first created column
    6. remove the original 'Start Date' column
    7. rename created columns as needed

    As an example, starting with...

    and ending up with...

     

     

    with the code...

     

    let
        Source = 
        Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZKxCoMwFEV/RTILaV6M2LFIC91ER3EIKjQgEUKkv19FWiE28blkeffm5YRT10SkQGKSl8V8cspSChcA0sTfSfkoonwyptc2qtpX301DH1VWGjsPGaOwFPhR4a47T7waB9UqK60adWARFeeK60KgiVu7vaXpdvl5ICjjSzhxWJ5aWSUHPPyugIT3LzqA9yz0wrv5HTzLxM+JjMJ1u2adnHAiUPj3LWsc6wS4T8Y5wdwa0omNBelEoICEDzgRhvc64YEPO9F8AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProjectID = _t, Milestone = _t, #"Start Date" = _t]),
        changeDataTypes = 
        Table.TransformColumnTypes(Source,{{"ProjectID", Int64.Type}, {"Milestone", type text}, {"Start Date", type date}}),
        addStartDate = 
        Table.AddColumn(changeDataTypes, "sDate", each if not Text.EndsWith([Milestone], "End") then [Start Date] else null, type date),
        addEndDate = 
        Table.AddColumn(addStartDate, "eDate", each if Text.EndsWith([Milestone], "Start") then null else [Start Date], type date),
        fillUpEndDate = 
        Table.FillUp(addEndDate,{"eDate"}),
        cleanMilestones = 
        Table.TransformColumns(fillUpEndDate, {{"Milestone", each if Text.EndsWith(_, "Start") or Text.EndsWith(_, "End") then Text.BeforeDelimiter(_, " ", RelativePosition.FromEnd) else _, type text}}),
        filterRows = 
        Table.SelectRows(cleanMilestones, each ([sDate] <> null)),
        removeOriginalDate = 
        Table.RemoveColumns(filterRows,{"Start Date"}),
        renameColumns = 
        Table.RenameColumns(removeOriginalDate,{{"sDate", "Start Date"}, {"eDate", "End Date"}})
    in
        renameColumns