Revenue accrual with Python
Once upon a time, I helped Tiendanube's finance team to set a process for revenue accrual. Since I was not familiarized with accounting requirements, first I had to learn what accrual means and the best practices related to its calculation. Also, I needed
Once upon a time, I helped Tiendanube's finance team to set a process for revenue accrual. Since I was not familiarized with accounting requirements, first I had to learn what accrual means and the best practices related to its calculation. Also, I needed to dive into our databases to see how we could organize revenue info. After a few meetings, we were able to set up an automatic process with Python. In this post, I want to share the basics about accrual and one solution using Python.
What is revenue accrual?
In a lot of SasS companies, you can charge for a service that you haven't offered yet, i.e. the users pay in advance. So if you want to know the revenue of your company it's not correct to take the cash flow of a period, because in both ins and outs you can have money that you haven't earned or expended yet. Imagine that most of your users signed up in January and pay an annual subscription, if you just count the cash inflow you'll only have revenue in January (a lot) and then nothing for the rest of the year. Furthermore, if you offer refunds or have a cancellation policy you'll need to revisit the number in the future. This is bad because following GAAP (Generally Accepted Accounting Principles) your numbers must be reproducible and consistent. So the basic idea behind accrual is that you have a contract with a start date, an end date, and an amount, and you have to divide that amount by the periods (generally months) the contract has. You can read more about accrual and why it's important here.
How to do it using Python?
In the company I was working at, the contracts could start and end any day of the month and didn't have a fixed duration. So a contract could have 14 days and start on the 28 of a month, or 255 or whatever. This added a little bit of complexity to the accrual code.
The basic idea behind "the algorithm" was:
Get the amount per day: take the total and divide it by duration in days: total / (end date - start date)
Get the days per month between contract dates: something like a dict {'january':12, 'february':2}, etc.
Multiply the number of days by the amount per day
The first step was easy once we managed to get the correct values from the contract database (we had some inconsistencies there that needed to be fixed and checked). The second step was a little bit trickier. I came up with a quick solution using Pandas that could be improved, but it works. Applying that function to each row generates a new dataframe with each month as a column and the accrued revenue as values. The function only accrues for the specified year, but it's easily adaptable. You can see it in action here.