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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
ilyakoslov
Frequent Visitor

Cumulating values in a calculated column

Hey guys, I have a problem in power BI that I can't quite understand what I'm doing wrong. What I want to do is to have a column that Sums all the values for each ID for the last 12 months, for that I'm using the code below. However it is giving me the cumulated value for the whole time. Not only the last 12 months. It is as my DATEADD isn't working. 

 

"Quantidade vendida" is my value and "Material" is my ID, and "Data" is my date. 

 

Qtd ven 12m = CALCULATE(
    SUM('Base de vendas S'[Quantidade vendida]),
    FILTER('Base de vendas S',
    'Base de vendas S'[Material] = EARLIER('Base de vendas S'[Material]) &&
    'Base de vendas S'[Data] >= DATEADD('Base de vendas S'[Data],-12,MONTH) &&
    'Base de vendas S'[Data] <= EARLIER('Base de vendas S'[Data])))
1 REPLY 1
ichavarria
Solution Specialist
Solution Specialist

Hi @ilyakoslov,

 

It looks like the problem might be with the use of the EARLIER function in your code. The EARLIER function is used to reference a previous row context in the current context, but in this case, it might not be working as intended.

 

To get the sum of the last 12 months for each ID, you can try using the CALCULATETABLE function with the FILTER function to filter the data to the last 12 months and the SUMMARIZE function to group the data by ID. Here's an example of the code you can use:

 

Qtd ven 12m =

CALCULATE(

SUM('Base de vendas S'[Quantidade vendida]),

FILTER(

SUMMARIZE(

'Base de vendas S',

'Base de vendas S'[Material],

"Last 12 Months",

DATESBETWEEN('Base de vendas S'[Data],

NEXTDAY(SAMEPERIODLASTYEAR(LASTDATE('Base de vendas S'[Data]))),

LASTDATE('Base de vendas S'[Data])) ),

'Base de vendas S'[Material] = 'Base de vendas S'[Material] &&

'Base de vendas S'[Data] >= [Last 12 Months]

)

)

 

This code creates a table that groups the data by ID and the last 12 months using the SUMMARIZE function and the DATESBETWEEN function. It then filters the table to the last 12 months for each ID using the FILTER function and the calculated column "Last 12 Months" from the SUMMARIZE function.

 

I hope this helps! Let me know if you have any further questions.

 

Isaac Chavarria

If this post helps, then please consider Accepting it as the solution and giving Kudos to help the other members find it more quickly.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.