How to SORT (and AVERAGE) the FILTER output of one column based on data from another column?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
578
Office Version
  1. 365
Platform
  1. Windows
Hello,

I need to generate output for drug_level for each subject_ID in the attached example XL2BB, such that the drug_level numbers are sorted based on the increasing number of days from the Day column. Also if there are more than one day with the same number, I want the drug levels to be averaged for that day. Also since the generated outputs will be used for graphing purposes, I want to exclude any blanks form the filtered outputs so that the chart lines don't collapse.

Book1
ABCDEFGHIJKLMNOPQ
1Subject_IDDayDrug_levelExample FILTER:A110005006547321033297345289672453
2A171000
3A15500
4A21760day245789
5B33892Intended output:A1558.33289467.51000693672
6A18654(avg.)(avg.)(avg.)
7A18732
8B246122346
9C111024Intended output:B24531063.5612655
10A121033(avg.)
11A12297
12A12345
13A14289
14D27456
15A13
16A19672
17A15453
18A18
19B22453
20C47781
21B231232
22B23895
23C19367
24B26655
25B23
26c17652
27
Sheet1
Cell Formulas
RangeFormula
G1:P1G1=TRANSPOSE(FILTER(C2:C18,(A2:A18=F1)*(C2:C18<>"")))
Dynamic array formulas.


I have included an example for FILTER for subject A1 in cell G1, but I wasn't sure how to sort the numbers based on increasing days and how to average those days that have more than one number.

Also I have included examples for intended output in G5 and G9. They shows that some days with more than one number have averaged numbers and some days are skipped from output because there are no numbers for it.

Thanks for any input!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Could just put it in a pivot table.

But there is also a couple of formulas there that will do what you are asking.

Book3
ABCDEFGHIJKLMNO
1Subject_IDDayDrug_levelSubject_IDDayAverage of Drug_level
2A171000A1597.50A1245789
3A155002558.33558.33289476.51000693672
4A217604289.00
5B338925476.50
6A1865471,000.00
7A187328693.00
8B246129672.00
9C111024A2760.00
10A1210331760.00
11A12297B2769.40
12A123452453.00
13A1428931,063.50
14D274564612.00
15A136655.00
16A19672B3892.00
17A154533892.00
18A18C1681.00
19B2245311,024.00
20C477817652.00
21B2312329367.00
22B23895C4781.00
23C193677781.00
24B26655D2456.00
25B237456.00
26c17652Grand Total670.64
Sheet2
Cell Formulas
RangeFormula
J2:O2J2=TRANSPOSE(SORT(UNIQUE(FILTER(Table4[Day],(Table4[Subject_ID]=I2)*(Table4[Drug_level]>0)))))
J3:O3J3=AVERAGEIFS(Table4[Drug_level],Table4[Subject_ID],I2,Table4[Day],J2#)
Dynamic array formulas.
 
Upvote 0
This is fabulous, thank you! Just to make sure, how did you generate the second table (columns E,F,G)? I was trying to that and couldn't figure out how to. (But I did press Control+T on my original table which generated your first table).
 
Upvote 0
After you've made the table, click on the 'Insert' tab and click on 'PivotTable'. Then drag 'subject_ID' and 'Drug_level' to the 'Rows' area and drag 'Drug_level' to the 'Values' area. After that, click on the arrow of what you just dragged in and select 'Value Field Settings' and select to summarize the field by average instead of sum.

Then you can use that to create a chart.
 
Upvote 0
This is so cool. I'm having a little difficulty replicating your table. After I did the steps you mentioned, I get this (only 2 columns; I cannot make the days columns appear no matter what I do):

Book1
ABC
2
3Row LabelsAverage of Drug_level
4A1597.5
5289289
6297297
7345345
8453453
9500500
10654654
11672672
12732732
1310001000
1410331033
15(blank)
16A2760
17760760
18B2769.4
19453453
20612612
21655655
22895895
2312321232
24(blank)
25B3892
26892892
27C1681
28367367
29652652
3010241024
31C4781
32781781
33D2456
34456456
35Grand Total670.6363636
36
Sheet8



😭 😭 😭 Am I doing anything wrong?
 
Upvote 0
It looks like you don't have days in there. You should have 2 fields dragged into the rows area.
 
Upvote 0
I see. I was going based on your instructions here:

After you've made the table, click on the 'Insert' tab and click on 'PivotTable'. Then drag 'subject_ID' and 'Drug_level' to the 'Rows' area and drag 'Drug_level' to the 'Values' area. After that, click on the arrow of what you just dragged in and select 'Value Field Settings' and select to summarize the field by average instead of sum.

You didn't mention about the "days". I've been trying this multiple times in various ways, but still only get two columns. When I drag "Day" it still becomes a total of two columns and the days go within the subject_ID.

If you could walk me through this one more time, that would be fabulous. I'm probably missing some simple step. Thank you! 🤗
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top