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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

SUM last 5 records

Data Structure: I have grouped each individual and assigned a sub-group index value in power query to each result for that individual (see example below) 

 

Table Name : 'Results'

Name

Force (N) 

Sub-Group Index

Bill

130

1

Bill

140

2

Bill

150

3

Bill

130

4

Bill

125

5

Bill

140

6

Ted

180

1

Ted

155

2

Ted

140

3

Ted

130

4

Ted

160

5

Ted

170

6

Ted

170

7

 

Aim: I wish to SUM the values of the results table based on the last 5 records for eac individual. My logic of including a sub-group Index was to to try and calculate the sum based off the TOPN ranked DESC - not sure if this is a good approach?

 

Any help would be greatly appreciated!

 

1 ACCEPTED SOLUTION

Here is a version that also handles the totals correctly.

 

Last5v2 = 
var n = SELECTCOLUMNS(VALUES(Results[Name]),"Name",Results[Name])
var s = ADDCOLUMNS(n,"v",sumx(TOPN(5,filter(Results,Results[Name]=[Name]),Results[Sub-Group Index],DESC),[Force (N) ]))
return sumx(s,[v])

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

yes, that's a good approach. Here is one possible implementation

 

Last5 = 
var n = SELECTEDVALUE(Results[Name])
var t = TOPN(5,filter(all(Results),Results[Name]=n),Results[Sub-Group Index],DESC)
return sumx(t,[Force (N) ])

 

It doesn't give you nice totals. Not sure if that is important. 

Here is a version that also handles the totals correctly.

 

Last5v2 = 
var n = SELECTCOLUMNS(VALUES(Results[Name]),"Name",Results[Name])
var s = ADDCOLUMNS(n,"v",sumx(TOPN(5,filter(Results,Results[Name]=[Name]),Results[Sub-Group Index],DESC),[Force (N) ]))
return sumx(s,[v])
Anonymous
Not applicable

@lbendlin You sir are a genius! It works perfectly and the way you've laid it out it makes so much sense. Thank you so much!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors