RFM is a customer segmentation technique for analysing customer behaviour by using their transaction history based on three main metrics:
RFM analysis can help to improve the impact of marketing by allowing you to make them more relevant to customers.
RFM works by giving having a number of bins to place each customer for each of the rthree metrics above. Usually you have five bins for each metric, for the package I will be using, 5 is the highest score and it will mean most recent, most frequent, and high cost purchases, respectively. Subsequently, 1 indicates the worst outcome.
To perform the analysis I will be using the rfm package for R, this will have to be installed into R prior to calling in Tableau since Rserve cannot install packages from Tableau.
The Tableau sample Superstore data set is perfect for this example, it contains customer information, order dates, order IDs, and price of order. Those are some of the fields taken by the rfm_table_customer() function from the rfm package - more information about that function in the image below.
Before calling the function from R we will have to do some calculations in Tableau. The n_transactions and recency_days arguments are not in Superstore but we can calculate those. The number of transactions is the count distinct of the Order IDs, and number of days since the last transaction requires using LODs to fix to the customer and then finding the latest purchase date for that person then subtract the date from the analysis date. For the analysis date I used TODAY(), so each time I update the workbook the recency days field will change hence impacting the RFM score.
Once that preamble is set up, it's time to run through the calculation to work out the rfm score.
|The output of this function is a three-digit integer, I convert it to string so that I can separate the elements later. The script starts with calling the package required.|
|rfm requires the use of dataframes, so I create one with the arguments here, also assigning names for referencing in the next step.|
|Calling the rfm function (described above), when inputting the arguments, the first is referencing the dataframe, then arguments after that are just the respective column names. I also created an argument for the analysis date and changed the number of bins to 4.|
|To access the rfm score, the output of the function are multiple dataframes in a tibble. So, first I assign the table I require to 'tab', then I can output the rfm score from tab.|
|Lastly, list the arguments from Tableau to be referenced in R, notice I have a field called 'analysis date' this is just a calculation with TODAY() in it.|
Since I changed the number of bins to four, the scoring now only reaches a maximum of 4 as the highest score. Putting my output in a crosstab with the customer name and other arguments we get the score per customer.
I converted the whole calculation to a string so that I could separate them to get the R, F, and M scores individually for analysis. For example, looking at how many customers fall into each bin for frequency, below.
The graph shows that most customers shop quite infrequently at Superstore, but there are a good number of repeat customers in bin 4.
Hopefully, this little snippet of R will be useful in your business analyses and it would be great to see other uses of rfm analyses in Tableau. It is also possible to do it in Tableau, it would be interesting to compare the outcomes of both methods at a later date.
Next week, I will be looking at sentiment analysis using a package in R and bringing that in for visualisation in Tableau.
If you would like to find out more or want bespoke training on using R in Tableau please contact us.