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

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.


Sum of Duration (Mins)Sum of Distance (m)
Week1
Person 1855,200
Person 2754,200
Person 31449,300
Person 41308,000
Person 525214,000
Person 6662,000
Person 7913,500
Person 827717,000
Person 925217,000
Week2
Person 125218,000
Person 225323,000
Person 31097,800
Person 41096,500
Person 5966,300
Person 62719,700
Person 727119,000
Person 81098,000
Person 91097,250


<tbody>
</tbody>
<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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

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