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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Count Distinict InvoiceDate

(0) ShareShare
ReportReport
Posted on by

how to Select Count Distinict InvoiceDate ?

Note : the Recid i used it in Select Statement

and group by InvoiceDate

but wrong Result

any other Idea for this Issue. 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Zain Bokhari Profile Picture
    3,208 Moderator on at

    It doesn't work that way in X++. If you use RecId in your select statement it will give you the count of first Group based on the invoiceDate value, not the count of unique date values. (Run it with while select, you'll get the idea).

    You want to count distinct InvoiceDate values, right?

    I don't think the distinct keyword is available in X++. 

    If I understand your problem correctly, you can achieve that by either running direct sql commands.

    Or you can simply use your x++ statement with while select and use a variable as counter to count the iterations that would give you distinct count of Dates, it is not an ideal solution but it will give you what you need. Something like this:

    Table myTable;
    int counter;
    
    While select RecId from myTable
       group by (myTable.Date)
    {
       counter++;
    }
    
    info(strfmt("%1",counter));


  • Walid Gamal Profile Picture
    on at

    Hi Zain

    i tried the idea of sql but did,t run corectlly

    the other idea will work but this is the last idea i will go to do

    is there any other idea to do that .

    thanks,

  • Suggested answer
    Chaitanya Golla Profile Picture
    17,225 on at

    Hi,

    Is there any reason to execute and get the distinct invoice dates only through constructing SQL from Ax?

    I made some small changes to the job given by Zain and it returned intended results for me.

    Please find the following job where job is written on custInvoiceTable.

    Samething is achievable through ax query classes too.

    static void DistinctDate(Args _args)
    {
    CustInvoiceTable custInvoiceTable;
    int counter;

    While select count(RecId), Invoicedate from custInvoiceTable
    group by InvoiceDate
    {
    counter++;
    info(strfmt("%1",custInvoiceTable.InvoiceDate));
    }

    info(strfmt("%1",counter));
    }

  • Suggested answer
    Zain Bokhari Profile Picture
    3,208 Moderator on at

    Chaitanya is right, you can also achieve it via X++ Query and QueryRun classes. Create your query through the Query class and then you can get the count of records(Iterations) from QueryRun object.

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    The typical workaround solution for distinct in AX is to create a View with a group by on your "distinct" field. Then if you want aggregation, you could simply run the count against the created view in a select statement, or add another query/view for the aggregation to consume that like in a FactBox.

  • Walid Gamal Profile Picture
    on at

    Hi all,

    i have the records but can't count the records

    i tried to use countTotal,countLoop but it didn,t work

    is that because i used join in query??

    thanks.

  • Suggested answer
    Zain Bokhari Profile Picture
    3,208 Moderator on at

    Yes. Joined query will return all number of records. Not just the grouping count for you.

    I don't know why exactly you need the count for. But if it is feasible for you then create a view like suggested by Vilmos or if not then simply use the While select and count the iterations.

  • Vilmos Kintera Profile Picture
    46,149 on at

    Explaining or showing what you did might help to identify better what you did wrong.

    Count works as an aggregate function on a field in a query/view.

    Please share your code or AOT object design with detailed explanation.

  • Walid Gamal Profile Picture
    on at
        qbd = query.addDataSource(tablenum(SalesTable));
        qbd.addRange(fieldNum(SalesTable,WorkerSalesResponsible)).value("5637168582");
        qbd2 = qbd.addDataSource(tableNum(CustInvoiceJour));
        qbd2.addGroupByField(fieldNum(CustInvoiceJour,InvoiceDate));
        qbd2.addSelectionField(fieldNum(CustInvoiceJour,InvoiceDate),SelectionField::Count);
        qbd2.relations(true);
    
        queryRun = new QueryRun(query);
        while(queryRun.next())
        {
            custInv = queryRun.get(tableNum(CustInvoiceJour));
            print custInv.InvoiceDate;
        }

    this is my code 

    thanks,

  • Verified answer
    Vilmos Kintera Profile Picture
    46,149 on at

    You cannot do the counting in a single query to get the number of groupped objects like this.

    You need to use a view that does the grouping first, then you can count the number of records in that afterwards.

    That is exactly what Chaitanya trying to demonstrate with the separate total variable for the counting - it will not work in one go like you are trying to do!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Andrew Jones a1x Profile Picture

Andrew Jones a1x 2

#3
Basit Profile Picture

Basit 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans