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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Fro88er
Helper IV
Helper IV

Power Query is latest flag on multiple criteria

I am trying to identify the MIN record dateBegin AND appointmentID for each jobOrderID in power query.  I was able to do this as a calculated column using allExcept and works great however I need it in the dataset (power query). Below are two examples JO 324942 min dateBegin it's the apt 889245 this is the earliest record.  Example JO 324967 there are multiple records with the same dateBegin and therefore the MIN appointmentID comes into play (because they are sequential) the apt 889265 is the record to flag. I tried grouping, list.Min, etc. in an If statement to get the below result without success.

 

appointmentIDdateBeginjobOrderIDFlag (result I am looking for)
8892441/2/2019 9:00:00 AM324942 
8892451/2/2019 8:30:00 AM3249421
8892461/2/2019 9:30:00 AM324942 
8892471/2/2019 10:00:00 AM324942 
8892481/2/2019 10:30:00 AM324942 
8892501/2/2019 11:15:00 AM324942 
8892531/2/2019 12:00:00 PM324942 
9253102/8/2019 3:30:00 PM324942 
8892651/2/2019 10:45:00 AM3249671
8892661/2/2019 10:45:00 AM324967 
8892701/2/2019 10:45:00 AM324967 
8892721/2/2019 10:45:00 AM324967 
8941711/8/2019 3:45:00 PM324967 
9070721/22/2019 3:45:00 PM324967 
9127971/28/2019 5:15:00 PM324967 
9474643/6/2019 3:45:00 PM324967 
9515413/11/2019 4:30:00 PM324967 
9679693/28/2019 4:30:00 PM324967 
2 ACCEPTED SOLUTIONS
v-yanjiang-msft
Community Support
Community Support

Hi @Fro88er ,

According to your description, here's my solution.

Add a custom column.

 

if [dateBegin]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[jobOrderID]=[jobOrderID])[dateBegin]) and [appointmentID]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[dateBegin]=[dateBegin])[appointmentID]) then 1 else ""

Get the result:

vkalyjmsft_0-1665023029505.png

Here's the whole M syntax:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdJLDsMgDATQq1SsI2EbY+PseoBK2Ue5/zUaEtIG2nwkVuhpxsiMo0vJiNl1Dj15ArSH9QDzeTxf820gNiY3dUXGvUx9OJZSZ55I3UuEs/rU0OPUCBXFHuMhDRWlMsDQ0Awxp5JPKw2lv5U5VGIzKtf9ol8qt6nCfUr3qDEqLvTzqlUOjTRQ2ELpgiKplbWW1FgW8ENZWfL/C14uQiNGxkUirpTbBWxU1MQWuvX/pdMb", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [appointmentID = _t, dateBegin = _t, jobOrderID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"appointmentID", Int64.Type}, {"dateBegin", type datetime}, {"jobOrderID", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Flag", each if [dateBegin]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[jobOrderID]=[jobOrderID])[dateBegin]) and [appointmentID]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[dateBegin]=[dateBegin])[appointmentID]) then 1 else "")
in
    #"Added Custom"

I also attach my sample below for your reference.

 

 

Best Regards,
Community Support Team _ kalyj

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

View solution in original post

Hi @Fro88er ,

You can try to add a condition in the code.

if [dateBegin]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[jobOrderID]=[jobOrderID])[dateBegin]) and [appointmentID]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[dateBegin]=[dateBegin])[appointmentID])and [type]="interview" then 1 else ""

Best Regards,
Community Support Team _ kalyj

View solution in original post

5 REPLIES 5
Fro88er
Helper IV
Helper IV

You are amazing!  Thank you soo much for this assistance, and quick response.  

v-yanjiang-msft
Community Support
Community Support

Hi @Fro88er ,

According to your description, here's my solution.

Add a custom column.

 

if [dateBegin]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[jobOrderID]=[jobOrderID])[dateBegin]) and [appointmentID]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[dateBegin]=[dateBegin])[appointmentID]) then 1 else ""

Get the result:

vkalyjmsft_0-1665023029505.png

Here's the whole M syntax:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdJLDsMgDATQq1SsI2EbY+PseoBK2Ue5/zUaEtIG2nwkVuhpxsiMo0vJiNl1Dj15ArSH9QDzeTxf820gNiY3dUXGvUx9OJZSZ55I3UuEs/rU0OPUCBXFHuMhDRWlMsDQ0Awxp5JPKw2lv5U5VGIzKtf9ol8qt6nCfUr3qDEqLvTzqlUOjTRQ2ELpgiKplbWW1FgW8ENZWfL/C14uQiNGxkUirpTbBWxU1MQWuvX/pdMb", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [appointmentID = _t, dateBegin = _t, jobOrderID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"appointmentID", Int64.Type}, {"dateBegin", type datetime}, {"jobOrderID", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Flag", each if [dateBegin]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[jobOrderID]=[jobOrderID])[dateBegin]) and [appointmentID]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[dateBegin]=[dateBegin])[appointmentID]) then 1 else "")
in
    #"Added Custom"

I also attach my sample below for your reference.

 

 

Best Regards,
Community Support Team _ kalyj

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

This is amazing, I was now aware I can run variables in M-code (mind blown)!! One last favor, if there was an additional column called [type] with records like "interview", 2nd interview" ect.  and I only wanted your logic with [type] = "interview" how / where do I also add that filter logic?

Hi @Fro88er ,

You can try to add a condition in the code.

if [dateBegin]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[jobOrderID]=[jobOrderID])[dateBegin]) and [appointmentID]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[dateBegin]=[dateBegin])[appointmentID])and [type]="interview" then 1 else ""

Best Regards,
Community Support Team _ kalyj

[FollowUp] works like a charm except I can't load this additional column you created.  When I close and load, it takes forever, and must abandon it.  Thoughts?

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors