Find the maximum value of a group of data then average another data value based upon that max

  • Thread starter Thread starter Legacy 167309
  • Start date Start date
L

Legacy 167309

Guest
Hi.

Completely new to this. Have been using powerpivot I guess at a basic level.

I have data whereby each week I have a list of people that work for certain durations of time each day over the course of the week. Over those durations they travel different distances. I want to compare the aggregate distances covered over a set of weeks. But to do that I want to identify the maximum duration from the week, then average the distance covered by any other person that has a duration within 10% of that maximum duration.


[TABLE="width: 379"]
<tbody>[TR]
[TD][/TD]
[TD]Sum of Duration (Mins)[/TD]
[TD]Sum of Distance (m)[/TD]
[/TR]
[TR]
[TD]Week1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]Person 1[/TD]
[TD="align: center"]85[/TD]
[TD="align: center"]5,200[/TD]
[/TR]
[TR]
[TD="align: center"]Person 2[/TD]
[TD="align: center"]75[/TD]
[TD="align: center"]4,200[/TD]
[/TR]
[TR]
[TD="align: center"]Person 3[/TD]
[TD="align: center"]144[/TD]
[TD="align: center"]9,300[/TD]
[/TR]
[TR]
[TD="align: center"]Person 4[/TD]
[TD="align: center"]130[/TD]
[TD="align: center"]8,000[/TD]
[/TR]
[TR]
[TD="align: center"]Person 5[/TD]
[TD="align: center"]252[/TD]
[TD="align: center"]14,000[/TD]
[/TR]
[TR]
[TD="align: center"]Person 6[/TD]
[TD="align: center"]66[/TD]
[TD="align: center"]2,000[/TD]
[/TR]
[TR]
[TD="align: center"]Person 7[/TD]
[TD="align: center"]91[/TD]
[TD="align: center"]3,500[/TD]
[/TR]
[TR]
[TD="align: center"]Person 8[/TD]
[TD="align: center"]277[/TD]
[TD="align: center"]17,000[/TD]
[/TR]
[TR]
[TD="align: center"]Person 9[/TD]
[TD="align: center"]252[/TD]
[TD="align: center"]17,000[/TD]
[/TR]
[TR]
[TD]Week2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]Person 1[/TD]
[TD="align: center"]252[/TD]
[TD="align: center"]18,000[/TD]
[/TR]
[TR]
[TD="align: center"]Person 2[/TD]
[TD="align: center"]253[/TD]
[TD="align: center"]23,000[/TD]
[/TR]
[TR]
[TD="align: center"]Person 3[/TD]
[TD="align: center"]109[/TD]
[TD="align: center"]7,800[/TD]
[/TR]
[TR]
[TD="align: center"]Person 4[/TD]
[TD="align: center"]109[/TD]
[TD="align: center"]6,500[/TD]
[/TR]
[TR]
[TD="align: center"]Person 5[/TD]
[TD="align: center"]96[/TD]
[TD="align: center"]6,300[/TD]
[/TR]
[TR]
[TD="align: center"]Person 6[/TD]
[TD="align: center"]271[/TD]
[TD="align: center"]9,700[/TD]
[/TR]
[TR]
[TD="align: center"]Person 7[/TD]
[TD="align: center"]271[/TD]
[TD="align: center"]19,000[/TD]
[/TR]
[TR]
[TD="align: center"]Person 8[/TD]
[TD="align: center"]109[/TD]
[TD="align: center"]8,000[/TD]
[/TR]
[TR]
[TD="align: center"]Person 9[/TD]
[TD="align: center"]109[/TD]
[TD="align: center"]7,250

[/TD]
[/TR]
</tbody>[/TABLE]
<strike></strike>
Essentially I do not want to include any individuals that have a low duration value as it would skew the data. So In week one I would only want the average of the distances covered of the highlighted players. They are within 10% of the maximum duration achieved (277mins) in that week. Which would be 16,000m.

For week two I would only use the highlighted to generate an average value of 17,425m as the maximum duration is 271 mins and the rest are within 10% of that maximum duration.



This will be used in a powerpivot so I am looking to use a calculated measure. This is only a set of dummy data but if someone would be happy to explain how I could maybe attempt this I'd very much appreciate it.

Thank you in advance



 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You could use an array formula. ctrl-alt-enter after typing in the formula. seems to work.


Excel 2010
ABC
1Sum of Duration (Mins)Sum of Distance (m)
10Person 827717000
11Person 925217000
12Week217425
13Person 125218000
14Person 225323000
15Person 31097800
16Person 41096500
17Person 5966300
18Person 62719700
19Person 727119000
2Week116000
20Person 81098000
21Person 91097250
3Person 1855200
4Person 2754200
5Person 31449300
6Person 41308000
7Person 525214000
8Person 6662000
9Person 7913500
Sheet2
Cell Formulas
RangeFormula
C2{=AVERAGE(IF(B3:B11>0.9*MAX(B3:B11),C3:C11))}
C12{=AVERAGE(IF(B13:B21>0.9*MAX(B13:B21),C13:C21))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
sorry, my html converter is really weirded out. but the formula is correct if you put it in C2 and C12.

-R
 
Upvote 0
Hi.

Thank you for this, however, I am trying to do it using powerpivot and a calculated measure.

Do you maybe have any suggestions?

K
 
Upvote 0
Sorry, I don't have powerpivot installed. Im working on that. Looks like an interesting add-on. However, it appears that you are just working with data tables. You could use the formula I supplied to create an additional table (which would could stay updated week by week) and then just access it through powerpivot? Just a thought.
 
Upvote 0
This is how to do it in Power Query with M:

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"duration", Int64.Type}, {"distance", type number}, {"week", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"week"}, {{"Max", each List.Max([duration])*0.9, type number}, {"Data", each _, type table}}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"duration", "distance"}, {"duration", "distance"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Data", each [duration] >= [Max]),
    #"Grouped Rows1" = Table.Group(#"Filtered Rows", {"week"}, {{"Average", each List.Average([distance]), type number}})
in
    #"Grouped Rows1"

simply load the result to the Data Model and it will be availabe for further use in Power Pivot


LinkToFile
 
Last edited:
Upvote 0
@Imke, I like your solution :-) And here is a little shorter, based on yours ( but probably a little more complicated ;-))
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"duration", Int64.Type}, {"distance", type number}, {"week", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"week"}, {{"Average", each List.Average(Table.SelectRows(_, (x) => x[duration] >= List.Max([duration])*0.9)[distance]), type number}})
in
    #"Grouped Rows"

sq ;-)
 
Upvote 0
Hey Bill, always love to see your code :-)

I often also prefer nesting of steps that belong together for better overview in my queries, but wonder which versions will scare M-newbies more:
Non-nested versions are easier to follow (and understand?) in the query editor but require longer code. Whereas nested versions often become more similar to Excel-formulas but there you are missing the formula evaluation-feature in M.

@All: What do you think about this?
 
Upvote 0
@Imke and Bill.

Really appreciate this from both of you guys. Sorry for the delay but I have been away.

I get the logic of what you have written, @Imke, i'll use your file to take a look at it. I use powerquery for very basic results but I will try this out.

I'll let you know how I get on. Thank you very much again though. I appreciate your time.

Gogs
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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