I would like to create row number that is partitioned by ACCOUNT, NAME and TYPE.
I tried dense rank and row number. However, I need all initial records that contain changes in any of those columns
df = spark.createDataFrame(
[
('20190910', 'A1', 'Linda', 'b2c'),
('20190911', 'A1', 'Tom', 'consultant'),
('20190912', 'A1', 'John', 'b2c'),
('20190913', 'A1', 'Tom', 'consultant'),
('20190914', 'A1', 'Tom', 'consultant'),
('20190915', 'A1', 'Linda', 'consultant'),
('20190916', 'A1', 'Linda', 'b2c'),
('20190917', 'B1', 'John', 'b2c'),
('20190916', 'B1', 'John', 'consultant'),
('20190910', 'B1', 'Linda', 'b2c'),
('20190911', 'B1', 'John', 'b2c'),
('20190915', 'C1', 'John', 'consultant'),
('20190916', 'C1', 'Linda', 'consultant'),
('20190917', 'C1', 'John', 'b2c'),
('20190916', 'C1', 'RJohn', 'consultant'),
('20190910', 'C1', 'Tom', 'b2c'),
('20190911', 'C1', 'John', 'b2c'),
],
['Event_date', 'account', 'name', 'type']
)
Expected outcome:
| Event_date | account | name | type | row_number |
|---|---|---|---|---|
| 20190910 | A1 | Linda | b2c | 1 |
| 20190911 | A1 | Tom | consultant | 1 |
| 20190912 | A1 | John | b2c | 1 |
| 20190913 | A1 | Tom | consultant | 2 |
| 20190914 | A1 | Tom | consultant | 3 |
| 20190915 | A1 | Linda | consultant | 1 |
| 20190916 | A1 | Linda | b2c | 2 |
| 20190917 | B1 | John | b2c | 1 |
| 20190916 | B1 | John | consultant | 1 |
| 20190910 | B1 | Linda | b2c | 2 |
| 20190911 | B1 | John | b2c | 3 |
| 20190915 | C1 | John | consultant | 1 |
| 20190916 | C1 | Linda | consultant | 1 |
| 20190917 | C1 | John | b2c | 1 |
| 20190916 | C1 | John | consultant | 2 |
| 20190910 | C1 | Tom | b2c | 1 |
| 20190911 | C1 | John | b2c | 2 |
