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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. 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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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