Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
slittleson
New Member

Filtering out previous occurrences BEFORE the date of an event

Evening.  I have a table of patient data (2 million rows) that lists all the admissions for a group of patients,  At some point in one of their admissions, an event takes place.  On subsequent admissions, this event may or may not happen again.  I am looking to exclude all the admissions BEFORE the FIRST occurence of the event, but leave in all subsequent admissions whether the event happened or not.  Example

 

Patient

Admission DateEvent HappenedCalculated Outcome

1

1/9/21NoRemove
12/10/21NoRemove
13/10/21YesInclude
110/10/21No

Include

11/11/21YesInclude
12/12/21NoInclude
22/12/21NoRemove
31/10/21NoRemove
32/11/21YesInclude
35/12/21YesInclude
41/10/21YesInclude

 

So I can identify the earliest date the event happened, but I can't work out how to remove admissions before this, and whether a loop needs to be involved.  Grateful for any help

 

Regards,

Steve

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@slittleson Well, you could create a separate query where you first filter for Yes. Then you would group by patient and do an aggregation of Minimum of Admission. You could then merge this back into your other query and expand the minimum date found for each patient. Then you could create a calculated column as to whether the date is less than this minimum date and if so flag it. Then filter out the flags.

 

 

let
    Source = Table,
    #"Filtered Rows" = Table.SelectRows(Source, each ([Event Happened] = "Yes")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Patient"}, {{"MinDate", each List.Min([Admission Date]), type nullable date}}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows", {"Patient"}, Table, {"Patient"}, "Table", JoinKind.RightOuter),
    #"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table", {"Patient", "Admission Date", "Event Happened", "Calculated Outcome"}, {"Patient.1", "Admission Date", "Event Happened", "Calculated Outcome"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Table",{"Patient"}),
    #"Added Conditional Column" = Table.AddColumn(#"Removed Columns", "Custom", each if [MinDate] = null then "" else if [Admission Date] < [MinDate] then "RemoveRow" else null),
    #"Filtered Rows1" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = null)),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows1",{"Custom", "MinDate"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Patient.1", "Patient"}})
in
    #"Renamed Columns"

 

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

1 REPLY 1
Greg_Deckler
Super User
Super User

@slittleson Well, you could create a separate query where you first filter for Yes. Then you would group by patient and do an aggregation of Minimum of Admission. You could then merge this back into your other query and expand the minimum date found for each patient. Then you could create a calculated column as to whether the date is less than this minimum date and if so flag it. Then filter out the flags.

 

 

let
    Source = Table,
    #"Filtered Rows" = Table.SelectRows(Source, each ([Event Happened] = "Yes")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Patient"}, {{"MinDate", each List.Min([Admission Date]), type nullable date}}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows", {"Patient"}, Table, {"Patient"}, "Table", JoinKind.RightOuter),
    #"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table", {"Patient", "Admission Date", "Event Happened", "Calculated Outcome"}, {"Patient.1", "Admission Date", "Event Happened", "Calculated Outcome"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Table",{"Patient"}),
    #"Added Conditional Column" = Table.AddColumn(#"Removed Columns", "Custom", each if [MinDate] = null then "" else if [Admission Date] < [MinDate] then "RemoveRow" else null),
    #"Filtered Rows1" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = null)),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows1",{"Custom", "MinDate"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Patient.1", "Patient"}})
in
    #"Renamed Columns"

 

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors