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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
datajunkie_29
Helper I
Helper I

DAX Aggregate by multiple fields

Hello all,

 

I am fairly new to power bi and have come across a road blocker while creating a measure. I'm trying to aggregate the measure based on several fields from different but related tables.

 

Dummy Data Model:

datajunkie_29_0-1683682327254.png

 

Measures Calculated :

Step1-------------

MarginAmt_Y = CALCULATE(SUM(MarginAmt), FILTER(QL_Flag = "Y")

MarginAmt_N = CALCULATE(SUM(MarginAmt), FILTER(QL_Flag = "N")

 

NetAmt_Y = CALCULATE(SUM(NetAmt), FILTER(QL_Flag = "Y")

NetAmt_Y = CALCULATE(SUM(NetAmt), FILTER(QL_Flag = "N")

 

QtyAmt_Y = CALCULATE(SUM(Qty), FILTER(QL_Flag = "Y")

Qty_Y = CALCULATE(SUM(Qty), FILTER(QL_Flag = "N")

 

Step2 ---------------------

Y_Margin/QTY = DIVIDE([NetAmt_Y] - [MarginAmt_Y], [Qty_Y])

N_Margin/QTY = DIVIDE([NetAmt_N] - [MarginAmt_N], [Qty_N])

 

Step3 -----------------------

FinalMeasure = 

IF (
    OR([Y_Margin/QTY] = BLANK (), [N_Margin/QTY] = BLANK ()),
    0,
    IF (
        [Y_Margin/QTY] < [N_Margin/QTY],
        ( ([N_Margin/QTY] - [Y_Margin/QTY]) * [Qty_N]),
        0
    )
)

 

However, the final measure is returning a zero, as it is calculating at overall level. How do I agrregate it by CustId, BranchId, ItemId

something like: 

Measure = SUM(Aggr(

IF (
    OR([Y_Margin/QTY] = BLANK (), [N_Margin/QTY] = BLANK ()),
    0,
    IF (
        [Y_Margin/QTY] < [N_Margin/QTY],
        ( ([N_Margin/QTY] - [Y_Margin/QTY]) * [Qty_N]),
        0
    )),BranchId, custid, itemid))


Desired Output:

BranchIdCustIdFinalMeasure
701 $ 234.00
652 $ 156.00
653 $ 786.00
541 $ 156.00



Any help is appreciated! Thank you

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You can use something like

My Measure =
SUMX (
    SUMMARIZE ( Sales, Sales[BranchId], Sales[custid], Sales[itemid] ),
    IF (
        OR ( [Y_Margin/QTY] = BLANK (), [N_Margin/QTY] = BLANK () ),
        0,
        IF (
            [Y_Margin/QTY] < [N_Margin/QTY],
            ( ( [N_Margin/QTY] - [Y_Margin/QTY] ) * [Qty_N] ),
            0
        )
    )
)

View solution in original post

2 REPLIES 2
datajunkie_29
Helper I
Helper I

Hi @johnt75 , thank you for your response, the solution worked 🙂

johnt75
Super User
Super User

You can use something like

My Measure =
SUMX (
    SUMMARIZE ( Sales, Sales[BranchId], Sales[custid], Sales[itemid] ),
    IF (
        OR ( [Y_Margin/QTY] = BLANK (), [N_Margin/QTY] = BLANK () ),
        0,
        IF (
            [Y_Margin/QTY] < [N_Margin/QTY],
            ( ( [N_Margin/QTY] - [Y_Margin/QTY] ) * [Qty_N] ),
            0
        )
    )
)

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.