Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

An explicit or eta reduced lambda (SUM, PERCENTOF, AVERAGE, COUNT, etc) that is used to aggregate values.

A vector of lambdas can be provided. If so, the output will have multiple aggregations.
The orientation of the vector will determine whether they are laid out row- or column-wise.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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


Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading