This article is relevant if you are building a NetSuite Saved Search and need to reference values from your dataset’s previous or next-row result.
Background
When working with NetSuite search results, there are cases where we need to reference values from another row. Depending on how the data is sorted, patterns may emerge in adjacent rows that contain helpful information.
NetSuite does not provide a built-in way to retrieve these values, but an undocumented method exists to achieve this. The key lies in understanding Oracle’s underlying PL/SQL Analytic Functions, specifically `LAG` and `LEAD`. These functions allow you to access data from previous or next rows within a dataset, enabling more advanced reporting and analysis.
Simple Ordering and Output Example
To illustrate how LAG and LEAD work, click the image to view a saved search that outputs transactions ordered by Internal ID. This example highlights how to reference the Previous and Next document numbers within the dataset.
The key to this search is understanding the following formula syntax:
LAG(column_name, offset, default_value) OVER (ORDER BY ordering_column)
Retrieves a value from a previous row in the result set.
LEAD(column_name, offset, default_value) OVER (ORDER BY ordering_column)
Retrieves a value from a following row in the result set.
Explanation of Function Parameters
- Offset: specifies how many rows to look back (for LAG) or ahead (for LEAD).
- Default Value (optional): defines what to return if no previous/next row exists.
- Ordering Column: determines the sequence of row evaluation, which can differ from the standard saved search sorting, allowing insights from a different perspective.
Click the image to see the example in action.
Business Case: Using LAG to Track Payment Intervals
To demonstrate the use of this undocumented capability, consider a scenario where we need to calculate the number of days between a client’s payments. This interval helps assess the frequency and velocity of payments, which is valuable for spotting irregular payment patterns in Accounts Receivable practices. A shift in timing may signal potential concerns that require follow-up.
- The search is ordered by most recent payments first.
- Using LAG, retrieve the date of each client’s previous payment.
- A simple numeric formula calculates the difference in days between payments.
- Saved Search highlighting flags cases with a payment interval for more than 14 days or 30 days, signaling the Accounts Receivable team to take action.
Click the images to see the saved search setup and the results in action.
Cumulative Tally and Summary Information
These formulas work only on detail rows and do not function when using the Saved Search Summary options (you will get an error). This is understandable because these PL SQL analytical queries are not designed to be used in fundamental SQL-based Group By queries.
However, in many cases, we need a similar approach to cumulatively tally values across rows. This challenge was addressed over a decade ago in my article: Solving the NetSuite Cumulative Saved Search Tally Challenge. That technique uses a function similar to LAG but tricks the NetSuite system to allow the SUM analytical function to run.
Business Use Cases for NetSuite Previous and Next Row Lookups
We often need to reference previous or next rows in a dataset to fully understand transaction sequences. Here are some scenarios where this approach is essential:
Change Detection
- Date or Status Shifts: if a record’s effective date jumps from January 10th to January 12th, we may need to investigate what happened on January 11th. This helps identify missing system updates or backdated transactions.
- Field Value Transitions: when a status changes (e.g., Pending → Approved), tracking how long the record remained in its prior status can improve workflow visibility.
Transaction Gaps or Overlaps
Consider a time interval analysis, which may be critical in subscription billing or shift scheduling. This analysis checks whether previous and next records ensure time periods don’t overlap or have gaps due to entry errors.
Sequential Step Checks
Think about workflow progression in a manufacturing process that needs to follow a strict Step 1 → Step 2 → Step 3 order. Looking at previous rows ensures that no steps were skipped or reordered.
Pairwise Comparisons
- Current vs. Prior Values: as detailed in my article, Explain NetSuite Inventory Quantity, Value and Average Cost Over Time, tracking daily inventory levels helps detect unexpected fluctuations by comparing an item’s quantity to its previous day’s value.
- Consolidation & Aggregation: if two successive records belong to the same group but only differ in date or quantity, we may need to flag them for review or consolidate them. This concept is covered in my cumulative tally challenge article.
By leveraging previous and next-row lookups, we can track trends, identify anomalies, and compute incremental changes that raw data alone wouldn’t reveal.
Join a NetSuite Team of Experts
Understanding that NetSuite operates on an Oracle database is essential for unlocking advanced solutions to Saved Search challenges. Our team is committed to continuous learning and deepening our expertise, ensuring our clients receive innovative and meaningful solutions.
By working within a team that serves a diverse range of clients and scenarios, one will gain valuable collaboration opportunities and exposure to real-world problem-solving. This dynamic environment fosters growth, enabling ambitious and curious individuals to tackle complex NetSuite challenges confidently.
If you found this article relevant, feel free to sign up for notifications of new articles as I post them. If you would like to join a group of professionals committed to their development and leadership, let’s have a conversation.
Hello, pretty useful thanks a lot!
Would you mind sharing what condition you put into Highlighting tab please?
Trying to make it work for that, but system does not want to accept highlighting condition with LAG() or LEAD().. 🙁
Hello Martin,
Here is the image screenshot of the highlights as requested:

Marty