Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Atttempting to create a matrix of intersecting students - meaning what teachers have how many common students, and eventually being able to see the students. Source is simply 2 columns, although there is an unknown number of teachers and students, so an unknown number of resulting columns and rows:
Teacher A | Student 1 |
Teacher A | Student 2 |
Teacher A | Student 3 |
Teacher A | Student 4 |
Teacher B | Student 1 |
Teacher B | Student 5 |
Teacher B | Student 7 |
Teacher B | Student 8 |
Teacher C | Student 3 |
Teacher C | Student 4 |
Teacher C | Student 7 |
Teacher C | Student 8 |
Teacher D | Student 5 |
Teacher D | Student 9 |
Teacher D | Student 10 |
Desired result is:
Teacher A | Teacher B | Teacher C | Teacher D | |
Teacher A | 4 | 1 | 2 | 0 |
Teacher B | 1 | 4 | 2 | 1 |
Teacher C | 2 | 2 | 4 | 0 |
Teacher D | 0 | 1 | 0 | 3 |
I think with a combination of grouping and poivoting I could get there with a static name and number of teachers, but I am stumped as to handle the variables.
Solved! Go to Solution.
Here is another way.
First create a new Table from the Modelling Tab>>New Table
Table = ALL ( Table1[Teachers] )
Now you can use this MEASURE in your original Table (say Table1)
Measure = VAR mytable = UNION ( VALUES ( Table1[Teachers] ), VALUES ( 'Table'[Teachers] ) ) VAR myrows = COUNTROWS ( DISTINCT ( mytable ) ) VAR M1 = COUNTROWS ( FILTER ( VALUES ( Table1[Students] ), CALCULATE ( COUNT ( Table1[Teachers] ), Table1[Teachers] IN mytable ) = 2 ) ) VAR M2 = COUNT ( Table1[Students] ) VAR M3 = IF ( myrows = 1, M1, M2 ) RETURN IF ( ISBLANK ( M3 ), 0, M3 )
@DThayer - I published the final version of this to the Quick Measure gallery here:
https://community.powerbi.com/t5/Quick-Measures-Gallery/In-Common/td-p/382956
It has the PBIX and a longer explanation.
Power Query solution:
Table Students:
TeacherStudent
Teacher A | Student 1 |
Teacher A | Student 2 |
Teacher A | Student 3 |
Teacher A | Student 4 |
Teacher B | Student 1 |
Teacher B | Student 5 |
Teacher B | Student 7 |
Teacher B | Student 8 |
Teacher C | Student 3 |
Teacher C | Student 4 |
Teacher C | Student 7 |
Teacher C | Student 8 |
Teacher D | Student 5 |
Teacher D | Student 9 |
Teacher D | Student 10 |
Create 2 tables of distinct teachers with a second column of "1" to join the tables together:
Students(2):
TeacherCustom
Teacher A | 1 |
Teacher B | 1 |
Teacher C | 1 |
Teacher D | 1 |
Students(3):
TeacherCustom
Teacher A | 1 |
Teacher B | 1 |
Teacher C | 1 |
Teacher D | 1 |
Now merge the 2 new tables, creating a list of all possible combinations of teacher relationships. Next, merge the newly created list back to the "Students" - twice - once on column "Teacher" and then on column "Teacher.1". Add a column to denote if the students match, then filter out non-matches. Group the data by "Teacher" and "Teacher.1" to get the count, then simply pivot the data around "Teacher.1". Here is the actual code:
let
Source = Table.NestedJoin(#"Students (3)",{"Custom"},#"Students (2)",{"Custom"},"Students (2)",JoinKind.LeftOuter),
#"Expanded Students (2)" = Table.ExpandTableColumn(Source, "Students (2)", {"Teacher"}, {"Teacher.1"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Students (2)",{"Custom"}),
#"Merged Queries" = Table.NestedJoin(#"Removed Columns",{"Teacher"},Students,{"Teacher"},"Students",JoinKind.LeftOuter),
#"Expanded Students" = Table.ExpandTableColumn(#"Merged Queries", "Students", {"Student"}, {"Student"}),
#"Merged Queries1" = Table.NestedJoin(#"Expanded Students",{"Teacher.1"},Students,{"Teacher"},"Students",JoinKind.LeftOuter),
#"Expanded Students1" = Table.ExpandTableColumn(#"Merged Queries1", "Students", {"Student"}, {"Student.1"}),
#"Added Custom" = Table.AddColumn(#"Expanded Students1", "Match", each if [Student] = [Student.1] then 1 else 0),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Match] = 1)),
#"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Student", "Student.1"}),
#"Grouped Rows" = Table.Group(#"Removed Columns1", {"Teacher", "Teacher.1"}, {{"Count", each Table.RowCount(_), type number}}),
#"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Teacher.1]), "Teacher.1", "Count", List.Sum)
in
#"Pivoted Column"
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
111 | |
94 | |
83 | |
66 | |
59 |
User | Count |
---|---|
151 | |
121 | |
104 | |
87 | |
67 |