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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
MountainDweller
New Member

list of dates in a field, how to filter the list to exclude dates before date in another field

I have a list of dates stored in a field and I want to add a new column that contains a subset of the list filtered to exclude dates before the date storred in a third column.

 

The following formula produces an error:

= Table.AddColumn(#"Expanded CR (Step1)", "FutureReferralDates", each List.Select([ReferralDateList], each _ > [Referral Date]))

 

It works if I replace [Referral Date] with a date literal. However, editor offers [Referral Date] as an available field to reference. 

 

What's wrong with the formula? Any suggestions for the best method to achieve this?

 

Result is error instead of  the list {2023-07-04}Result is error instead of the list {2023-07-04}The source column contains {2020-11-04, 2023-07-04} and the criteria column contains the date 2023-07-04The source column contains {2020-11-04, 2023-07-04} and the criteria column contains the date 2023-07-04

1 ACCEPTED SOLUTION
Rickmaurinus
Helper V
Helper V

Hey Mountaindweller,

 

The code is a little hard to test without source dataset. But I think your issue lies in this step: 

 

= Table.AddColumn(#"Expanded CR (Step1)", "FutureReferralDates", each  List.Select( [ReferralDateList],each _ > [Referral Date] ) )

The each statement of List.Select calls a new context, so the scope of [Referral Date] (that lies outside of this context) can't be reached. What you can do is make a variable as follows: 

 

= Table.AddColumn(#"Expanded CR (Step1)", "FutureReferralDates", each let RefDate = [Referral Date] in List.Select( [ReferralDateList],each _ > RefDate ) )

 

Then this particular step will give you a result. I can't test the query, but please do let me know if this solves your issue. 🙏

 

--------------------------------------------------

@ me in replies or I'll lose your thread

 

Master Power Query M? -> https://powerquery.how

Read in-depth articles? -> BI Gorilla

Youtube Channel: BI Gorilla

 

If this post helps, then please consider accepting it as the solution to help other members find it more quickly.

View solution in original post

3 REPLIES 3
Rickmaurinus
Helper V
Helper V

Hey Mountaindweller,

 

The code is a little hard to test without source dataset. But I think your issue lies in this step: 

 

= Table.AddColumn(#"Expanded CR (Step1)", "FutureReferralDates", each  List.Select( [ReferralDateList],each _ > [Referral Date] ) )

The each statement of List.Select calls a new context, so the scope of [Referral Date] (that lies outside of this context) can't be reached. What you can do is make a variable as follows: 

 

= Table.AddColumn(#"Expanded CR (Step1)", "FutureReferralDates", each let RefDate = [Referral Date] in List.Select( [ReferralDateList],each _ > RefDate ) )

 

Then this particular step will give you a result. I can't test the query, but please do let me know if this solves your issue. 🙏

 

--------------------------------------------------

@ me in replies or I'll lose your thread

 

Master Power Query M? -> https://powerquery.how

Read in-depth articles? -> BI Gorilla

Youtube Channel: BI Gorilla

 

If this post helps, then please consider accepting it as the solution to help other members find it more quickly.

Thank you Rick! That works perfectly.

MountainDweller
New Member

Here is the full code.

 

let
Source = #"CR (Step1)",
#"Removed Other Columns" = Table.SelectColumns(Source,{"Patient #", "Referral Date"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{"Referral Date", "OtherReferralDate"}}),
TypeChange = Table.TransformColumnTypes(#"Renamed Columns",{{"OtherReferralDate", type date}}),
Group = Table.Group(TypeChange, {"Patient #"}, {{"ReferralDatesTable", each _, type table [#"Patient #"=nullable number, OtherReferralDate=nullable date]}}),
#"Added ReferralDateList" = Table.AddColumn(Group, "ReferralDateList", each Table.Column([ReferralDatesTable],"OtherReferralDate")),
#"Removed Other Columns1" = Table.SelectColumns(#"Added ReferralDateList",{"Patient #", "ReferralDateList"}),
#"Merged Queries" = Table.NestedJoin(#"Removed Other Columns1", {"Patient #"}, #"CR (Step1)", {"Patient #"}, "CR (Step1)", JoinKind.RightOuter),
#"Expanded CR (Step1)" = Table.ExpandTableColumn(#"Merged Queries", "CR (Step1)", {"First Name", "Surname", "ReferringDoctor", "Referral Date", "Referral Urgency", "Referral Reason", "Referral Details", "Pat Md", "Status", "StatusComments", "Next Appt Date", "Next Appt Prvdr", "Extract Date", "Health Number", "CR Occurrence Date", "(on form) Decision", "RequestedMD", "RqstdConsultType", "RqstdConsult", "(on form) Track", "FormType", "Patient Name", "DateEDC", "BookingInstruction", "BookingPlan", "CRKey", "Decision", "Track"}, {"First Name", "Surname", "ReferringDoctor", "Referral Date", "Referral Urgency", "Referral Reason", "Referral Details", "Pat Md", "Status", "StatusComments", "Next Appt Date", "Next Appt Prvdr", "Extract Date", "Health Number", "CR Occurrence Date", "(on form) Decision", "RequestedMD", "RqstdConsultType", "RqstdConsult", "(on form) Track", "FormType", "Patient Name", "DateEDC", "BookingInstruction", "BookingPlan", "CRKey", "Decision", "Track"}),
#"Added FutureReferralDates" = Table.AddColumn(#"Expanded CR (Step1)", "FutureReferralDates", each List.Select([ReferralDateList],each _ > [Referral Date])),
#"Reordered Columns" = Table.ReorderColumns(#"Added FutureReferralDates",{"Patient #", "First Name", "Surname", "ReferringDoctor", "Referral Urgency", "Referral Reason", "Referral Details", "Pat Md", "Status", "StatusComments", "Next Appt Date", "Next Appt Prvdr", "Extract Date", "Health Number", "CR Occurrence Date", "(on form) Decision", "RequestedMD", "RqstdConsultType", "RqstdConsult", "(on form) Track", "FormType", "Patient Name", "DateEDC", "BookingInstruction", "BookingPlan", "CRKey", "Decision", "Track", "ReferralDateList", "Referral Date", "FutureReferralDates"})
in
#"Reordered Columns"

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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