Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Grouping Dimension In Query X++ / D365FO

(0) ShareShare
ReportReport
Posted on by 245

Hi Everyone,


I have a requirement to build a Query. 

I have created custom tables, named ConsolidationDetail, and have several fields like MainAccountId, LedgerAccount, LedgerDimension, and TransactionCurrencyAmount.

My problem is in my custom tables don't have a field DimensionAttribute, and only have the LedgerDimension

Here is my query SQL to link my custom table to DimensionAttributeValueCombination

My requirement is,

Summary TransactionCurrencyAmount based on Grouping by MainAccountId, and some dimensions e.g. P1_PatientType and P5_CostCenter.

Regards, 
Rangga.

  • Rangga Pramana Profile Picture
    245 on at
    RE: Grouping Dimension In Query X++ / D365FO

    Hi Girish S.

    It works.

    Thanks a lot.

  • Verified answer
    GirishS Profile Picture
    27,823 Moderator on at
    RE: Grouping Dimension In Query X++ / D365FO

    Okay I understand about separate grouping - Can you try it with AOT query and view?

    Thanks,

    Girish S.

  • Rangga Pramana Profile Picture
    245 on at
    RE: Grouping Dimension In Query X++ / D365FO

    Hi Girish,

    If I don't add another data source for another dimension, How can I group the different fields in one column, is that possible?

    I want the result like this one.

    pastedimage1686217784421v3.png

    If I don't add another table, the result is like this. 

    The value "I" and "O" is patient type and must be in another column, because the grouping must be the combination of CostCenter and PatientType

    Thanks,

    Rangga

  • Suggested answer
    GirishS Profile Picture
    27,823 Moderator on at
    RE: Grouping Dimension In Query X++ / D365FO

    Don't add same DataSource again and again to the query, instead add the range to same QueryBuildDataSource.

    My suggestion will be creating query with all the required tables and adding group by fields - Create a new query with that view - Use that view to get the data as you want. It will be easy for you to troubleshoot, and it will increase performance.

    Thanks,

    Girish S.

  • Rangga Pramana Profile Picture
    245 on at
    RE: Grouping Dimension In Query X++ / D365FO

    Hi Girish S.

    I have created a Query with a table relation between DimensionAttributeLevelValueAllView and my custom table ConsolidationDetail

    public static void main(Args _args)
    {
        Query                   q = new Query();
        QueryBuildDataSource    qbds, qbdsCostCenter, qbdsPatientType;
        QueryBuildRange         qbr, qbrCostCenter, qbrPatientType;
        QueryRun                qr;
    
        TransDate               periodFrom, periodTo;
    
        periodFrom  = mkDate(1, 3, 2023);
        periodTo    = mkDate(31, 3, 2023);
    
        ConsolidationDetail                 consolDetail;
        DimensionAttributeLevelValueAllView dimAttrValueAllView_CostCenter, dimAttrValueAllView_PatientType;
    
        DimensionAttribute              dimAttr_CostCenter = DimensionAttribute::findByName('P1_PatientType');
        DimensionAttribute              dimAttr_PatientTyoe = DimensionAttribute::findByName('P5_CostCenter');
    
    
        qbds = q.addDataSource(tablenum(ConsolidationDetail));
        qbds.addGroupByField(fieldNum(ConsolidationDetail, SubledgerVoucherDataAreaId));
        qbds.addGroupByField(fieldNum(ConsolidationDetail, MainAccountId));
        qbds.addGroupByField(fieldNum(ConsolidationDetail, MainAccountName));
        qbds.addGroupByField(fieldNum(ConsolidationDetail, MainAccountType));
        qbds.addGroupByField(fieldNum(ConsolidationDetail, PostingLayer));
    
        qbr = qbds.addRange(fieldNum(ConsolidationDetail, SubledgerVoucherDataAreaId));
        qbr.value(curExt());
        qbr = qbds.addRange(fieldNum(ConsolidationDetail, MainAccountType));
        qbr.value(strFmt('((%1.%2 == %3) || (%1.%2 == %4) || (%1.%2 == %5))',
                qbds.name(),
                fieldStr(ConsolidationDetail, MainAccountType),
                any2Int(DimensionLedgerAccountType::ProfitAndLoss),
                any2Int(DimensionLedgerAccountType::Revenue),
                any2Int(DimensionLedgerAccountType::Expense)
                ));
        qbr = qbds.addRange(fieldNum(ConsolidationDetail, AccountingDate));
        qbr.value(queryRange(periodFrom, periodTo));
    
        qbr = qbds.addRange(fieldNum(ConsolidationDetail, PostingLayer));
        qbr.value(queryValue(CurrentOperationsTax::Current));
    
        qbdsCostCenter = qbds.addDataSource(tablenum(DimensionAttributeLevelValueAllView), 'CostCenter');
        qbdsCostCenter.addGroupByField(fieldNum(DimensionAttributeLevelValueAllView, DisplayValue));
        qbdsCostCenter.addLink(fieldNum(ConsolidationDetail, LedgerDimension), fieldNum(DimensionAttributeLevelValueAllView, ValueCombinationRecId));
    
        qbrCostCenter = qbdsCostCenter.addRange(fieldNum(DimensionAttributeLevelValueAllView, DimensionAttribute));
        qbrCostCenter.value(queryValue(dimAttr_CostCenter.RecId));
    
        qbdsPatientType = qbds.addDataSource(tablenum(DimensionAttributeLevelValueAllView), 'PatientType');
        qbdsPatientType.addGroupByField(fieldNum(DimensionAttributeLevelValueAllView, DisplayValue));
        qbdsPatientType.addLink(fieldNum(ConsolidationDetail, LedgerDimension), fieldNum(DimensionAttributeLevelValueAllView, ValueCombinationRecId));
    
        qbrPatientType = qbdsPatientType.addRange(fieldNum(DimensionAttributeLevelValueAllView, DimensionAttribute));
        qbrPatientType.value(queryValue(dimAttr_PatientTyoe.RecId));
    
        qr = new QueryRun(q);
    
        while (qr.next())
        {
            consolDetail        = qr.get(tablenum(ConsolidationDetail));
            dimAttrValueAllView_CostCenter  = qr.getNo(2);
            dimAttrValueAllView_PatientType = qr.getNo(3);
    
            Info(strFmt('Main account: %1 (%2) Cost-center[%3] Patient-type[%4] - %5', consolDetail.MainAccountId, consolDetail.MainAccountName, dimAttrValueAllView_CostCenter.DisplayValue, dimAttrValueAllView_PatientType.DisplayValue, consolDetail.SubledgerVoucherDataAreaId));
        }
    
    
    
    }

    When I used the field just P5_CostCenter for grouping, it works.

    But, when I add another dimension e.g. P1_PatientType, it not work. 

    Can you help me to review my code? 



    Thanks, 

    Rangga

  • GirishS Profile Picture
    27,823 Moderator on at
    RE: Grouping Dimension In Query X++ / D365FO

    If you want to get Dimension attribute from DimensionAttributeValueCombination, refer to the view named "DimensionAttributeLevelValueAllView ".

    Thanks,

    Girish S.

  • Rangga Pramana Profile Picture
    245 on at
    RE: Grouping Dimension In Query X++ / D365FO

    Hi Girish S.

    The origin table field is like this,

    There is no field like CostCenter 

    Thanks,

    Rangga

  • GirishS Profile Picture
    27,823 Moderator on at
    RE: Grouping Dimension In Query X++ / D365FO

    Refer to the view DefaultDimensionView - There will be a field named Name where you can find dimension names like cost center etc. open the table browser and see if it helps.

    Thanks,

    Girish S.

  • Rangga Pramana Profile Picture
    245 on at
    RE: Grouping Dimension In Query X++ / D365FO

    Hi Girish S.
    Thank you for helping me. 

    I tried to create a query in X++, but there is no field dimension attribute in DimensionAttributeValueCombination e.g. P1_PatientType or P5_CostCenter.

    Any idea? 

    Thanks,

    Rangga

  • Suggested answer
    GirishS Profile Picture
    27,823 Moderator on at
    RE: Grouping Dimension In Query X++ / D365FO

    Hi Ranga,

    You already linked DimensionAttributeValueCombination table with LedgerDimension.

    Just try to create a query using the joins you added in SQL - Add the grouping of fields in the query - Create a view from the query and add the TransactionCurrencyAmount with Aggregate property set to Sum on the field properties.

    Thanks,

    Girish S.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans