r/ExcelPowerQuery • u/Euphoric_Lecture9405 • Mar 25 '25
Exclude oldest program if more than 1
Very new to PowerQuery. This should be an easy one but I just can't figure it out. I need to exclude any rows of data for people that have more than one Day Program. I need to keep the most recently enrolled day program as well as any residential programs they are in. It would be easy if I only had the Day programs but the Residential are listed as well so not sure how to group or filter out only the oldest Day Program. In the example below...I would want it to Exclude Woodstock's Activities A because it is the oldest Enrollment Date. Any suggestions is greatly appreciated!!!
| Initial Data | ||||
|---|---|---|---|---|
| Name | Status | Enrollment Date | Category | Program |
| Snoopy | Active | 1/1/2024 | Residential | House A |
| Woodstock | Active | 3/1/2024 | Residential | House B |
| Snoopy | Active | 1/1/2024 | Day | Activities A |
| Woodstock | Active | 3/1/2024 | Day | Activities A |
| Woodstock | Active | 6/1/2024 | Day | Activities B |
| Desired Data | ||||
| Name | Status | Enrollment Date | Category | Program |
| Snoopy | Active | 1/1/2024 | Residential | House A |
| Woodstock | Active | 3/1/2024 | Residential | House B |
| Snoopy | Active | 1/1/2024 | Day | Activities A |
| Woodstock | Active | 6/1/2024 | Day | Activities B |
3
u/Dwa_Niedzwiedzie Apr 07 '25
You can do it with one query, splitting the data into residential and day parts, sorting the second one by user and day (descending), removing duplicates and appending both sets at the end. There's one tricky thing thou: PQ treats sorting as non-essencial step, so under the hood it is sometimes shifted to the end to optimize the whole query. Buffering or indexing prevents this behaviour, that's why I added dummy index column after sorting.
2
u/johndering Mar 25 '25
Steps:
Hope this helps.
PS. Put everything in one PQ script, if required.
1
u/coolfozzie Mar 25 '25
Have you tried asking CoPilot? It’s actually pretty decent at coming up with power query solutions. You may have to give it context and probably paste your code used to this point.