Get Inspire insights from former attendees in our AMA discussion thread on Inspire Buzz. ACEs and other community members are on call all week to answer!

Alteryx Designer Cloud Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Cloud.

Count Unique Values on a Moving 12 month basis

HI Everyone,

 

I am a first time poster and new Trifacta user. Currently trying to find a method to count the number of unique values in a column within a moving 12 month period, for each month.

 

i.e unique values from:

 

May 2021 to May 2022

June 2021 to June 2022

 

I have tried a number of methods using the pivot function in combination with the count distinct function, stipulating that it is greater than or equal to the 1st month and less than the 13th month of the 12 month period. Nothing seems to work at it only returns the unique count for the month.

 

I am not sure what else I can do to achieve what I am looking for, I really appreciate any help in advance!

2 REPLIES 2
APrasad_Tri
Alteryx Alumni (Retired)

Hi Anthony,

 

Can you please provide sample data to experiment?

 

Thanks,

Apeksha

AMiller_Tri
Alteryx Alumni (Retired)

Hi @Anthony Labib? , thank you for your question.

We've discussed this also in an email thread, but allow me to answer here as well =]

 

Solution (rolling distinct from the first month of the table, rather than "calendaric year" is at the end)

I've created a sample table with 24 records. Each record represents a random date in each given month, across 2 years.

I've used numbers for the different store names - just for example.

The next step would be to extract the year and quarter portions of the dates. (some functions can be nested inside each other to minimize recipe steps; however, I'm prioritizing readability)

 

We use the YEAR() function to extract the year:

The quarter is less straightforward. For this, we extract the month by using the MONTH() function (same way). Then, we divide it by 3 - while rounding it up:

I'm using the MERGE() function to ensure the quarter is within the context of the specific year.

 

Next, we're creating the aggregations. We're interested to know how many distinct stores are in each year+quarter.

For this, we use the New Formula transform, and the COUNTDISTINCT() function:

 

Now, if we simply want an aggregated presentation of the info - we'll use the Group-By Transform:

And that's our final output

 

an alternative approach - counting months from the first row in the table

Not sure if you're interested in this, but I thought to add that approach.

So instead of measuring the rolling years+months through "calendaric eyes", we can do it relative to the first date in the table.

 

For this, we use the DATEDIF() function to calculate the difference in dates between the minimum date and the current record's date:

?

  • We're reaching the 6th quarter since the beginning of the data
  • We're nesting 3 functions within one another. MINDATE() inside DATEDIF() inside CEILING().

The process is the same from here, using the COUNTDISTINCT() function and Group-By transform.

 

That's it. I hope that makes sense; please feel free to ask any additional questions regarding the matter.

 

Thanks,

Amit.