I am trying to create a flag that shows which id1 values have a duplicate mapping to id2 over an arbitrary window n (n = 2 in the example below). For example, given this DataFrame:
df = spark.createDataFrame(
[("2010-03-10", "A", "X"),
("2010-03-10", "A", "Y"),
("2010-03-10", "B", "Z"),
("2010-04-10", "A", "X"),
("2010-04-10", "A", "Y"),
("2010-04-10", "B", "Z"),
("2010-05-10", "A", "X"),
("2010-05-10", "A", "Y"),
("2010-05-10", "B", "Z"),
("2010-06-10", "A", "X"),
("2010-06-10", "B", "Z"),
("2010-07-10", "A", "X"),
("2010-07-10", "B", "Z")],
("date", "id1", "id2")
)
df.show()
+----------+---+---+
| date|id1|id2|
+----------+---+---+
|2010-03-10| A| X|
|2010-03-10| A| Y|
|2010-03-10| B| Z|
|2010-04-10| A| X|
|2010-04-10| A| Y|
|2010-04-10| B| Z|
|2010-05-10| A| X|
|2010-05-10| A| Y|
|2010-05-10| B| Z|
|2010-06-10| A| X|
|2010-06-10| B| Z|
|2010-07-10| A| X|
|2010-07-10| B| Z|
+----------+---+---+
With expected output:
+----------+---+---+--------+
| date|id1|id2|dup_flag|
+----------+---+---+--------+
|2010-03-10| A| X| 1|
|2010-03-10| A| Y| 1|
|2010-03-10| B| Z| 0|
|2010-04-10| A| X| 1|
|2010-04-10| A| Y| 1|
|2010-04-10| B| Z| 0|
|2010-05-10| A| X| 1|
|2010-05-10| A| Y| 1|
|2010-05-10| B| Z| 0|
|2010-06-10| A| X| 1|
|2010-06-10| B| Z| 0|
|2010-07-10| A| X| 0|
|2010-07-10| B| Z| 0|
+----------+---+---+--------+
Where dup_flag is a flag that indicates whether or not id1 contains a duplicate mapping to id2 somewhere in the past 2 months.
That is, for id1 B, it never maps to any other id2 than Z, hence it is never flagged as a duplicate.
For id1 A we do have duplicate mappings. In this example I put the window size to 2, which means that if A maps to multiple id2 within any date of a window that goes at most one month back (i.e. the current month as well as the past month should be included in the window), it should receive a flag. Hence, A receives this flag up until 2010-07-10, as in this period, we have a window that consists of both 2010-06-10 and 2010-07-10, in which A does not map to multiple id2 (it only maps to id2 X in this window).