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
yuvals907
New Member

calculating multiple averages by category and flattening a table

Dear forum,

 

i'm trying to transform a table like this:

studentsubjecttypescore
s-1Mathhomework100
s-1Mathquiz90
s-1Mathfinal exam80
s-1Historyhomework80
s-1Historyquiz70
s-1Historyfinal exam60
s-2...  
s-2   

 

into a flat table like this (1 line per student):

studentAVG MathAVG HistoryAVG homeworkAVG quizAVG final exams
s-19070908070
s-2...    

 

i tried "group by" student and then got a nested table, and wasn't sure how to continue.

i do not want to add the new columns manually as the categories might change over time.

 

Thanks a lot!

 

 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Use below

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKtY1VNJR8k0syQBSGfm5qeX5RdlApqGBgVKsDpp8YWlmFZCyxCKVlpmXmKOQWpGYC+RYICvwyCwuyS+qRDUehwqoBebYZVHsMIOpMcLqfktLTGmo6RammFIoRpsgG43V+SamWFWUpBaXoDsNh/PNkU0IyKgszkwuRrXDDLsKqB3m5kqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [student = _t, subject = _t, #"type" = _t, score = _t]),
    Custom1 = Table.FromColumns({Source[student], List.Transform(Source[subject], each _ & " Avg"), List.Transform(Source[score], Number.From)}, {"subject","temp","score"}) & Table.FromColumns({Source[student], List.Transform(Source[type], each _ & " Avg"), List.Transform(Source[score], Number.From)}, {"subject","temp","score"}),
    #"Grouped Rows" = Table.Group(Custom1, {"subject", "temp"}, {{"Avg", each List.Average([score]), type nullable number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[temp]), "temp", "Avg")
in
    #"Pivoted Column"

 

View solution in original post

2 REPLIES 2
Vijay_A_Verma
Super User
Super User

Use below

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKtY1VNJR8k0syQBSGfm5qeX5RdlApqGBgVKsDpp8YWlmFZCyxCKVlpmXmKOQWpGYC+RYICvwyCwuyS+qRDUehwqoBebYZVHsMIOpMcLqfktLTGmo6RammFIoRpsgG43V+SamWFWUpBaXoDsNh/PNkU0IyKgszkwuRrXDDLsKqB3m5kqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [student = _t, subject = _t, #"type" = _t, score = _t]),
    Custom1 = Table.FromColumns({Source[student], List.Transform(Source[subject], each _ & " Avg"), List.Transform(Source[score], Number.From)}, {"subject","temp","score"}) & Table.FromColumns({Source[student], List.Transform(Source[type], each _ & " Avg"), List.Transform(Source[score], Number.From)}, {"subject","temp","score"}),
    #"Grouped Rows" = Table.Group(Custom1, {"subject", "temp"}, {{"Avg", each List.Average([score]), type nullable number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[temp]), "temp", "Avg")
in
    #"Pivoted Column"

 

Thank you so much Vijay,

It's great to see that the community is still stronger than google & ChatGPT together 😀

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.