excelexcel-formulalambdagroup-bypivot

Excel Lambdas (GROUPBY and PIVOTBY) - Providing a vector of lambdas in function arguments


In the new functions GroupBy and PivotBy the lambda function argument is described as follows:

However, how does one provide a vector of lambda?

The naive way of writing Groupby(...., {SUM, AVERAGE}, ...) does not provide a possible solution.

Example data:

Col1    Col2    Vals
A   A   1
A   A   2
B   A   3
B   A   4
A   B   5
A   B   6
B   B   7
B   B   8
A   C   9
B   C   10
B   C   11

Formula for 1 lambda =GROUPBY(A1:B12,C1:C12,SUM,3), formula for multiple lambda (does not work) =GROUPBY(A1:B12,C1:C12,{SUM,AVERAGE},3)


Solution

  • You need to apply the functions and aggregations within GROUPBY() or PIVOTBY() in the following manner in order to get the desired output:

    enter image description here


    =GROUPBY(A2:B12,C2:C12,HSTACK(SUM,AVERAGE),0,0)
    

    You cannot use curly braces to apply the multiple aggregations, instead you can use the HSTACK() to embed the aggregations you need to apply. also to drop the header can use the DROP() function:

    =DROP(GROUPBY(A2:B12,C2:C12,HSTACK(SUM,AVERAGE),,0),1)
    

    Also, refer this post multiple use cases of GROUPBY() :

    Sum of Article Numbers with Excel Power Query

    enter image description here