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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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