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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
elvincplee
New Member

DAX for next purchase date

Hi, I'm going to calculate the next purchase date by DAX.

 

The background is that I have a table with purchase_date, customer, product, qty. I'd like to base on the same customer and product to calculate the next purchase date. The table should as follow.

 

DateCustomerProductQty
5-Feb-22B004PC0037
9-May-22B004PC00312
25-Aug-22B004PC00388
7-Nov-22B004PC00323
8-Apr-22D002PB0029
22-Jul-22D002PB00246
21-Oct-22D002PB0027
22-Dec-22D002PC0035
1-Jan-22E001PA0012
4-Mar-22E001PA0018
7-Jun-22E001PA00166
24-Sep-22E001PA00134

 

While I'd like to insert the last column "next purchase date" with result like below

 

DateCustomerProductQtyNext Date
5-Feb-22B004PC00379-May-22
9-May-22B004PC0031225-Aug-22
25-Aug-22B004PC003887-Nov-22
7-Nov-22B004PC00323 
8-Apr-22D002PB002922-Jul-22
22-Jul-22D002PB0024621-Oct-22
21-Oct-22D002PB0027 
22-Dec-22D002PC0035 
1-Jan-22E001PA00124-Mar-22
4-Mar-22E001PA00187-Jun-22
7-Jun-22E001PA0016624-Sep-22
24-Sep-22E001PA00134 

 

I'm trying the "DATEADD" or "PARALLELPERIOD" but unable to get the result. Thanks a lot!!!

1 ACCEPTED SOLUTION
m_alireza
Solution Specialist
Solution Specialist

Hi @elvincplee ,

First, create an index column. 
You can do this by opening Power Query Editor, go to Add Column tab and click on Index Column. After you do that, close Power Query Editor. 

addindexcolumn.png

Then create a calculated column as below: 

Next Date = 

CALCULATE(
    MAX('Table'[Date]),
    FILTER(
        'Table',
        'Table'[Index] = EARLIER('Table'[Index],1) +1 && 'Table'[Customer]=EARLIER('Table'[Customer]) && 'Table'[Product]=EARLIER('Table'[Product]
        )))


Sample output: 
outputnextdate.png

 



View solution in original post

3 REPLIES 3
elvincplee
New Member

Thanks for your prompt help. How about if customer buying the same product on the same day? (there is tiny chance but can it work if indexed the column?)

Hi @elvincplee ,

What is the expected output if a customer buys the same product on the same day? should the earliest occurances of the purchase have the date in the Next Date even though its on the same day and the latest blank? Or should it all be blank?
e.g. 

m_alireza_0-1663414842234.png


One thing I didnt mention was the sort order for the date. In the example you gave, the dates were in the order I needed them to be for the index to work. But if the dates are all scrambled, then you need the index to sort by date in ascending order, grouped by customer and product. 

To do that, instead of adding the index column, add this step instead, changing the references as required. Make sure date column has date as data type before doing this.

= Table.Group(Source, {"Customer"}, {{"AllRows", (tableint) => let sort = Table.Sort(tableint,{"Date", Order.Ascending}), AddIndex = Table.AddIndexColumn(sort,"Index",1) in AddIndex}})

And then expand all rows. 

Attached is a sample pbix file so its easier to understand. You can modify it as needed to fit your purpose.

https://drive.google.com/file/d/1tmfjCeoGbaO2qsiAhq_pUKKL7J6j4Nlj/view?usp=sharing 



 

m_alireza
Solution Specialist
Solution Specialist

Hi @elvincplee ,

First, create an index column. 
You can do this by opening Power Query Editor, go to Add Column tab and click on Index Column. After you do that, close Power Query Editor. 

addindexcolumn.png

Then create a calculated column as below: 

Next Date = 

CALCULATE(
    MAX('Table'[Date]),
    FILTER(
        'Table',
        'Table'[Index] = EARLIER('Table'[Index],1) +1 && 'Table'[Customer]=EARLIER('Table'[Customer]) && 'Table'[Product]=EARLIER('Table'[Product]
        )))


Sample output: 
outputnextdate.png

 



Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

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