Forum Discussion

hnb_tor's avatar
hnb_tor
Frequent Visitor
1 year ago
Solved

calculated column to aggregate based on two columns

 

Hi ,

 

I'm looking for some help in writing DAX to create a Calculated column with the below requirements - 

 

Below table contains Customer, Department, Region and number of Orders. 

I want to create the Calculated Column as shown below, which has the sum of Orders of each customer in each Region (ignoring the Deparment) 

 

Total number of Orders = Group (customer) + Group (Region) 

 

CustomerDepartmentRegionOrdersCALCULATED COLUMN
AAASalesAsia02
AAAMarketingAsia22
AAAR&DEurope55
BBBSalesAmericas00
BBBProcurementAmericas00
BBBResearchEurope33
CCCSalesAmericas511
CCCMarketingAmericas611
CCCHRAsia1024
CCCFinanceAsia1424

 

Appreciate any help on writing the DAX for this, thank you. 

  • Hello hnb_tor , 

     

    Please try the below formula its more simpler..

     

    CALCULATED COLUMN =
    CALCULATE(SUM('Table'[Orders]),ALLEXCEPT('Table', 'Table'[Customer], 'Table'[Region]))

     

     

    If you find this helpful , please mark it as solution which will be helpful for others and Your Kudos/Likes are much appreciated!

     

    Thank You

    Dharmendar S

    LinkedIN 

     

7 Replies

  • dharmendars007's avatar
    dharmendars007
    Memorable Member

    Hello hnb_tor , 

     

    Please try the below formula its more simpler..

     

    CALCULATED COLUMN =
    CALCULATE(SUM('Table'[Orders]),ALLEXCEPT('Table', 'Table'[Customer], 'Table'[Region]))

     

     

    If you find this helpful , please mark it as solution which will be helpful for others and Your Kudos/Likes are much appreciated!

     

    Thank You

    Dharmendar S

    LinkedIN 

     

    • hnb_tor's avatar
      hnb_tor
      Frequent Visitor

      thanks so much, this solution worked! 

  • Hi,

    Please check the below picture and the attached pbix file.

    One of ways to achieve this is using WINDOW DAX Function.

     

     

    WINDOW function (DAX) - DAX | Microsoft Learn

     

    Expected result CC =
    SUMX (
        WINDOW (
            1,
            ABS,
            -1,
            ABS,
            Data,
            ,
            ,
            PARTITIONBY ( Data[Customer], Data[Region] ),
            MATCHBY ( Data[Customer], Data[Department], Data[Region] )
        ),
        Data[Orders]
    )
    

     

     

    • hnb_tor's avatar
      hnb_tor
      Frequent Visitor

      Hi, 

       

      Thank you for looking into this request. I'm trying to follow the DAX example, however I see an error "Failed to resolve name 'MATCHBY', it is not a valid table, variable, or function name" 

      Could it be that the PowerBI desktop version is old? 

      • Jihwan_Kim's avatar
        Jihwan_Kim
        Super User

        Hi,

        Thank you for your message, and another option to achieve this is writing DAX formula for calculated column something like below. Please also check the attache pbix file.

        I am not sure how you wrote DAX formula in your sample on your computer, but please check if MATCHBY dax function works in my sample on your computer.

         

        Expected result CC =
        /* SUMX (
            WINDOW (
                1,
                ABS,
                -1,
                ABS,
                Data,
                ,
                ,
                PARTITIONBY ( Data[Customer], Data[Region] ),
                MATCHBY ( Data[Customer], Data[Department], Data[Region] )
            ),
            Data[Orders]
        ) */
        VAR _currentrowcustomer = Data[Customer]
        VAR _currentrowregion = Data[Region]
        RETURN
            SUMX (
                FILTER (
                    Data,
                    Data[Customer] = _currentrowcustomer
                        && Data[Region] = _currentrowregion
                ),
                Data[Orders]
            )