KNOWLEDGE BASE

## How to create a stacked bar chart of new and existing customers, with customers who have not purchased in the last N months as new customers

Published: 05 Feb 2023

### Question

How to create a stacked bar chart of new and existing customers, with customers who have not purchased in the last N months as new customers.

<IMAGE>
The number entered in the parameter is calculated as N.

### Environment

• Tableau Desktop

1. Drag [Customer Name] to Rows.
2. With right clicking [Order Date] to Columns -> Select [MY(Order Date)].
3. Create a new parameter as following.

4. Right click on [Select Months] -> Show Parameter
5. Create a new calculated field as following.
Name : New Customer
Calculation : IF SUM([Sales]) > 0 AND IFNULL(WINDOW_MIN(SUM([Sales]),-([Select Months]),-1),0) = 0
THEN 1
END
6. Create a new calculated field as following.
Name : New Customer Window_SUM
Calculation : WINDOW_SUM(New Customer)
7. Create a new calculated field as following.
Name : Existing Customer
Calculation : IF SUM([Sales]) > 0 AND WINDOW_MIN(SUM([Sales]),-([Select Months]),-1) > 0
THEN 1
END
8. Create a new calculated field as following.
Name : Existing Customer Window_SUM
Calculation : WINDOW_SUM([Existing Customer])
9. Drag [Measure Name] to Color on Marks.

10. Right click on [Measure Name] on Color -> Filter -> Check in only [New Customer Window_SUM] and [Existing Customer Window_SUM].

11. Drag [Measure Values] to Rows.
12. Change chart type to Bar from Automatic.

13. Right click on [New Customer Window_SUM] on Measure Values Pane -> Edit Table Calculation -> Set as following.

14. Right click on [Existing Customer Window_SUM] on Measure Values Pane -> Edit Table Calculation -> Set as following.

15. Create a new calculated field as following.
Name : First Filter
Calculation : FIRST()=0
16. Drag [First Filter] to Filter Pane -> Click on "All" -> OK
17. Right Click on [First Filter] -> Edit Table Calculation -> Set as following.

18. Right Click on [First Filter] -> Edit Filter -> Check in only "True".

19. Right click on [Customer Name] on Rows -> Show Header.