arraysexceloffsetmedian

Excel: how to calculate average and median for data from each quartile, where data should match specific criteria


Question seems simple but I can not get working formula, already desperate. I have to calculate gender pay gap. And for it need following: Imagine column B =gender of employees (male/female), colum C - total remuneration. Need to calculate average and median total remuneration of only male employees in quartile 1 of data. (And then in Q2, Q3, Q4). By googling I have found some formulas that contain References to values of Q1, Q2, Q3, Q4 - it's not correct statistically, as, e.g.employees from Q2 can have same total remuneration as employees in Q1...


Solution

  • Here is a possible formula for Males Q1 assuming you have Let, Filter and Sequence in your version of Excel:

    =LET(seq,SEQUENCE(ROWS(Table1[#Data])),malesQ1,FILTER(Table1[Pay],(seq<=5)*(Table1[Gender]="M")),AVERAGE(malesQ1))
    

    enter image description here

    I will add to my answer later and try to add corrections for when the number of employees is not a multiple of 4 and (more difficult) when the same pay rate goes across quartile boundaries.


    Here is a more general formula which will pick out males or females from a given quartile where the number of cases needn't be a multiple of 4 so average or median can be calculated:

    =LET(Q,3,gender,"F",
    rows,ROWS(Table2[#Data]),
    seq,SEQUENCE(rows),
    quarter,QUOTIENT(rows,4),
    cupper,MIN(MOD(rows,4),Q),
    upperLim,Q*quarter+cupper,
    clower,MIN(MOD(rows,4),Q-1),
    lowerLim,(Q-1)*quarter+clower+1,
    FILTER(Table2[Pay],(seq>=lowerLim)*(seq<=upperLim)*(Table2[Gender]=gender)))
    

    e.g. for n=23 Q3 females

    enter image description here