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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

calculate the sum in a column (quantity) for just unique values in another column (category)

Hello, I am trying to create a measure in DAX that calculate the sum in a column (quantity) for just unique values in another column (category). The paramenter "quantity" is always the same in the same category.

 

To explain better, a have this table:

 

        category              | quantity

              1                    |        1

              1                    |        1

              1                    |        1

              2                    |        1

              3                    |        2

              3                    |        2

 

 

And I want to have:

 

         category             | quantity

              1                    |        1

              2                    |        1

              3                    |        2

 

SUM = 1+1+2

 

In SQL I obtain that sum by doing this:

 with total as (

select Distinct(category), quantity FROM table.name )

Select SUM(quantity) from total

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

Hi @Anonymous ,

 

Please try this:

Total = 
SUMX (
    SUMMARIZE (
        Table1,
        Table1[category],
        "Distinct", DISTINCT ( Table1[quantity] )
    ),
    [Distinct]
)

v-xuding-msft_0-1598876366164.png

 

Best Regards,
Xue Ding
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

3 REPLIES 3
v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please try this:

Total = 
SUMX (
    SUMMARIZE (
        Table1,
        Table1[category],
        "Distinct", DISTINCT ( Table1[quantity] )
    ),
    [Distinct]
)

v-xuding-msft_0-1598876366164.png

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
edhans
Super User
Super User

You could do this in one step, but I split steps out so the logic is easier to see. This will return 4 from your sample data if you drop this measure in a card:

Grand Total = 
VAR varUniqueRecords =
    DISTINCT( Data )
VAR Result =
    SUMX(
        varUniqueRecords,
        [quantity]
    )
RETURN
    Result

edhans_0-1598552676990.png

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
lbendlin
Super User
Super User

All you need to do is remove duplicate rows in your first table.

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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.