Forum Discussion

cottrera's avatar
cottrera
Post Prodigy
3 years ago
Solved

Power Query group to show only MAX REF

Hi   I have a table which contains mainly sinlge rows per unit reference. However some unit references have more than one row.

Each entry has a SAP Ref . So I would like a function in power query to only show the Max SAP Ref for each row. This will remove the duplicate unit references that I do not require.

 

These are the Unit reference that I am expecting to see only one result for any the max ax SAP Ref they are unit refereneces 26, 84 & 133

 

Unit ReferenceSurvey DateSAP RatingSAP RefSAP_GroupingSAP_Grouping2
1801/04/2010551DCloned Apr 10
2601/04/2010541ECloned Apr 10
2601/04/2010603DCloned Apr 10
3427/05/2022693CCurrent
4201/04/2010671DCloned Apr 10
5027/07/2021652DCurrent
7601/04/2010591DCloned Apr 10
8429/04/2009741CExpired
8429/04/2009804CExpired
9201/04/2010671DCloned Apr 10
10901/04/2010581DCloned Apr 10
11718/09/2015612DCurrent
12527/03/2018692CCurrent
13315/04/2021732CCurrent
13315/04/2021993CCurrent
14101/04/2010611DCloned Apr 10
15901/08/2022743CCurrent

 

thank you

 

Richard

  • Hi Richard,

     

    Try this example query:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lVLLCoMwEPwVyVkwu3kfi/UrxJseCsVKaKGf32w00BIj9rABycxkdsa+Z2BZzTg0XDbIgYcPpcIBYa5h2vtjnsbqsvgqXA51z1BnBLkRupMETYcoviBID03DVSAgEsFthJbm5f00PyNUYqZtDt0rvmkb0iacpnUxEb60Tb6oO9S20bdbCZywJiVDvrv3cvPTWIBaekDuQN2/K0LU+zVujxkQFW3DHTEoEA2FVADVlqAgrE3t4E47IKgzUKuTmLYR57Gu1DpIyDKB4w1VysSmfyp2k6kPHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Unit Reference" = _t, #"Survey Date" = _t, #"SAP Rating" = _t, #"SAP Ref" = _t, SAP_Grouping = _t, SAP_Grouping2 = _t]),
        chgTypes = Table.TransformColumnTypes(Source,{{"Unit Reference", Int64.Type}, {"Survey Date", type date}, {"SAP Rating", Int64.Type}, {"SAP Ref", Int64.Type}, {"SAP_Grouping", type text}, {"SAP_Grouping2", type text}}),
    
        groupUnitRef = Table.Group(chgTypes, {"Unit Reference"}, {{"data", each _, type table [Unit Reference=nullable number, Survey Date=nullable date, SAP Rating=nullable number, SAP Ref=nullable number, SAP_Grouping=nullable text, SAP_Grouping2=nullable text]}}),
        addMaxSAPRecord = Table.AddColumn(groupUnitRef, "maxSAP", each Table.Max([data], "SAP Ref")),
        expandMaxSAPRecord = Table.ExpandRecordColumn(addMaxSAPRecord, "maxSAP", {"Survey Date", "SAP Rating", "SAP Ref", "SAP_Grouping", "SAP_Grouping2"}, {"Survey Date", "SAP Rating", "SAP Ref", "SAP_Grouping", "SAP_Grouping2"}),
    
        removeDataCol = Table.RemoveColumns(expandMaxSAPRecord,{"data"})
    in
        removeDataCol

     

    Summary:

    -1- Group By [Unit Reference] and change default aggregated column from Count to 'All Rows'.

    -2- Add a new column using Table.Max to pick out only records that have the MAX value of your chosen column.

    -3- Expand the new nested records column to reinstate all original columns.

     

    Pete

3 Replies

  • Hi Richard,

     

    Try this example query:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lVLLCoMwEPwVyVkwu3kfi/UrxJseCsVKaKGf32w00BIj9rABycxkdsa+Z2BZzTg0XDbIgYcPpcIBYa5h2vtjnsbqsvgqXA51z1BnBLkRupMETYcoviBID03DVSAgEsFthJbm5f00PyNUYqZtDt0rvmkb0iacpnUxEb60Tb6oO9S20bdbCZywJiVDvrv3cvPTWIBaekDuQN2/K0LU+zVujxkQFW3DHTEoEA2FVADVlqAgrE3t4E47IKgzUKuTmLYR57Gu1DpIyDKB4w1VysSmfyp2k6kPHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Unit Reference" = _t, #"Survey Date" = _t, #"SAP Rating" = _t, #"SAP Ref" = _t, SAP_Grouping = _t, SAP_Grouping2 = _t]),
        chgTypes = Table.TransformColumnTypes(Source,{{"Unit Reference", Int64.Type}, {"Survey Date", type date}, {"SAP Rating", Int64.Type}, {"SAP Ref", Int64.Type}, {"SAP_Grouping", type text}, {"SAP_Grouping2", type text}}),
    
        groupUnitRef = Table.Group(chgTypes, {"Unit Reference"}, {{"data", each _, type table [Unit Reference=nullable number, Survey Date=nullable date, SAP Rating=nullable number, SAP Ref=nullable number, SAP_Grouping=nullable text, SAP_Grouping2=nullable text]}}),
        addMaxSAPRecord = Table.AddColumn(groupUnitRef, "maxSAP", each Table.Max([data], "SAP Ref")),
        expandMaxSAPRecord = Table.ExpandRecordColumn(addMaxSAPRecord, "maxSAP", {"Survey Date", "SAP Rating", "SAP Ref", "SAP_Grouping", "SAP_Grouping2"}, {"Survey Date", "SAP Rating", "SAP Ref", "SAP_Grouping", "SAP_Grouping2"}),
    
        removeDataCol = Table.RemoveColumns(expandMaxSAPRecord,{"data"})
    in
        removeDataCol

     

    Summary:

    -1- Group By [Unit Reference] and change default aggregated column from Count to 'All Rows'.

    -2- Add a new column using Table.Max to pick out only records that have the MAX value of your chosen column.

    -3- Expand the new nested records column to reinstate all original columns.

     

    Pete

    • cottrera's avatar
      cottrera
      Post Prodigy

      Thank you for your quick reponse . Works fine

  • ovde's avatar
    ovde
    Resolver II

    You can use the Table.Group function Table.Group - PowerQuery M | Microsoft Learn

     

    Should be something like this, but you should check what you need to keep.

    Table.Group( #table/step, {"Unit Reference", "Survey Date", "SAP_Grouping2"}, {"MaxRef", each List.Max ([SAP])} )