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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

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
Super User
Super User

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
Super User
Super User

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors