KNOWLEDGE BASE

## Calculating Last or Next Purchase Date

Published: 25 Oct 2017

### Question

How to calculate the last or next purchase date per customer.

### Environment

Tableau Desktop

The below steps can be reproduced using the attached NextPurchaseDate.twbx workbook. This example is using the Superstore sample data.

### Step 1: Connect to the Data

1. In the Data Source tab, drag and drop the Orders sheet.
2. Place the Orders sheet again to next to the existing Orders sheet
3. Select Inner Join with using Customer Name = Customer Name (Orders 1).
4. Go to Sheet 1 and rename Order Date (Orders 1) to 2nd+ Purchase Date.

### Step 2: Create three calculated fields

1. Select Analysis > Create calculated field
2. Name the calculated field "Not Same Order Date?" and enter the following formula and click OK
`[Order Date]<>[2nd+ Purchase Date]`
3. Select Analysis > Create calculated field
4. Name the calculated field "Later Than First Order Date?" and enter the following formula and click OK
`[Order Date]< [2nd+ Purchase Date]`
5. Select Analysis > Create calculated field
6. Name the calculated field "Next Purchase Date?" and enter the following formula and click OK
`{FIXED [Customer Name],[Order Date]:MIN([2nd+ Purchase Date])}=[2nd+ Purchase Date]`

### Step 3: Build the view

1. Place Customer NameOrder Date and 2nd+ Purchase Date to Rows.
2. Place Not Same Order Date? to Filters and select True.
3. Place Later Than First Order Date? to Filters and select True.
4. Right click Not Same Order Date? on the Filters shelf > Add to context.
5. Right click Later Than First Order Date? on the Filters shelf > Add to context.
6. Place Next Purchase Date? to Filters and select True.