web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Filtering dataitems in a query before joining

(4) ShareShare
ReportReport
Posted on by 112
Hello community,

I'm currently working on a query, which includes two FullOuterJoins.
(FYI: I'm summing up orders and transfers for locations on item variant level)

As you might guess, this query has quite some rows and it takes some time to calculate it. So it would be great to put filters on each dataitem before joining them. But as far as I know ColumnFilter as well as DataItemTableFilter are applied to the final table. Is that correct? If so, is there any way to apply filters before they are joined? 

Thanks in advance for some input!
I have the same question (0)
  • Sagar Dangar, MCP Profile Picture
    799 Super User 2025 Season 2 on at
    Hi,
     
    Yes, right. it takes all rows.
     
    Another way is by creating Custom temporary buffer tables and insert filtered required data only and take buffer table as dataitem
     
    write query like below:
     
    query 50100 CustomQuery
    {
        QueryType = API;
        APIPublisher = 'Sagar';
        APIGroup = 'Dangar';
        EntityName = 'CustomQuerys';
        EntitySetName = 'CustomQuery';
        elements
        {
            dataitem(SalesHeaderTemp; "Custom Sales Header Temp")
            {
                column(No_; "No.")
                {
                }
            }
        }
        trigger OnBeforeOpen()
        var
            SalesHeaderL: Record "Sales Header";
            TempSalesHeaderL: Record "Custom Sales Header Temp";
        begin
            SalesHeaderL.Reset();
            SalesHeaderL.SetRange("Document Type", SalesHeaderL."Document Type"::Order);
            if SalesHeaderL.Findset() then begin
                TempSalesHeaderL.init();
                TempSalesHeaderL.TransferFields(SalesHeaderL);
                TempSalesHeaderL.Insert();
            end;
        end;
    }
  • Chris Brill Profile Picture
    112 on at
    Hi!

    Thanks for confirmation and the suggested alternative.
    I'm gonna give it a try! :-)
  • Suggested answer
    YUN ZHU Profile Picture
    95,739 Super User 2025 Season 2 on at
  • Chris Brill Profile Picture
    112 on at
    @Yun Zhu:
    Thanks, I've looked on that page before. I do find the description "Filter directly on a data item in query object" for the DataItemTableFilter quite misleading though. For me it implies that this just filters the dataitem and not the whole query and therefore is applied before two dataitems are joined. 
    This is really a feature I'm missing in AL, especially as it available in SQL.

    @Sagar Dangar:
    I tried out your approach today, but I can't get it to work.
    The transferFields and Insert in the OnBefore - trigger works, but the query is empty.

    My assumption was, that the Record "Custom Sales Header Temp" in your code refers to a temporary table that just includes the fields that are needed. Is that correct?
    Any idea, how I can make the code work?
     
    query 50100 "Orders Query"
    {
        QueryType = Normal;
     
        elements
        {
            dataitem(PurchaseLine; "Temp Purchase Line")
            {
                column(Location_Code; "Location Code")
                { }
                column(No_; "No.")
                { }
                column(Variant_Code; "Variant Code")
                { }
            }
        }
     
        trigger OnBeforeOpen()
        var
            PurchaseLine_L: Record "Purchase Line";
            WTTempPurchaseLine_L: Record "WT Temp Purchase Line";
        begin
            PurchaseLine_L.Reset();
            PurchaseLine_L.SetRange("Location Code", '0110');
            PurchaseLine_L.SetRange("Document Type", PurchaseLine_L."Document Type"::Order);
            PurchaseLine_L.LoadFields("Document Type", "Location Code", "No.", "Variant Code");
            if PurchaseLine_L.FindSet() then
                repeat
                    WTTempPurchaseLine_L.Init();
                    WTTempPurchaseLine_L.TransferFields(PurchaseLine_L);
                    if WTTempPurchaseLine_L.Insert() then;
                until PurchaseLine_L.Next() = 0;
        end;
     
  • Sohail Ahmed Profile Picture
    11,150 Super User 2025 Season 2 on at

    This might be helpful for resolving your issue.

    You're right, ColumnFilter and DataItemTableFilter in Business Central queries apply after joins, causing performance issues with large datasets. To filter before a FullOuterJoin, the best approach is to:


    1.  

      Use Temporary Tables:

       

      • Create temporary tables for each dataset you want to join (e.g., orders and transfers).

      • Populate these temporary tables with only the filtered records by applying your conditions in the OnAfterGetRecord trigger of the original dataitems.

      • Then, perform the FullOuterJoin on these smaller temporary tables.


      •  

    2.  

      Consider Views (for static filters):

       

      • If your filters are predefined, create SQL views with the filtering logic and use these views as your DataItem sources.


      •  


    3.  

    Applying filters within the OnAfterGetRecord of the original dataitems using CurrQry.Skip() is less efficient as the data is still fetched initially.

    For your case, temporary tables are the recommended way to filter order and transfer data before the FullOuterJoin to improve query performance.

    Did this help you? Mark this answer as verified if it did!

  • Chris Brill Profile Picture
    112 on at
    Hello Sohail Ahmed, 

    I'm a little confused: As far as I can see, Query-DataItems just have the OnBeforeOpen-Trigger. What OnAfterGetRecord-Trigger do you mean? 
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,614 Super User 2025 Season 2 on at

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,135

#2
YUN ZHU Profile Picture

YUN ZHU 733 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 612

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans