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
DucLuong
Frequent Visitor

Generate new table for Network Navigator visual

Hello!

 

I have a table like this:

Student_IDCourse
AEnglish
AMath
CEnglish
DMath
EArt

 

I want to generate a new table that shows the relationship between each student that shares the same class in each row. The purpose is to show which student has shared the same class with each other using the Network Navigator visual by Microsoft. My desired output is as below:

 

Student_IDContactCourse
ACEnglish
ADMath
CAEnglish
DAMath
E  

 

There's got to be a pretty straightforward way to do this but I don't know the right wording to look for it.

 

Many thanks for your help.

Duc

2 ACCEPTED SOLUTIONS
mahoneypat
Employee
Employee

You could do this in the query editor by doing a self merge of this table with itself, joined on the Course column, expanding the Student column, and then filtering away the rows where the the same student is twice on the same row.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.  Call your original table Class or update the code below with the correct name.

let
    Source = Table.NestedJoin(Class, {"Course"}, Class, {"Course"}, "Class", JoinKind.LeftOuter),
    #"Added Custom1" = Table.AddColumn(Source, "NumRows", each Table.RowCount([Class]), Int64.Type),
    #"Expanded Class" = Table.ExpandTableColumn(#"Added Custom1", "Class", {"Student_ID"}, {"Student_ID.1"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Class", "SameStudent", each if ([Student_ID] = [Student_ID.1] and [NumRows]>1) then "Y" else "N"),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([SameStudent] = "N")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"SameStudent", "NumRows"})
in
    #"Removed Columns"

 

 

You could also do this with a DAX table with the code below.  I called your table "Class".

 

Same Class =
VAR StudentClass =
    SELECTCOLUMNS ( Class, "Student", Class[Student_ID], "Class", Class[Course] )
VAR NewTable =
    GENERATE (
        StudentClass,
        VAR vThisStudent = [Student]
        VAR vThisCourse = [Class]
        RETURN
            CALCULATETABLE (
                VALUES ( Class[Student_ID] ),
                Class[Course] = vThisCourse,
                Class[Student_ID] <> vThisStudent
            )
    )
RETURN
    NewTable

 

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

v-yingjl
Community Support
Community Support

Hi @DucLuong ,

You can try this query to generate the new table:

let
    Source = Table.NestedJoin(Table, {"Course"}, Table, {"Course"}, "Table", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(Source, "Count", each Table.RowCount([Table])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each if [Count] > 1 then [Table] else null),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"Student_ID", "Course"}, {"Custom.Student_ID", "Custom.Course"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Course", "Table", "Count"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Custom.Student_ID] <> [Student_ID]),
    #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Custom.Student_ID", type text}, {"Custom.Course", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Custom.Student_ID", "Contact"}, {"Custom.Course", "Course"}})
in
    #"Renamed Columns"

tb.png

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-yingjl
Community Support
Community Support

Hi @DucLuong ,

You can try this query to generate the new table:

let
    Source = Table.NestedJoin(Table, {"Course"}, Table, {"Course"}, "Table", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(Source, "Count", each Table.RowCount([Table])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each if [Count] > 1 then [Table] else null),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"Student_ID", "Course"}, {"Custom.Student_ID", "Custom.Course"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Course", "Table", "Count"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Custom.Student_ID] <> [Student_ID]),
    #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Custom.Student_ID", type text}, {"Custom.Course", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Custom.Student_ID", "Contact"}, {"Custom.Course", "Course"}})
in
    #"Renamed Columns"

tb.png

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks a lot @v-yingjl . I was able to reproduce it by referring to your attached pbix file. The issue is that my table has about 30,000 rows so it takes a very long time to generate the new table, which is about 1 million rows. But I've made some progress with your solution so thank you very much!

mahoneypat
Employee
Employee

You could do this in the query editor by doing a self merge of this table with itself, joined on the Course column, expanding the Student column, and then filtering away the rows where the the same student is twice on the same row.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.  Call your original table Class or update the code below with the correct name.

let
    Source = Table.NestedJoin(Class, {"Course"}, Class, {"Course"}, "Class", JoinKind.LeftOuter),
    #"Added Custom1" = Table.AddColumn(Source, "NumRows", each Table.RowCount([Class]), Int64.Type),
    #"Expanded Class" = Table.ExpandTableColumn(#"Added Custom1", "Class", {"Student_ID"}, {"Student_ID.1"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Class", "SameStudent", each if ([Student_ID] = [Student_ID.1] and [NumRows]>1) then "Y" else "N"),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([SameStudent] = "N")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"SameStudent", "NumRows"})
in
    #"Removed Columns"

 

 

You could also do this with a DAX table with the code below.  I called your table "Class".

 

Same Class =
VAR StudentClass =
    SELECTCOLUMNS ( Class, "Student", Class[Student_ID], "Class", Class[Course] )
VAR NewTable =
    GENERATE (
        StudentClass,
        VAR vThisStudent = [Student]
        VAR vThisCourse = [Class]
        RETURN
            CALCULATETABLE (
                VALUES ( Class[Student_ID] ),
                Class[Course] = vThisCourse,
                Class[Student_ID] <> vThisStudent
            )
    )
RETURN
    NewTable

 

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thanks a lot @mahoneypat . Your solution also works. However, it takes a really long time to generate the new table so I used Excel to create the table first, then Power BI just read the data. It wold be great if there's a faster way but this will do for now.

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.

Top Solution Authors
Top Kudoed Authors