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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Sum by current and the last two months

Hello community 

 I want to sum the last 3 months for each Month by client 

This exemple of data 

Month ClientAmount
Decembre A5
JanA2
FebA3
MarchA4
AprilA5

 

Wanted Result 

Month ClientAmount
Decembre A5
JanA7
FebA(3+2+5) = 10
MarchA(4+3+2) = 9
AprilA5 +4+3 = 12

 

I want to sum the amount of the current month and the last two months for each  customer as calculated column not a measure

 

Best Regards and Thank You for your help

1 ACCEPTED SOLUTION
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

Not sure whether your data is that simple as your example.

Try below method:

Create an index column in query editor;

Then create a calculated column as below:

Column = 
CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[Index]>=EARLIER('Table'[Index])-2&&'Table'[Index]<=EARLIER('Table'[Index])&&'Table'[Client]=EARLIER('Table'[Client])))

And you will see:

vkellymsft_0-1632734603067.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my reply as a solution!

View solution in original post

2 REPLIES 2
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

Not sure whether your data is that simple as your example.

Try below method:

Create an index column in query editor;

Then create a calculated column as below:

Column = 
CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[Index]>=EARLIER('Table'[Index])-2&&'Table'[Index]<=EARLIER('Table'[Index])&&'Table'[Client]=EARLIER('Table'[Client])))

And you will see:

vkellymsft_0-1632734603067.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my reply as a solution!

DataZoe
Microsoft Employee
Microsoft Employee

@Anonymous To do this as a calculated column, I first figured out a proper date format for your month:

Month Date = 
VAR First3 =
    LEFT ( [Month], 3 )
VAR SearchString = "XXJanFebMarAprMayJunJulAugSepOctNovDec"
VAR MonthNum =
    DIVIDE ( SEARCH ( First3, SearchString, 1, -1 ), 3 )
VAR YearNum =
    IF ( MonthNum <= 4, 2021, 2020 )
RETURN
    DATE ( YearNum, MonthNum, 1 )

 

Then the requested calculated column:

 

Amount Plus Last 2 =
VAR client = 'Table'[Client]
VAR maxmonth = 'Table'[Month Date]
VAR minmonth =
    DATE ( YEAR ( maxmonth ), MONTH ( maxmonth ) - 2, 1 )
VAR newamount =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        ALL ( 'Table' ),
        'Table'[Month Date] >= minmonth
            && 'Table'[Month Date] <= maxmonth
            && 'Table'[Client] = client
    )
RETURN
    newamount

 

You can also create the measure then reference it in a calculated column too.

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.