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

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.

Reply
DThayer
Frequent Visitor

Teachers with Students in common

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 AStudent 1
Teacher AStudent 2
Teacher AStudent 3
Teacher AStudent 4
Teacher BStudent 1
Teacher BStudent 5
Teacher BStudent 7
Teacher BStudent 8
Teacher CStudent 3
Teacher CStudent 4
Teacher CStudent 7
Teacher CStudent 8
Teacher DStudent 5
Teacher DStudent 9
Teacher DStudent 10

 

 

Desired result is:

 Teacher ATeacher BTeacher CTeacher D
Teacher A4120
Teacher B1421
Teacher C2240
Teacher D0103

 

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.

3 ACCEPTED SOLUTIONS

@DThayer

 

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 )




Regards
Zubair

Please try my custom visuals

View solution in original post

Greg_Deckler
Super User
Super User

@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.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

Power Query solution:

Table Students:

 

TeacherStudent

Teacher AStudent 1
Teacher AStudent 2
Teacher AStudent 3
Teacher AStudent 4
Teacher BStudent 1
Teacher BStudent 5
Teacher BStudent 7
Teacher BStudent 8
Teacher CStudent 3
Teacher CStudent 4
Teacher CStudent 7
Teacher CStudent 8
Teacher DStudent 5
Teacher DStudent 9
Teacher DStudent 10

 

Create 2 tables of distinct teachers with a second column of "1" to join the tables together:

Students(2):

TeacherCustom

Teacher A1
Teacher B1
Teacher C1
Teacher D1

 Students(3):

TeacherCustom

Teacher A1
Teacher B1
Teacher C1
Teacher D1

 

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"

View solution in original post

10 REPLIES 10
MargueriteWood
New Member

Hi I study at University. I have a programming lesson. We also did a matrix of students and teachers there. It was interesting. At our university there are ten students for each teacher who deal with it. But only according to the scriptures we have for each student there is a teacher. Because writing is very important. Our group sends its written paper works, including matches, www.au.papersowl.com to the service for verification. I consider that it is very correct to undo much attention to writing, so I recommend my university. Thank you.

Greg_Deckler
Super User
Super User

@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.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Sorry for the delay in answering.  I neglected to mention in my initial post that I am using Power Query and Power Pivot - not PowerBI.  However, based on both Greg and Zubair's posting, I was able to see how to solve the problem with Power Query, and have accomplished that.  However - I am not sure I can accept either answer as a solution, as I cannot test either one.

 

I was also happy to find the "Quick Measure Gallery" - could come in handy with future development.

 

Thanks to both.

Hi @DThayer,

 

This can be solved only with the PowerPivot as well.  If you are interested in getting that solution, please post back.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Could you post what you did in Power Query for the help of others with similar problems in the future?


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Power Query solution:

Table Students:

 

TeacherStudent

Teacher AStudent 1
Teacher AStudent 2
Teacher AStudent 3
Teacher AStudent 4
Teacher BStudent 1
Teacher BStudent 5
Teacher BStudent 7
Teacher BStudent 8
Teacher CStudent 3
Teacher CStudent 4
Teacher CStudent 7
Teacher CStudent 8
Teacher DStudent 5
Teacher DStudent 9
Teacher DStudent 10

 

Create 2 tables of distinct teachers with a second column of "1" to join the tables together:

Students(2):

TeacherCustom

Teacher A1
Teacher B1
Teacher C1
Teacher D1

 Students(3):

TeacherCustom

Teacher A1
Teacher B1
Teacher C1
Teacher D1

 

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"

Greg_Deckler
Super User
Super User

I would have thought it would be something like this but I can't get it to work correctly.

 

Measure 2 = 
VAR students1 = CALCULATETABLE(StudentTeachers,ALLEXCEPT(Teachers2,Teachers2[Teacher]))
VAR students1a = SELECTCOLUMNS(students1,"Student",[Student])
VAR students2 = CALCULATETABLE(StudentTeachers,ALLEXCEPT(Teachers,Teachers[Teacher]))
VAR students2a = SELECTCOLUMNS(students2,"Student",[Student])
VAR myvar = COUNTROWS(INTERSECT(students1a, students2a))
RETURN myvar

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

OK, not sure why the first one didn't work but this one does. 

 

Measure 4 = 
VAR students1 = FILTER(ALL(StudentTeachers),[Teacher]=MAX(Teachers[Teacher]))
VAR students1a = SELECTCOLUMNS(students1,"MyStudent",[Student])
VAR students2 = FILTER(ALL(StudentTeachers),[Teacher]=MAX(Teachers2[Teacher]))
VAR students2a = SELECTCOLUMNS(students2,"MyStudent",[Student])
VAR myvar = COUNTROWS(INTERSECT(students1a,students2a))
RETURN myvar

Note that I have two tables that list the unique teachers Teachers and Teachers two but you probably only need one of those. They are related to StudentTeachers (your table) in the obvious way.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@DThayer

 

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 )




Regards
Zubair

Please try my custom visuals

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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