Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Groupby with a Condition

Wow. I didn't think this would be so involved. In Power Query Editor how can I convert the table below grouping Store Managers into two categories, if they failed an audit bucket as "Failed at least 1 Audit", if they have never failed, bucket them as "Passed all Audits":

Starting Table

Store ManagerAudit Violation
233Failed Audit
233Passed Audit
233Passed Audit
238Passed Audit
238Passed Audit
240Failed Audit
249Passed Audit
249Passed Audit
249Passed Audit

 

Resulting End Table:

Store ManagerAudit Failed or Passed
233Failed at least 1 Audit
238Passed all Audits
240Failed at Least 1 Audit
249Passed all Audits

 

Help is much appreciated here. I couldn't believe this was so difficult.

  •  

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjI2VtJRckvMzElNUXAsTcksUYrVgQkHJBYXEydsQYqwiQFWK00ssasmWjgWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Store Manager" = _t, #"Audit Violation" = _t]),
        #"Grouped Rows" = Table.Group(Source, {"Store Manager"}, {{"Result", each let l = [Audit Violation], res = List.Accumulate(l, false, (s,c) => s or Text.Contains(c, "Failed", Comparer.OrdinalIgnoreCase)) in if res then "Failed at least once" else "Passed"}})
    in
        #"Grouped Rows"

     

     

     

     

    For fun only,

1 Reply

  • CNENFRNL's avatar
    CNENFRNL
    Icon for Community Champion rankCommunity Champion

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjI2VtJRckvMzElNUXAsTcksUYrVgQkHJBYXEydsQYqwiQFWK00ssasmWjgWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Store Manager" = _t, #"Audit Violation" = _t]),
        #"Grouped Rows" = Table.Group(Source, {"Store Manager"}, {{"Result", each let l = [Audit Violation], res = List.Accumulate(l, false, (s,c) => s or Text.Contains(c, "Failed", Comparer.OrdinalIgnoreCase)) in if res then "Failed at least once" else "Passed"}})
    in
        #"Grouped Rows"

     

     

     

     

    For fun only,