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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anon29
Helper II
Helper II

Get previous row value

Hi all, I am trying to create a new column, (unsure to do it in Power Query editor or Dax) to show the previous value based on 2 conditions.

 

I want add a column called "Previous Purchase". It has to be for the same Customer ID. So below we have Sally Smith purchasing a Soft Toy at 4:56pm, so I want to show the previous purchase was a Barbie, and so on till its a different Customer ID. The time column is not always sorted in order. I'd like to avoid sort if possible as it tends to slow down my refreshes in power query.

After this I will apply a filter on the irrelevant Previous Purchase products.


This is what my Data looks like this below:

Anon29_0-1689229641145.png

 

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @Anon29 ,

 

I would assume that the date has to be considered in the calculation  as well and not just the time. If so,  create a datetime column first. If those two columns are in the proper date type, you should be able to add both.

datetime =
'table'[date] + 'table'[time]

then this column to acccess the previous record that is preceding the current time and which is equal to the current row customer

Previous Purchase =
CALCULATE (
    LASTNONBLANK ( 'table'[product], 1 ),
    FILTER (
        FILTER ( 'table', 'table'[customer id] = EARLIER ( 'table'[customer id] ) ),
        'table'[datetime] < EARLIER ( 'table'[datetime] )
    )
)

If you don't need to consider the date, just keep the datetime calculated column and use 'table'[time instead] in the second calc column formula.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

3 REPLIES 3
danextian
Super User
Super User

Hi @Anon29 ,

 

I would assume that the date has to be considered in the calculation  as well and not just the time. If so,  create a datetime column first. If those two columns are in the proper date type, you should be able to add both.

datetime =
'table'[date] + 'table'[time]

then this column to acccess the previous record that is preceding the current time and which is equal to the current row customer

Previous Purchase =
CALCULATE (
    LASTNONBLANK ( 'table'[product], 1 ),
    FILTER (
        FILTER ( 'table', 'table'[customer id] = EARLIER ( 'table'[customer id] ) ),
        'table'[datetime] < EARLIER ( 'table'[datetime] )
    )
)

If you don't need to consider the date, just keep the datetime calculated column and use 'table'[time instead] in the second calc column formula.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hi @danextian , can this also be achieved in the power query editor?

It can be but pls take note that DAX is more optimized at scanning a table than M. Depending on your source and the size of your table, this can be very slow to run. 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.