Forum Discussion

RAKESH1986's avatar
RAKESH1986
Helper II
3 years ago
Solved

How to group values in two groups-power query

Hi all,

 

i have these value i column, and i want to group these values in two group and take sum of another column values.

 

 

 

 

i want to group all red highligted in to one group and project management in another group

 

discipline, early_aw_wk, aw_early_baseline

Group 1, aw01, sum of values

project management, aw01, sum of values

Group 1, aw02, sum of values

project management, aw02, sum of values

Group 1, aw03, sum of values

project management, aw03, sum of values

 

could u tell me simplest way to achive this?

 

thanks

rakesh

 

 

 

  • RAKESH1986 

    See it all at work in the attached file. Place the following 3 pieces of M code in a blank query each to see the steps

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZTdbsMgDIVfBUXaXTUB2U9vEfJapgAVOK2mro+x919RUBtTQnOVi/PJxzbHOZ+7Q/AaYuw2nTpxcf3wVy5fusumkOQkvVWkfpI+s2QOMBgH1YpEyyX7mpZrvt/svkEjs8qpHVhwSKrzNjX5CNGmJsdt7saC3itntBpYRIVGE0PRhCSZeQHqyfAzKPhEud0TQ4rJddV68ogIA2hv7ZhQNN7VLBcgargAUTvjIoYxLfzRrG8gksSrikxGIucgNYMhjV/zKFVJklaqeYQtiT2L6gvwp7auGlHkvELQRV0voXx/+ajQvucKvUdtjmZgv3+cyw+WdqdxDMVq5AqU+jVR2sAsjfujenZId2TxjO4I3ZtVCMGoW1vah+BjGTW5Ci4Oqg3PflaXfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Discipline = _t, EARLY_AW_WK = _t, AW_EARLY_BASELINE = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Discipline", type text}, {"EARLY_AW_WK", type text}, {"AW_EARLY_BASELINE", Percentage.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Discipline] = "PROJECT MANAGEMENT")),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Discipline", "EARLY_AW_WK"}, {{"AW_EARLY_BASELINE", each List.Sum([AW_EARLY_BASELINE]), type nullable number}})
    in
        #"Grouped Rows"

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZTdbsMgDIVfBUXaXTUB2U9vEfJapgAVOK2mro+x919RUBtTQnOVi/PJxzbHOZ+7Q/AaYuw2nTpxcf3wVy5fusumkOQkvVWkfpI+s2QOMBgH1YpEyyX7mpZrvt/svkEjs8qpHVhwSKrzNjX5CNGmJsdt7saC3itntBpYRIVGE0PRhCSZeQHqyfAzKPhEud0TQ4rJddV68ogIA2hv7ZhQNN7VLBcgargAUTvjIoYxLfzRrG8gksSrikxGIucgNYMhjV/zKFVJklaqeYQtiT2L6gvwp7auGlHkvELQRV0voXx/+ajQvucKvUdtjmZgv3+cyw+WdqdxDMVq5AqU+jVR2sAsjfujenZId2TxjO4I3ZtVCMGoW1vah+BjGTW5Ci4Oqg3PflaXfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Discipline = _t, EARLY_AW_WK = _t, AW_EARLY_BASELINE = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Discipline", type text}, {"EARLY_AW_WK", type text}, {"AW_EARLY_BASELINE", Percentage.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Discipline] <> "PROJECT MANAGEMENT")),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"EARLY_AW_WK"}, {{"AW_EARLY_BASELINE", each List.Sum([AW_EARLY_BASELINE]), type nullable number}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Discipline", each "Group 1"),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Discipline", "EARLY_AW_WK", "AW_EARLY_BASELINE"})
    in
        #"Reordered Columns"

     

     

     

    let
        Source = Table.Combine({T1, T2}),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Discipline", type text}, {"AW_EARLY_BASELINE", Percentage.Type}})
    in
        #"Changed Type"

     

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

     

3 Replies

  • AlB's avatar
    AlB
    Community Champion

    RAKESH1986 

    See it all at work in the attached file. Place the following 3 pieces of M code in a blank query each to see the steps

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZTdbsMgDIVfBUXaXTUB2U9vEfJapgAVOK2mro+x919RUBtTQnOVi/PJxzbHOZ+7Q/AaYuw2nTpxcf3wVy5fusumkOQkvVWkfpI+s2QOMBgH1YpEyyX7mpZrvt/svkEjs8qpHVhwSKrzNjX5CNGmJsdt7saC3itntBpYRIVGE0PRhCSZeQHqyfAzKPhEud0TQ4rJddV68ogIA2hv7ZhQNN7VLBcgargAUTvjIoYxLfzRrG8gksSrikxGIucgNYMhjV/zKFVJklaqeYQtiT2L6gvwp7auGlHkvELQRV0voXx/+ajQvucKvUdtjmZgv3+cyw+WdqdxDMVq5AqU+jVR2sAsjfujenZId2TxjO4I3ZtVCMGoW1vah+BjGTW5Ci4Oqg3PflaXfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Discipline = _t, EARLY_AW_WK = _t, AW_EARLY_BASELINE = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Discipline", type text}, {"EARLY_AW_WK", type text}, {"AW_EARLY_BASELINE", Percentage.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Discipline] = "PROJECT MANAGEMENT")),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Discipline", "EARLY_AW_WK"}, {{"AW_EARLY_BASELINE", each List.Sum([AW_EARLY_BASELINE]), type nullable number}})
    in
        #"Grouped Rows"

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZTdbsMgDIVfBUXaXTUB2U9vEfJapgAVOK2mro+x919RUBtTQnOVi/PJxzbHOZ+7Q/AaYuw2nTpxcf3wVy5fusumkOQkvVWkfpI+s2QOMBgH1YpEyyX7mpZrvt/svkEjs8qpHVhwSKrzNjX5CNGmJsdt7saC3itntBpYRIVGE0PRhCSZeQHqyfAzKPhEud0TQ4rJddV68ogIA2hv7ZhQNN7VLBcgargAUTvjIoYxLfzRrG8gksSrikxGIucgNYMhjV/zKFVJklaqeYQtiT2L6gvwp7auGlHkvELQRV0voXx/+ajQvucKvUdtjmZgv3+cyw+WdqdxDMVq5AqU+jVR2sAsjfujenZId2TxjO4I3ZtVCMGoW1vah+BjGTW5Ci4Oqg3PflaXfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Discipline = _t, EARLY_AW_WK = _t, AW_EARLY_BASELINE = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Discipline", type text}, {"EARLY_AW_WK", type text}, {"AW_EARLY_BASELINE", Percentage.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Discipline] <> "PROJECT MANAGEMENT")),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"EARLY_AW_WK"}, {{"AW_EARLY_BASELINE", each List.Sum([AW_EARLY_BASELINE]), type nullable number}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Discipline", each "Group 1"),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Discipline", "EARLY_AW_WK", "AW_EARLY_BASELINE"})
    in
        #"Reordered Columns"

     

     

     

    let
        Source = Table.Combine({T1, T2}),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Discipline", type text}, {"AW_EARLY_BASELINE", Percentage.Type}})
    in
        #"Changed Type"

     

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

     

  • AlB's avatar
    AlB
    Community Champion

    Hi RAKESH1986 

    Just split the initial table in two. On one you keep Project Management, on the other one all the rest. Then, for both tables, group by discipline and early_aw_wk, adding the results.

    Finally, just append both tables to create the final output

    If you can share a sample of the data in text-tabular format, so that the contents can be copied, I can build the query but it should be straightforward with the steps above

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

     

  • AlB  thanks for response. this data i have in table-

    DisciplineEARLY_AW_WKAW_EARLY_BASELINE
    PROCESSAW010.02%
    PROCESSAW020.04%
    PROCESSAW030.07%
    PIPELINEAW010.02%
    PIPELINEAW020.03%
    PIPELINEAW030.05%
    PROJECT MANAGEMENTAW010.00%
    PROJECT MANAGEMENTAW020.11%
    PROJECT MANAGEMENTAW030.83%
    MECHANICAL STATICAW010.01%
    MECHANICAL STATICAW020.02%
    MECHANICAL STATICAW030.03%
    MECHANICAL ROTATINGAW010.01%
    MECHANICAL ROTATINGAW020.03%
    MECHANICAL ROTATINGAW030.04%
    TELECOMMUNICATIONAW010.01%
    TELECOMMUNICATIONAW020.03%
    TELECOMMUNICATIONAW030.04%
    INSTRUMENTATIONAW010.03%
    INSTRUMENTATIONAW020.07%
    INSTRUMENTATIONAW030.10%
    ELECTRICALAW010.03%
    ELECTRICALAW020.05%
    ELECTRICALAW030.08%
    PROCESS SAFETYAW010.01%
    PROCESS SAFETYAW020.03%
    PROCESS SAFETYAW030.04%
    PIPINGAW010.02%
    PIPINGAW020.05%
    PIPINGAW030.07%
    CIVIL & STRUCTURALAW010.02%
    CIVIL & STRUCTURALAW020.05%
    CIVIL & STRUCTURALAW030.07%
    MECHANICAL HVACAW010.01%
    MECHANICAL HVACAW020.02%
    MECHANICAL HVACAW030.04%
    MATERIAL & CORROSIONAW010.02%
    MATERIAL & CORROSIONAW020.03%
    MATERIAL & CORROSIONAW030.05%