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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
askelton
Resolver I
Resolver I

Avg from a Variable Table

I need to find out how to calculate the average of a column of a table stored as a variable.  I have data stored in the same format as A-C in the example below.

 

 

 image.PNG

 

 

I want to find out how to average the top 3 values of the 5 most recent dates for each group.   I think I have figured out how to get those top 3 values alone in a table stored as a variable, however I can't seem to average them.

 

 

I first created a table with a unique list of the groups:

 Averages = distinct(Data[Group])

 

Then I tried to make a calculated column as follows: 

 

Avgs = 
var GroupTable = filter(Data, Data[Group] = Averages[Group])
var DatesRankedTable = addcolumns(GroupTable, "DateRank", rankx(GroupTable, [Date]))
var Top5Dates = filter(DatesRankedTable, [DateRank] <= 5)
var ValueRankedTable = addcolumns(Top5Dates, "ValueRank", rankx(Top5Dates, [Value]))
var Top3Values = filter(ValueRankedTable, [ValueRank] <= 3)
return
Average(Top3Values[Value])

If I do all the variable tables manually step by step, the "Top3Values" table comes out as it should.  However then when I try to do the average of [Value] in "Top3Values" it errors out.  How can I get the average of a column that is in this table stored as a variable.

 

1 ACCEPTED SOLUTION
askelton
Resolver I
Resolver I

I'm not sure why I can't average a column from a variable table.  However I was able to get the result I wanted by using:

 

Avgs = 
var GroupTable = filter(Data, Data[Group] = Averages[Group])
var DatesRankedTable = addcolumns(GroupTable, "DateRank", rankx(GroupTable, [Date]))
var Top5Dates = filter(DatesRankedTable, [DateRank] <= 5)
var ValueRankedTable = addcolumns(Top5Dates, "ValueRank", rankx(Top5Dates, [Value]))
var Top3Values = filter(ValueRankedTable, [ValueRank] <= 3)
var AverageValue = sumx(Top3Values[Value])/countrows(Top3Values)
return
AverageValue

 

View solution in original post

1 REPLY 1
askelton
Resolver I
Resolver I

I'm not sure why I can't average a column from a variable table.  However I was able to get the result I wanted by using:

 

Avgs = 
var GroupTable = filter(Data, Data[Group] = Averages[Group])
var DatesRankedTable = addcolumns(GroupTable, "DateRank", rankx(GroupTable, [Date]))
var Top5Dates = filter(DatesRankedTable, [DateRank] <= 5)
var ValueRankedTable = addcolumns(Top5Dates, "ValueRank", rankx(Top5Dates, [Value]))
var Top3Values = filter(ValueRankedTable, [ValueRank] <= 3)
var AverageValue = sumx(Top3Values[Value])/countrows(Top3Values)
return
AverageValue

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.