Forum Discussion

elvincplee's avatar
elvincplee
New Member
4 years ago
Solved

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!!!

  • 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. 



    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: 

     



3 Replies

  • m_alireza's avatar
    m_alireza
    Icon for Solution Specialist rankSolution 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. 



    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: 

     



  • 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?)

    • m_alireza's avatar
      m_alireza
      Icon for Solution Specialist rankSolution Specialist

      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. 


      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