Forum Discussion

OscarSuarez10's avatar
OscarSuarez10
Icon for Helper III rankHelper III
7 years ago
Solved

Filter by letter and sum by year

Hello, I´m trying to sum the production of some products by year but I can´t sum all the products, I only want to sum some specific names, for example in the following table I only want the production of the products that have the word cheme in their name, as you can see the sum for the first year is 153 and for the sencond year is 1099, How can I do this using power query?

 

Yearproductproduction  
1arbol110cheme year1153
1arbol220  
1arbol35  
1arbol44  
1cheme178  
1cheme252  
1cheme313  
1cheme410  
1arbol59  
1arbol64  
1arbol71  
2arbol178  
2arbol245  
2arbol398  
2arbol412  
2cheme1987cheme year21099
2cheme212  
2cheme345  
2cheme455  
2arbol53  
2arbol611  
2arbol722  
  • Hi OscarSuarez10,

    I've prepared a solution for you. All that you can just click together in the PowerQuery Editor UI. Go through all the steps and if you don't understand one of them, don't hesitate to ask.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY/BCsMgEET/xXMOdXWjfkvIoS1CDgmF/v+hO1LEkdyGh+7M2zbn3eKe39fnRPAPty8DEwsysWBBGUULsaP3Ua+KkDIzXFNhhms+MIs3S9RCYbRSa0MJPxuSUes/REatqMwwpEzv2hDprIuVnBjKzcPAJd1Mp2KYBUYw85MH1MQq9h8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ear = _t, product = _t, production = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ear", Int64.Type}, {"product", type text}, {"production", Int64.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each Text.Contains([product], "cheme")),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"ear"}, {{"Cheme Production", each List.Sum([production]), type number}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Cheme Year", each "cheme year" & Text.From([ear])),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"ear"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Cheme Year", "Cheme Production"})
    in
        #"Reordered Columns"

1 Reply

  • Nolock's avatar
    Nolock
    Icon for Resident Rockstar rankResident Rockstar

    Hi OscarSuarez10,

    I've prepared a solution for you. All that you can just click together in the PowerQuery Editor UI. Go through all the steps and if you don't understand one of them, don't hesitate to ask.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY/BCsMgEET/xXMOdXWjfkvIoS1CDgmF/v+hO1LEkdyGh+7M2zbn3eKe39fnRPAPty8DEwsysWBBGUULsaP3Ua+KkDIzXFNhhms+MIs3S9RCYbRSa0MJPxuSUes/REatqMwwpEzv2hDprIuVnBjKzcPAJd1Mp2KYBUYw85MH1MQq9h8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ear = _t, product = _t, production = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ear", Int64.Type}, {"product", type text}, {"production", Int64.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each Text.Contains([product], "cheme")),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"ear"}, {{"Cheme Production", each List.Sum([production]), type number}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Cheme Year", each "cheme year" & Text.From([ear])),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"ear"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Cheme Year", "Cheme Production"})
    in
        #"Reordered Columns"