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
I have a clarifying question. There is a way to do this in DAX but if you're trying to shrink the dataset you would want to move the logic back either to the source or to the power query. Are you needing the other dates and statuses at all in the report or do you truly just want the latest for any and all visuals?
Truly the latest but I could see them wanting something for the last status of each quarter for those projects that hang out there for a time. Thanks