Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
hnb_tor
Regular Visitor

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. 

1 ACCEPTED SOLUTION
dharmendars007
Memorable Member
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]))

 

dharmendars007_0-1726910822193.png

 

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 

 

View solution in original post

7 REPLIES 7
dharmendars007
Memorable Member
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]))

 

dharmendars007_0-1726910822193.png

 

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 

 

thanks so much, this solution worked! 

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

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

 

Jihwan_Kim_0-1726801687331.png

 

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]
)

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

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? 

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]
    )

  

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Hi, Thanks again for helping on this request. 

I cant open the pbix file you have shared, there is a message that my PowerBI Desktop version is not compatiable with some of the features on this file. 
I'm going to see if I can get PowerBI desktop client upgraded on my computer and test if I can try your solution. Thanks, 

Sorry, I havent yet upgraded the version, will upgrade and test soon. 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.