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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
TomsNg
Helper I
Helper I

How this DAX works

Hi All,

 

I am given a task to categorize item after the amount is summed. I got a solution provided by one of super users 'Jihwan_Kim' from this community. However, I tried but I can't realize how the DAX works. Can someone help to explain this DAX. 

 

TomsNg_1-1694512422606.png

 

*** This is the DAX ***

Sum by Category =

SUMX (
    FILTER (
        VALUES ( Data[Id] ),
        COUNTROWS (
            FILTER (
                Categories,
                CALCULATE ( SUM ( Data[Amount] ) ) >= Categories[Min]
                    && CALCULATE ( SUM ( Data[Amount] ) ) < Categories[Max]
            )
        ) > 0
    ),
    CALCULATE ( SUM ( Data[Amount] ) )
)
 
TomsNg_2-1694513390944.png

this is [data] table

 

TomsNg_3-1694513434990.png

this is [categories] table

 

this is the original post

https://community.fabric.microsoft.com/t5/Desktop/Sum-of-amount-by-id-and-categorize-after-summing/m...

 

Many thanks

 

Toms

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi  @TomsNg ,

You can understand the meaning of the following dax formula in two parts:

1. It filter the Ids which the sum of Amount is between Categories[Min] and Categories[Max]

  FILTER (
        VALUES ( Data[Id] ),
        COUNTROWS (
            FILTER (
                Categories,
                CALCULATE ( SUM ( Data[Amount] ) ) >= Categories[Min]
                    && CALCULATE ( SUM ( Data[Amount] ) ) < Categories[Max]
            )
        ) > 0
    )

2. This is calculating the sum of the Amount for each Id that meets the conditions specified in the filter. The SUMX function is an iterator that calculates the sum row by row.

SUMX (
XXX,
 CALCULATE ( SUM ( Data[Amount] ) )
)

You can refer the following official documentations to get understanding of the involved functions:

FILTER function (DAX) - DAX | Microsoft Learn

VALUES function (DAX) - DAX | Microsoft Learn

COUNTX function (DAX) - DAX | Microsoft Learn

SUMX function (DAX) - DAX | Microsoft Learn

 

In addition, you can create two measures as below to achieve the same requirement. You can find the details in the attachment.

Measure = 
VAR _tab =
    SUMMARIZE ( 'Data', 'Data'[Id], "@amount", SUM ( 'Data'[Amount] ) )
RETURN
    SUMX (
        FILTER (
            _tab,
            [@amount] >= SELECTEDVALUE ( 'Categories'[Min] )
                && [@amount] < SELECTEDVALUE ( 'Categories'[Max] )
        ),
        [@amount]
    )
Sum by category = SUMX(VALUES('Categories'[Category]),[Measure])

vyiruanmsft_0-1694671763390.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yiruan-msft
Community Support
Community Support

Hi  @TomsNg ,

You can understand the meaning of the following dax formula in two parts:

1. It filter the Ids which the sum of Amount is between Categories[Min] and Categories[Max]

  FILTER (
        VALUES ( Data[Id] ),
        COUNTROWS (
            FILTER (
                Categories,
                CALCULATE ( SUM ( Data[Amount] ) ) >= Categories[Min]
                    && CALCULATE ( SUM ( Data[Amount] ) ) < Categories[Max]
            )
        ) > 0
    )

2. This is calculating the sum of the Amount for each Id that meets the conditions specified in the filter. The SUMX function is an iterator that calculates the sum row by row.

SUMX (
XXX,
 CALCULATE ( SUM ( Data[Amount] ) )
)

You can refer the following official documentations to get understanding of the involved functions:

FILTER function (DAX) - DAX | Microsoft Learn

VALUES function (DAX) - DAX | Microsoft Learn

COUNTX function (DAX) - DAX | Microsoft Learn

SUMX function (DAX) - DAX | Microsoft Learn

 

In addition, you can create two measures as below to achieve the same requirement. You can find the details in the attachment.

Measure = 
VAR _tab =
    SUMMARIZE ( 'Data', 'Data'[Id], "@amount", SUM ( 'Data'[Amount] ) )
RETURN
    SUMX (
        FILTER (
            _tab,
            [@amount] >= SELECTEDVALUE ( 'Categories'[Min] )
                && [@amount] < SELECTEDVALUE ( 'Categories'[Max] )
        ),
        [@amount]
    )
Sum by category = SUMX(VALUES('Categories'[Category]),[Measure])

vyiruanmsft_0-1694671763390.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.