Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Group By and Aggregate Based on Aggregated Column

Hi Community,

 

I have been stuck with these for a few hours, i would like to group by 'Name' and find the Mode for each Name . From the calcualted Mode , i would like find the average for those containing the mode value. Eg Group A, avg = (12.1+12.4+12.7)/3

 

Data:

Namestepcountduration
A512.1
A512.4
A512.7
A415
B15.3
B15.5
B21.7

 

Desired Result

NameStepcountModeAvgBasedOnMode
A512.4
B15.4

 

I tried doing like below but can't solve it

= Table.Group(#"Sorted Rows", {"Name"}, {{"Details", each _, type table [Name=text, duration=number, stepcount= number]},{"StepCountMode",each List.Mode([stepcount]), type number}, {"AvgBasedOnMode", each let mode = List.Mode([stepcount]) , x = List.Select([stepcount],each _ >0) in List.Average(List.Select([duration],each x= mode)), type number}})

 

 

Any help in Dax / Mquery is fine

Thanks in advance

1 ACCEPTED SOLUTION
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

Try the below

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIFYkMjPUOlWB0UARN0AXO4gAlIwBTMdQIxQWr0jNH4CHkjkBhIeywA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name = _t, stepcount = _t, duration = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"stepcount", Int64.Type}, {"duration", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name", "stepcount"}, {{"Count", each Table.RowCount(_), type number}, {"AvgBasedOnMode", each List.Average([duration]), type number}}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Name"}, {{"tbl", each Table.First( Table.Sort( _,{{"Count", Order.Descending}}) ), type record }}),
    #"Expanded tbl" = Table.ExpandRecordColumn(#"Grouped Rows1", "tbl", {"stepcount", "AvgBasedOnMode"}, {"StepcountMode", "AvgBasedOnMode"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded tbl",{{"StepcountMode", Int64.Type}, {"AvgBasedOnMode", type number}})
in
    #"Changed Type1"

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

View solution in original post

2 REPLIES 2
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

Try the below

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIFYkMjPUOlWB0UARN0AXO4gAlIwBTMdQIxQWr0jNH4CHkjkBhIeywA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name = _t, stepcount = _t, duration = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"stepcount", Int64.Type}, {"duration", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name", "stepcount"}, {{"Count", each Table.RowCount(_), type number}, {"AvgBasedOnMode", each List.Average([duration]), type number}}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Name"}, {{"tbl", each Table.First( Table.Sort( _,{{"Count", Order.Descending}}) ), type record }}),
    #"Expanded tbl" = Table.ExpandRecordColumn(#"Grouped Rows1", "tbl", {"stepcount", "AvgBasedOnMode"}, {"StepcountMode", "AvgBasedOnMode"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded tbl",{{"StepcountMode", Int64.Type}, {"AvgBasedOnMode", type number}})
in
    #"Changed Type1"

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Anonymous
Not applicable

Hi @Mariusz , thanks for the answer, i didn't think of that and it works! 😄

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Top Kudoed Authors