PowerBI技术问询:如何统计不同课程中的学生人数(去重场景)
Hey there! Let's work through this Power BI problem together—you need to count unique students, right? The key here is making sure someone like Ahmad (who’s taking two courses) only gets counted once, even though they show up twice in your table.
First, let’s map out your example data to make sure we’re on the same page: your table probably looks something like this:
| Name | Course | Attendance |
|---|---|---|
| Ahmad | Math | Present |
| Ahmad | English | Present |
| Sarah | Math | Present |
| John | Science | Present |
And you want the total unique student count to be 3, not 4. Here are the easiest ways to pull this off in Power BI:
If all you need is a single number showing how many unique students you have, create a new Measure with this DAX code:
Unique Student Count = DISTINCTCOUNT('YourTable'[name])
Drop this into a Card visual, and it’ll instantly show 3 for your data—DISTINCTCOUNT automatically ignores duplicate names, so Ahmad only gets counted once.
If you want to see how many students are in each course, but also have a total that doesn’t double-count students in multiple courses, use this more flexible measure:
Unique Students per Course = VAR CurrentCourse = SELECTEDVALUE('YourTable'[course]) RETURN IF( ISFILTERED('YourTable'[course]), COUNTROWS(DISTINCT(SELECTCOLUMNS(FILTER('YourTable', 'YourTable'[course] = CurrentCourse), "StudentName", [name]))), DISTINCTCOUNT('YourTable'[name]) )
Here’s what this does:
- When you look at a specific course (like Math), it counts the unique students in that course (Ahmad + Sarah = 2)
- When you view the overall total (no course filter), it shows the deduplicated count of 3 students
If you want to manually filter out duplicate student entries later, add a Calculated Column:
Is First Enrollment = VAR StudentName = [name] VAR FirstCourse = MINX(FILTER('YourTable', [name] = StudentName), [course]) RETURN IF([course] = FirstCourse, TRUE(), FALSE())
This marks the first course each student took as TRUE—you can then filter your visuals to only include these rows if you want to avoid duplicates in tables or charts.
Pro Tips:
- Double-check your
namecolumn for typos (like "Ahmad" vs "ahmad")—small inconsistencies will makeDISTINCTCOUNTtreat them as different students. - If you only want to count students who attended, add a filter to your measure or visual for
attendance = "Present"(adjust the value to match your data’s attendance label).
内容的提问来源于stack exchange,提问作者hanan alharbi

