Hello,
I'm have a query that I'm trying to reference the previous row's value to calculate a percentage change. I can't figure it out. Here's my info:
Table: Transactions
| | | Transactions |
| ID | Type | OpenDate |
| --- | --- | --- |
| 774 | S | 01/01/2023 |
| 775 | S | 01/01/2023 |
| 777 | R | 01/01/2023 |
| 778 | S | 02/01/2023 |
| 779 | S | 02/01/2023 |
| 780 | S | 02/01/2023 |
| 782 | R | 02/01/2023 |
| 783 | S | 03/01/2023 |
| 784 | R | 03/01/2023 |
| 785 | S | 03/01/2023 |
| 786 | R | 03/01/2023 |
| 787 | R | 04/01/2023 |
| 788 | S | 04/01/2023 |
| 789 | S | 04/01/2023 |
Query:
SQL:
SELECT Format([OpenDate],"mm-yyyy") AS [Month], Count(Format([Type],"Standard")) AS Total, [PreviousTotal] AS Expr1, Format(([Total]-[PreviousTotal])/[PreviousTotal],"Percent") AS [%Change]
FROM Transactions
GROUP BY Format([OpenDate],"mm-yyyy");
| | | | Query1 |
| Month | Total | PreviousTotal | %Change |
| --- | --- | --- | --- |
| 01-2023 | 3 | | |
| 02-2023 | 4 | 3 | |
| 03-2023 | 4 | 4 | |
| 04-2023 | 3 | 4 | |
OpenDate Field: Formatted to Month and Year and Group it
Type Field: Count it and Format it to Standard so I can use it to calculate %Change
PreviousTotal: Should be the previous month's Total
%Change: ([Total]-[PreviousTotal])/([PreviousTotal])