asked on
Customer Ageing Report - Ageing in Calender Months
Hi Experts,
Am looking for an MS SQL script that will generate a report showing customer ageing balance on a monthly level. The only parameter being the due date. Hence if the parameter due date is selected as 17/09/2023 then the first column should be for September indicating the customer balances from 1st Sept - 30th Sept, the second column should be for August with balances from 1st Aug - 31st Aug...etc going 12 months back, Any amounts beyond 12 months will be indicated under the column 12 Months +
ASKER
Hi David,
Could you please explain where the DueDate column is coming from ? Which table would this be ?
Thanks !
That's sample data. You may change it adapt to your actual environment. You may share some of your snippet code if you require further assistance here.
ASKER
Hi David,
I executed this part of the query : -
DECLARE @DueDate DATE = '20230917'; -- Input Due Date Parameter
SELECT
DATEADD(MONTH, -1 * (ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1), @DueDate) AS StartDate,
DATEADD(MONTH, -1 * (ROW_NUMBER() OVER (ORDER BY (SELECT 1))), @DueDate) AS EndDate
FROM
(VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) AS Months(MonthNumber)
and I got this result
StartDate EndDate
2023-09-17 2023-08-17
2023-08-17 2023-07-17
2023-07-17 2023-06-17
2023-06-17 2023-05-17
2023-05-17 2023-04-17
2023-04-17 2023-03-17
2023-03-17 2023-02-17
2023-02-17 2023-01-17
2023-01-17 2022-12-17
2022-12-17 2022-11-17
2022-11-17 2022-10-17
2022-10-17 2022-09-17
The expected date range should be as follows :-
StartDate | EndDate | |
1 | 17/09/2023 | 01/09/2023 |
2 | 31/08/2023 | 01/08/2023 |
3 | 31/07/2023 | 01/07/2023 |
4 | 30/06/2023 | 01/06/2023 |
5 | 31/05/2023 | 01/05/2023 |
6 | 30/04/2023 | 01/04/2023 |
7 | 31/03/2023 | 01/03/2023 |
8 | 28/02/2023 | 01/02/2023 |
9 | 31/01/2023 | 01/01/2023 |
10 | 31/12/2022 | 01/12/2022 |
11 | 30/11/2022 | 01/11/2022 |
12 | 31/10/2022 | 01/10/2022 |
12+ | 30/09/2022 | All other months |
It's monthly based ageing hence should represent the data in actual calender months.
Thank you!
Try this revised:
DECLARE @DueDate DATE = '2023-09-17'; -- Input Due Date Parameter
-- Generate dynamic aging buckets based on the DueDate
WITH MonthlyPeriods AS (
SELECT
DATEADD(MONTH, -1 * (ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1), @DueDate) AS StartDate,
DATEADD(DAY, -1, DATEADD(MONTH, -1 * (ROW_NUMBER() OVER (ORDER BY (SELECT 1))), @DueDate)) AS EndDate
FROM
(VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) AS Months(MonthNumber)
)
-- Calculate customer balances for each aging bucket and monthly period
SELECT
FORMAT(MonthlyPeriods.StartDate, 'dd/MM/yyyy') AS StartDate,
FORMAT(EndDate, 'dd/MM/yyyy') AS EndDate,
SUM(CASE WHEN DueDate BETWEEN MonthlyPeriods.StartDate AND EndDate THEN Balance ELSE 0 END) AS Balance
FROM
MonthlyPeriods
LEFT JOIN
YourTableNameHere AS CustomerData -- Replace YourTableNameHere with your actual table name
ON
DueDate <= EndDate
GROUP BY
MonthlyPeriods.StartDate,
EndDate
ORDER BY
MonthlyPeriods.StartDate;
ASKER
Hi,
The start date and end date range are not correct. If we can have the two dates correct then the amounts will also be correct. See my example in the last message and compare with the below from your latest script.
StartDate | EndDate |
17/09/2023 | 16/08/2023 |
17/08/2023 | 16/07/2023 |
17/07/2023 | 16/06/2023 |
17/06/2023 | 16/05/2023 |
17/05/2023 | 16/04/2023 |
17/04/2023 | 16/03/2023 |
17/03/2023 | 16/02/2023 |
17/02/2023 | 16/01/2023 |
17/01/2023 | 16/12/2022 |
17/12/2022 | 16/11/2022 |
17/11/2022 | 16/10/2022 |
17/10/2022 | 16/09/2022 |
Try this:
DECLARE @DueDate DATE = '2023-09-17'; -- Input Due Date Parameter
-- Generate dynamic aging buckets based on the DueDate
WITH MonthlyPeriods AS (
SELECT
DATEADD(MONTH, -1 * (ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1), DATEADD(DAY, 1, EOMONTH(@DueDate, -1))) AS StartDate,
DATEADD(MONTH, -1 * (ROW_NUMBER() OVER (ORDER BY (SELECT 1))), EOMONTH(@DueDate, -1)) AS EndDate
FROM
(VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13)) AS Months(MonthNumber)
),
AgingBuckets AS (
SELECT
StartDate,
EndDate,
CASE
WHEN StartDate = MIN(StartDate) OVER () THEN '12+'
ELSE CONVERT(VARCHAR(10), StartDate, 103)
END AS AgingCategory
FROM
MonthlyPeriods
)
-- Calculate customer balances for each aging bucket and monthly period
SELECT
AgingBuckets.AgingCategory,
SUM(CASE WHEN DueDate BETWEEN AgingBuckets.StartDate AND AgingBuckets.EndDate THEN Balance ELSE 0 END) AS Balance
FROM
AgingBuckets
LEFT JOIN
YourTableNameHere AS CustomerData -- Replace YourTableNameHere with your actual table name
ON
DueDate <= AgingBuckets.EndDate
GROUP BY
AgingBuckets.AgingCategory
ORDER BY
AgingBuckets.StartDate;
ASKER
Hi David,
Let me share sample data which you could use on your end to better understand the scenario.
- The problem persists in that the total of the column Balance does not add up to the correct summation total.
- The problem seems to be coming on the 12+ range.
- Sample Data is on the excel file - Customer_Detail - Run the query and the total amount on the balance column should add up to - 5,749,816.39.
- There was an error on the AgeingBuckets.EndDate - I corrected it to be EOMONTH(AgeingBuckets.EndDate) - to get the proper end date of the month.
This part here is the problem
SUM(CASE WHEN CustomerData.DueDate BETWEEN AgingBuckets.StartDate AND EOMONTH (AgingBuckets.EndDate) THEN CustomerData.Balance ELSE 0 END) AS Balance,
Try this query:
Open in new window
Adjust it based on your actual due date and working environment.