Forum Discussion

lipesouza12's avatar
lipesouza12
Frequent Visitor
4 years ago
Solved

Group By with CountIf and SumIf

Hello everybody, I have a question that may have a very simple solution, but I haven't found it yet. I need to group a 300k rows table and perform conditional calculations as below.   Product ...
  • Vijay_A_Verma's avatar
    4 years ago

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzVUwVNJR8nWM1DUyAjKMDMCUsYGBUqwOVIERFgWGxhZmembmCEXGQEGvUD9UU4yN9YyNEWoMsagxtDTAr8DIAEOBD6oCUyQFJlgUmCCbYAoUcAx1R7PC2FTPzATVGkxFQFNiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Month = _t, Year = _t, Total = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Month", type date}, {"Year", Int64.Type}, {"Total", type number}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Year"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Product]), "Product", "Total", List.Sum),
        Custom1 = Table.ReplaceValue(#"Pivoted Column",each [Month],each Text.Upper(Date.ToText([Month],"MMM-yyyy")),Replacer.ReplaceValue,{"Month"})
    in
        Custom1