Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Calculated Column or Measure to Get Min Date, Max Date and Max Status

Hi, I am trying to get the min date, max date and last status per project.  I would love to be able to do this with columns so that I should shrink my data set by removing duplicates but a measure w...
  • AmiraBedh's avatar
    AmiraBedh
    1 year ago

    If your dataset has duplicate records (same Project, Date, and Status), you may want to remove them. In Power Query, you can do this by selecting Remove Duplicates on the ribbon.

    Then you can group the data by Project:
    - Click on the Project column.
    - Go to the Home tab, and click Group By.
    - In the Group By dialog:
    - Group by: Project
    - New column name: Min Date → Operation: Minimum → Column: Created
    - New column name: Max Date → Operation: Maximum → Column: Created
    - Click OK to apply the groupings. At this point, you will have the minimum and maximum dates for each project.

    Now you need to merge the original table to get the last status:
    - Now, you need to retrieve the status corresponding to the maximum date. To do this, merge the grouped table with the original table.
    - Click on Home > Merge Queries.
    - Merge the grouped table with the original table on:
    - The Project column in both tables.
    - The Max Date column (from the grouped table) and the Created column (from the original table).
    - Choose a Left Join (default).
    - After merging, expand the Status column from the original table to bring in the status corresponding to the maximum date.

    After expanding, you'll have the Status for the max date. You can rename the columns appropriately, such as changing the new Status column to Last Status.
    Don't forget to remove any unnecessary columns if needed.

    let
        Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
        
        RemovedDuplicates = Table.Distinct(Source),
    
        GroupedRows = Table.Group(RemovedDuplicates, {"Project"}, 
            {
                {"Min Date", each List.Min([Created]), type date}, 
                {"Max Date", each List.Max([Created]), type date}
            }),
    
        MergedTable = Table.NestedJoin(GroupedRows, {"Project", "Max Date"}, RemovedDuplicates, {"Project", "Created"}, "MergedData", JoinKind.LeftOuter),
    
        ExpandedTable = Table.ExpandTableColumn(MergedTable, "MergedData", {"Status"}, {"Last Status"}),
    
        CleanedTable = Table.SelectColumns(ExpandedTable, {"Project", "Min Date", "Max Date", "Last Status"})
    in
        CleanedTable