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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
ajlindquist
Regular Visitor

Single Group By Step With Multiple Criteria In Power Query

I have a table that I need to group by a single field, but I need to return multiple columns with different criteria. My dataset looks like this:

 

JobOperationDescriptionComplete
ABC10First OperationTRUE
ABC20Second OperationTRUE
ABC30Third OperationFALSE
ABC40Fourth OperationFALSE
XYZ10First OperationFALSE
XYZ20Second OperationFALSE
XYZ30Third OperationFALSE
XYZ40Fourth OperationFALSE

 

 

I need to group by the Job column, and return 6 columns with the following criteria:

  • First Op = operation with the lowest value
  • First Op Desc = description associated with the operation with the lowest value
  • Last Compl Op = operation with the highest value where "Complete" = TRUE
  • Last Compl Op Desc = the description associated with the operation with the highest value where "Complete" = TRUE
  • Current Op = operation with the lowest value where "Complete" = FALSE
  • Current Op Desc = the description associated with the operation with the lowest value where "Complete" = FALSE

This is the desired output:

 

JobFirst OpFirst Op DescLast Compl OpLast Compl Op DescCurrent OpCurrent Op Desc
ABC10First Operation20Second Operation30Third Operation
XYZ10First Operationnullnull10First Operation

 

In this example, job XYZ has no completed operations so the Last Compl Op and Last Compl Op Desc should return null.

 

Is it possible to do this in a single group by step?

1 ACCEPTED SOLUTION
dufoq3
Community Champion
Community Champion

Hi @ajlindquist,

 

result:

dufoq3_0-1708971958428.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVtJRMjQAEm6ZRcUlCv4FqUWJJZn5eUCRkKBQV6VYHZgqI5Cq4NTk/LwUfMqMQcpCMjKLUFW5OfoEIyszAduZX1pUkoFDXURkFG63oSvD5Th0dQRdB1FGwHWxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Job = _t, Operation = _t, Description = _t, Complete = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Operation", type number}, {"Complete", type logical}}),
    GroupedRows = Table.Group(ChangedType, {"Job"}, {
      { "First Op", each Table.First(Table.Sort(_, {"Operation", Order.Ascending}))[Operation], type nullable number },
      { "First Op Desc", each Table.First(Table.Sort(_, {"Operation", Order.Ascending}))[Description], type text },
      { "Last Compl Op", each Record.FieldOrDefault(Table.Last(Table.SelectRows(Table.Sort(_, {"Operation", Order.Ascending}), (x)=> x[Complete] = true )), "Operation", null), type nullable number },
      { "Last Compl Op Desc", each Record.FieldOrDefault(Table.Last(Table.SelectRows(Table.Sort(_, {"Operation", Order.Ascending}), (x)=> x[Complete] = false )), "Description", null), type text },
      { "Current Op", each Record.FieldOrDefault(Table.First(Table.SelectRows(Table.Sort(_, {"Operation", Order.Ascending}), (x)=> x[Complete] = false )), "Operation", null), type nullable number },
      { "Current Op Desc", each Record.FieldOrDefault(Table.First(Table.SelectRows(Table.Sort(_, {"Operation", Order.Ascending}), (x)=> x[Complete] = false )), "Description", null), type text }
})
in
    GroupedRows

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

1 REPLY 1
dufoq3
Community Champion
Community Champion

Hi @ajlindquist,

 

result:

dufoq3_0-1708971958428.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVtJRMjQAEm6ZRcUlCv4FqUWJJZn5eUCRkKBQV6VYHZgqI5Cq4NTk/LwUfMqMQcpCMjKLUFW5OfoEIyszAduZX1pUkoFDXURkFG63oSvD5Th0dQRdB1FGwHWxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Job = _t, Operation = _t, Description = _t, Complete = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Operation", type number}, {"Complete", type logical}}),
    GroupedRows = Table.Group(ChangedType, {"Job"}, {
      { "First Op", each Table.First(Table.Sort(_, {"Operation", Order.Ascending}))[Operation], type nullable number },
      { "First Op Desc", each Table.First(Table.Sort(_, {"Operation", Order.Ascending}))[Description], type text },
      { "Last Compl Op", each Record.FieldOrDefault(Table.Last(Table.SelectRows(Table.Sort(_, {"Operation", Order.Ascending}), (x)=> x[Complete] = true )), "Operation", null), type nullable number },
      { "Last Compl Op Desc", each Record.FieldOrDefault(Table.Last(Table.SelectRows(Table.Sort(_, {"Operation", Order.Ascending}), (x)=> x[Complete] = false )), "Description", null), type text },
      { "Current Op", each Record.FieldOrDefault(Table.First(Table.SelectRows(Table.Sort(_, {"Operation", Order.Ascending}), (x)=> x[Complete] = false )), "Operation", null), type nullable number },
      { "Current Op Desc", each Record.FieldOrDefault(Table.First(Table.SelectRows(Table.Sort(_, {"Operation", Order.Ascending}), (x)=> x[Complete] = false )), "Description", null), type text }
})
in
    GroupedRows

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.