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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Filipe_Pacheco
New Member

How to sum the last 5 rows separated by categories in power bi using DAX syntax

Hi everyone, I'm trying to sum the last 5 rows in a table for each row and divided by category, such as the example below that I did in Excel. The expected result is the column D 'RUNNING_SUM'.

 

If I would write this in SQL, I would use SUM OVER PARTITION. 

 

Could you anwser that for me?

 

.Example in Excel how to do itExample in Excel how to do it

1 ACCEPTED SOLUTION

hi @Filipe_Pacheco 

try like:

Coulmn = 
VAR _fam = [Fam]
VAR _year = [YEAR]
RETURN
SUMX(
    FILTER(
        TableName,
        TableName[Fam] = _fam
           &&TableName[Year] >=_year-4
           &&TableName[Year]<=_year
    ),
    TableName[Value]
)

 

FreemanZ_0-1678282226371.png

 

View solution in original post

4 REPLIES 4
FreemanZ
Super User
Super User

hi @Filipe_Pacheco 

could you provide the expected result directly?

Hi @FreemanZ , I edited the post, but the expected result is the Column D 'RUNNING_SUM'.

hi @Filipe_Pacheco 

try like:

Coulmn = 
VAR _fam = [Fam]
VAR _year = [YEAR]
RETURN
SUMX(
    FILTER(
        TableName,
        TableName[Fam] = _fam
           &&TableName[Year] >=_year-4
           &&TableName[Year]<=_year
    ),
    TableName[Value]
)

 

FreemanZ_0-1678282226371.png

 

That one was hard, congratulations, overwhelming 👏

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.