Hi Tom, thanks for your questions. Happy to clarify: 1) The data I provided is not the source data, the source data contains only one ID column with different ID values as you might expect. Each ID can have multiple transaction records with different dates and transaction values. The dataset I have provided in the post is what I got after transposing the data by Date, turning each ID into it's own column (ID1, ID2) with the values of those columns being the transaction values. The 'Counter' columns are all missing initially as I have just created them, there is one column per ID. The intention is to add +1 to each ID's 'Counter' column for each row based on the number of preceding records that have a different date. Eg: if person 1 has 10 transactions, I'll need to compare transaction 1 to transactions 2-10, transaction 2 to transactions 3-10 and so on - and add the number to that ID's counter column by transaction level. 2) The data was transposed because it was too difficult to loop through each ID and count how many transactions matched the criteria, as each ID can have a different number of transactions. My initial solution could not finish running for an hour on the full dataset (which contains a lot more IDs and records), whereas after transposing it ran in little time (albeit with inaccurate results). As the data is transposed by date, 3) The values for the first and second ID variables are the transaction values for each ID, and the amount is not as relevant as whether it is missing or not. If the value is populated, it means that this ID has a transaction of this amount on this date. If it is missing, then this ID does not have a transaction on this date. In the 2 sample cases I provided, ID1 and ID2 do not have any transactions on the same day, hence only one of each column is populated for each observation. ID1 has 4 transactions in total - on 1st March, 1st April and 2 on 1st May. ID2 has 6 transactions in total, 5 on the same day, and 1 on 1st Feb. Hence, in the expected results - ID1's Counter column has a value of 2 for the third record, as there are 2 preceding transactions (the dataset is sorted by date) for ID1 that have a different date. The fourth record should have a value of 2 as well for this reason, as the 3rd and 4th records have the same day and thus would not count. Similarly for the second ID2 counter column, the 5 at the end is because for ID2 there are 5 preceding transactions with a different date, hence why it is 5, and hence why the previous rows were missing even in the counter column. As to why the values are missing instead of 0, it's an oversight on my part - I should initialize the counter columns to be 0. Hope that clarifies it, and apologies if the initial post was not very clear!
... View more