'Distinct 12 month rolling count of unique customers

I have a database of unique customers (CUST_ID), dates (MONTH_ID) and brands (ABC, XYZ) and I want to get a distinct count of each individual customer over a 12 month period for each brand. So, as an example, if a customer transacted three times with one brand over a 12 month period say in Jan21, Feb21 and Mar21 and I run a 12 month rolling report in Mar21 for that brand I would like this customer to show up once (not three times). Similarly, if they transacted in 10 of the 12 months again I would only want to count them once.

Ultimately, I need to assign a population weight to this customer over the 12 month period and we can't have them being counted three times in 12 months as it will throw off the customer penetration metric.

I would also like a monthly column in my data output like the below.

Essentially it is that last column I want to populate, but I keep getting a distinct count by month because I have the monthly column included rather than a count over a 12 month period.

Thanks enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source