'For Loop Count Unique
I have a table like this
| month | customer_id |
|---|---|
| 2015-01-01 | customer_A |
| 2015-01-01 | customer_B |
| 2015-02-01 | customer_A |
| 2015-02-01 | customer_C |
| 2015-03-01 | customer_A |
| 2015-03-01 | customer_C |
| 2015-03-01 | customer_D |
| 2015-04-01 | customer_C |
| 2015-04-01 | customer_D |
I want to find out how active my customers are every month.
I tried to write a loop that can do like the example below:
Filter to count the unique customer_id in Jan 2015 (in the table above: 2)
Filter all the customer_id in Feb 2015 and check how many are in the customer list of Jan 2015. (in the table above: 1)
Continue to the last month
Loop back to start from Feb 2015
The result should be a table like this (the numbers are random)
| month | month_1 | customers |
|---|---|---|
| 2015-01-01 | 2015-02-01 | 2 (A&B) |
| 2015-01-01 | 2015-02-01 | 1 (only A left) |
| 2015-01-01 | 2015-03-01 | 1 (only A left) |
| 2015-01-01 | 2015-04-01 | 0 (none in the original left) |
| 2015-02-01 | 2015-03-01 | 2 (A&C) |
| 2015-02-01 | 2015-04-01 | 1 (only C left) |
Thank you so much!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
