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
HeirsPowerBi
Helper I
Helper I

Get Sum of Current and Previous for each Customer

Hey guys.

I have a little challenge.

I have a table like the one below with customers and their orders.

 

CustomerOrder IDAmountDate
123500001/09/2022
124600002/09/2022
125700003/09/2022
126300004/09/2022
227390001/09/2022
228400002/09/2022
229290003/09/2022
230100004/09/2022

 

 

I would like to create two additional columns that will tell me the order id of the customers last purchase and then the sum of the current purchase and the previous one as well. So the output should be something like the table below

 

Customer Order ID Amount Date Previous ID Plus Previous
1    23500001/09/2022  
1    24600002/09/2022      2311000
1    25700003/09/2022      2413000
1    26300004/09/2022      2510000
2    27390001/09/2022  
2   28400002/09/2022     277900
2   29290003/09/2022     286900
2   30100004/09/2022     293900

 

I can do this DAX but i want to be able to do it in Power Query because there are some other steps i need to do that depend on this one.

 

I would really appreciate any help. Thanks

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @HeirsPowerBi 

For Previous ID column you can use merge queries function

1. Add 2 New Columns with Index Starting from 0 and 1

Dax_Noob_0-1662022911770.png

2. Go to Home Tab > click on Merge Queries>Index left join with Index.1 > Sort Rows

Dax_Noob_1-1662022956684.png

 


For Plus Previous you can add a custom column .

List.Sum(List.FirstN(#"Renamed Columns"[Amount],[Index.1]))


Hope that helps.

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@HeirsPowerBi ,

Edited: I Noticed @Anonymous  Already replied. Only refer if that solution does not work

 

Create a new column  like

 

new column=

Var _last = sumx(filter(Table, Table[Customer] =earlier([Customer]) && Table[Order ID] = earlier( Table[Order ID] )-1 ) , Table[Amount])

return

if(isblank(_last), blank(), Table[Amount]+_last)

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

Hi @HeirsPowerBi 

For Previous ID column you can use merge queries function

1. Add 2 New Columns with Index Starting from 0 and 1

Dax_Noob_0-1662022911770.png

2. Go to Home Tab > click on Merge Queries>Index left join with Index.1 > Sort Rows

Dax_Noob_1-1662022956684.png

 


For Plus Previous you can add a custom column .

List.Sum(List.FirstN(#"Renamed Columns"[Amount],[Index.1]))


Hope that helps.

Okay so this works perfectly for the previous order ID but the Solution for plus previous is not working exactly as i would have hoped but my way around it was to get the previous amount in one column and then create another column that adds the current one to the previous one.

 

Thank You so much

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.