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

Dax for sum of distinct values

Hi

 

I have a dataset which has a student ID and Course-name. One student takes multiple courses. Below is my dataset sample

sdhilip_0-1603262918249.png

 

I need output as below

sdhilip_1-1603262938754.png

 

How to do the DAX calculation for the above? I checked an existing question on the same but not successful.

 

 

2 ACCEPTED SOLUTIONS

@Anonymous , Try a measure like

sumx(values(Table[student_id]) , calculate(DISTINCTCOUNT(Table[Course])))

 

Please provide your feedback comments and advice for new videos
Tutorial Series Dax Vs SQL Direct Query PBI Tips
Appreciate your Kudos.

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

AlB
Community Champion
Community Champion

@sdhilip

Yes, if you want the total as the sum of the individual rows, you must change the code as already suggested.

In the total row that you want to replicate the behavior to the visual, and to do so you must have all dates in rows, apply the measure to each row, and then sum the result of each row. That's what you do with the new code:

SUMX (
    DISTINCT ( Table1[Student ID] ),
    CALCULATE ( DISTINCTCOUNT ( Table1[Course] ) )
)

Note that CALCULATE needs to trigger the context transition and therefore count courses only for current students.

In any of the other rows in the visual, you have a specific StudentID as the filter context, so DISTINCT() will actually return a single row with that particular StudentID. Then our initial measure is invoked in that row and the SUMX adds only that result, since we only have one row. Therefore, you will get the same result as applying the original measure directly.

Please mark the resolved question when you are finished and consider giving a thumbs up if the posts are useful.

Contact me privately for assistance with any large-scale BI needs, tutoring, etc.

Bless you

SU18_powerbi_badge

View solution in original post

7 REPLIES 7
Ray_Minds
Responsive Resident
Responsive Resident

Hi 

Create measures 1. #MAX MONTLY MARKING EXPENSE = MAX('Date'{Monthly Marking Expense]) 2. #DISTINCT SUM= SUMX(DISTINCT('Date'[Channel Type ]), [MAX MONTLY MARKING EXPENSE ])

If your requirement is solved, please make THIS ANSWER a SOLUTION ✔️ and help other users find the solution quickly. Please hit the LIKE 👍 button if this comment helps you.

AlB
Community Champion
Community Champion

Hi @Anonymous 

1. Place Student ID in the rows of a table visual

2. Create this measure and place it in the visual

Measure = 

DISTINCTCOUNT(Table1[Course])

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

Anonymous
Not applicable

HI @AlB 

 

Thanks

 

It works but the total is not correct

 

sdhilip_0-1603263675304.png

 

AlB
Community Champion
Community Champion

@sdhilip

Yes, if you want the total as the sum of the individual rows, you must change the code as already suggested.

In the total row that you want to replicate the behavior to the visual, and to do so you must have all dates in rows, apply the measure to each row, and then sum the result of each row. That's what you do with the new code:

SUMX (
    DISTINCT ( Table1[Student ID] ),
    CALCULATE ( DISTINCTCOUNT ( Table1[Course] ) )
)

Note that CALCULATE needs to trigger the context transition and therefore count courses only for current students.

In any of the other rows in the visual, you have a specific StudentID as the filter context, so DISTINCT() will actually return a single row with that particular StudentID. Then our initial measure is invoked in that row and the SUMX adds only that result, since we only have one row. Therefore, you will get the same result as applying the original measure directly.

Please mark the resolved question when you are finished and consider giving a thumbs up if the posts are useful.

Contact me privately for assistance with any large-scale BI needs, tutoring, etc.

Bless you

SU18_powerbi_badge

Anonymous
Not applicable

Thanks @AlB for your detailed explanation

@Anonymous , Try a measure like

sumx(values(Table[student_id]) , calculate(DISTINCTCOUNT(Table[Course])))

 

Please provide your feedback comments and advice for new videos
Tutorial Series Dax Vs SQL Direct Query PBI Tips
Appreciate your Kudos.

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Can you generate a similar formula for a calculated column instead of a measure? When I applied this formula to a calculated column duplicate values weren't removed as in the measure?

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!

December 2024

A Year in Review - December 2024

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