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 :
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.

I have the same question (0)
  • Suggested answer
    GirishS Profile Picture
    27,827 Moderator on at

    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.

  • Rangga Pramana Profile Picture
    245 on at

    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

  • GirishS Profile Picture
    27,827 Moderator on at

    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

    Hi Girish S.

    The origin table field is like this,

    There is no field like CostCenter 

    Thanks,

    Rangga

  • GirishS Profile Picture
    27,827 Moderator on at

    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

    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

  • Suggested answer
    GirishS Profile Picture
    27,827 Moderator on at

    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

    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

  • Verified answer
    GirishS Profile Picture
    27,827 Moderator on at

    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

    Hi Girish S.

    It works.

    Thanks a lot.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 422 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans