Forum Discussion
Calculated Column or Measure to Get Min Date, Max Date and Max Status
- 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
Wow. That is great. For my own benefit, is there a way to do that in Power Query? Removing duplicates after that would be ideal for me.
Thanks
Yes, there is a function in Power query called remove duplicates but I think what you truly want in Power query is a "group by". In Transformations if you select "group by" you can go to advanced and it will let you pick more than one column. (The basic just lets you pick one column) You'd want to pick project and status. Then in the bottom pick one aggregation, pick a max on Created date.
There is also a remove duplicates function in the power query transformation but that will not necessarily get you the latest date.