Search code examples
google-sheetslambdameanmediangoogle-query-language

Query with Median and Group by and where google sheets


I need to get the grouped median

I have grouped data of the form

From    type                 Weight
A       person-person        4
A       person-person        3
A       person-organization  11
A       person-person        5
A       person-organization  6
B       person-person        2
B       person-organization  3
B       person-organization  7
C       person-person        5
C       person-person        2
C       person-organization  15
S       person-organization  7
S       person-person        4
S       person-person        3

I need to get the Median grouped by col A where Col B = 'person-person'

A   value1
B   value2
C   value3
S   value4

I can do this for Average with:

=QUERY(Connections!A:C,"Select A, Avg(C) where B='person-person' and C is not null group by A",1)

I have tried using the Median function but I can not get it to group by column A

Google sheet https://docs.google.com/spreadsheets/d/1ZPia3LkVg2Pt4YGIH4KtW49G2uElRFcvWLY6LYOQ-Ss/edit?usp=sharing

Is it possible to get the median is this way?

Thanks


Solution

  • unfortunatelly, in Google Sheets the QUERY (nor ARRAYFORMULA) function does not support median at all.


    draggable solution:

    =MEDIAN(FILTER(C:C, A:A=H2))
    

    enter image description here


    UPDATE:

    it's now possible:

    =BYROW(H2:H7, LAMBDA(xx, MEDIAN(FILTER(C:C, A:A=xx))))
    

    enter image description here

    or even like:

    =BYROW(UNIQUE(FILTER(A:A, B:B="person-person")), LAMBDA(xx, MEDIAN(FILTER(C:C, A:A=xx))))
    

    enter image description here