Published on September 13, 2022.
Over the past several months, I've been working on a custom Business Intelligence project for one of my NetSuite clients. The project has involved developing all sorts of interesting SuiteQL queries.
For one of the project's KPIs, I needed a way to identify all of the Accounting Periods that are applicable based on the current date, including the current fiscal year, quarter, and month.
It turns out that this is very easy to do, and simply requires a self-join on the AccountingPeriod table. Here's what that query looks like.
SELECT AccountingPeriod.ID, AccountingPeriod.PeriodName, ParentPeriod.ID AS ParentPeriodID, ParentPeriod.PeriodName AS ParentPeriodName, AccountingPeriod.StartDate, AccountingPeriod.EndDate, AccountingPeriod.IsYear, AccountingPeriod.IsQuarter, AccountingPeriod.IsPosting, AccountingPeriod.IsAdjust, AccountingPeriod.AllLocked, AccountingPeriod.ARLocked, AccountingPeriod.APLocked, AccountingPeriod.AllowNonGLChanges, AccountingPeriod.LastModifiedDate, AccountingPeriod.Closed, AccountingPeriod.ClosedOnDate FROM AccountingPeriod LEFT OUTER JOIN AccountingPeriod AS ParentPeriod ON ( ParentPeriod.ID = AccountingPeriod.Parent ) WHERE ( AccountingPeriod.IsInactive = 'F' ) AND ( SYSDATE BETWEEN AccountingPeriod.StartDate AND AccountingPeriod.EndDate ) ORDER BY AccountingPeriod.IsYear DESC, AccountingPeriod.IsQuarter DESC
And here are the results.
Click the image to view a larger version.
As you can see, AccountingPeriod records that represent the fiscal year, the current quarter, and the current month are returned.
By using a LEFT OUTER JOIN, the record representing the Fiscal Year is included in the results. Note that it does not have a parent period.
The ORDER BY clause is somewhat interesting. By sorting in this way, the applicable Accounting Periods are returned in order based on the current fiscal year first, followed by the quarter, and then the month.
If you'd like to limit the results to