cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
arturos
Frequent Visitor

Create a measure to sum unique values from another column and group by ID

I am working with data from a video chat app. Some entries are duplicated because they correspond to different events within a single call. The data looks as follows:

account_idcall_ideventduration
42abcdefCallStarted22
42abcdefCallEnded22
45abcdefCallStarted21
45abcdefCallEnded21
99ghijklmCallReceived13
99ghijklmCallAnswered13
99ghijklmCallEnded13
13nopqrsCallConnected39
13tuvwxyCallAccepted15
13tuvwxyCallEnded15

I need to get the sum of `duration` for each `account_id` in the table. However, I need to account for only one row per `account_id` and `call_id` combination.

I would like to get the following result from the sample data I posted above:

account_idsum_duration
42

22

4521
9913
1344

How can I achieve this?

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@arturos Try this:

Measure = 
  VAR __Table = 
    GROUPBY(
      'Table',
      [account_id],
      [call_id],
      "__Sum",SUMX(CURRENTGROUP(),[duration])
    )
  VAR __Result = SUMX(__Table,[__Sum])
RETURN
  __Result

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

2 REPLIES 2
Greg_Deckler
Super User
Super User

@arturos Try this:

Measure = 
  VAR __Table = 
    GROUPBY(
      'Table',
      [account_id],
      [call_id],
      "__Sum",SUMX(CURRENTGROUP(),[duration])
    )
  VAR __Result = SUMX(__Table,[__Sum])
RETURN
  __Result

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

In the end, I opted for importing the data using `ROW_NUMBER()` directly in the SQL query to only sum the first row per combination.
However, your solution works too, so thank you!

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

Thank you 2022 Review

2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.