Graph.Nodes

D

T

The M Code Behind the Power Query M function Graph.Nodes

One of the most useful functions in Power Query is Graph.Nodes. This function allows you to extract information about the nodes in a graph, which can be useful for analyzing network data. In this article, we’ll take a look at the M code behind the Graph.Nodes function, and show you how to use it to extract information from a graph.

What is a Graph?

Before we dive into the M code behind Graph.Nodes, it’s important to understand what a graph is. In computer science, a graph is a collection of nodes (also called vertices) and edges. Nodes can represent anything from people to computer systems to web pages, and edges represent the connections between them.

Graphs are often used to represent complex networks, such as social networks or computer networks. By analyzing the nodes and edges in a graph, you can gain insight into the underlying structure of the network.

The M Code Behind Graph.Nodes

The Graph.Nodes function takes a graph as its input and returns a table that contains information about the nodes in the graph. The table has two columns: Node and Properties. The Node column contains the name of each node in the graph, and the Properties column contains a record that describes the properties of each node.

Here is an example of the M code that you can use to extract information about the nodes in a graph:


let

Source = Graph.FromEdges({{1, 2}, {1, 3}, {2, 3}, {3, 4}, {4, 5}}),

Nodes = Graph.Nodes(Source)

in

Nodes


In this example, we are creating a graph with five nodes and five edges. The Graph.FromEdges function takes a list of edges and returns a graph object. We then pass this graph object to the Graph.Nodes function to extract information about the nodes.

When you run this code, you will get a table that looks like this:


| Node | Properties |

|——|———————–|

| 1 | [ConnectedEdges=[1,2]] |

| 2 | [ConnectedEdges=[1,3]] |

| 3 | [ConnectedEdges=[2,3,4]]|

| 4 | [ConnectedEdges=[3,5]] |

| 5 | [ConnectedEdges=[4]] |


As you can see, the table contains one row for each node in the graph. The Node column contains the name of the node (in this case, a number from 1 to 5), and the Properties column contains a record that describes the properties of the node.

Understanding the Properties Record

The Properties record in the table returned by Graph.Nodes contains a number of useful pieces of information about each node in the graph. Here is a breakdown of the properties that are included:

- Degree: The number of edges connected to the node.

- InDegree: The number of edges that point to the node.

- OutDegree: The number of edges that start at the node.

- ConnectedEdges: A list of the IDs of the edges that are connected to the node.

- IsIsolated: A Boolean value that indicates whether the node is isolated (i.e. has no edges connected to it).

- IsSource: A Boolean value that indicates whether the node is a source node (i.e. has edges that start at it but do not end at any other nodes in the graph).

- IsTarget: A Boolean value that indicates whether the node is a target node (i.e. has edges that end at it but do not start at any other nodes in the graph).

By using the Properties record, you can gain insight into the structure of the network and the relationships between the nodes.

The Graph.Nodes function in Power Query is a powerful tool for analyzing networks and extracting information about the nodes in a graph. By understanding the M code behind this function and the Properties record that it returns, you can gain valuable insights into the structure of networks and the relationships between nodes. Whether you are analyzing social networks, computer networks, or any other kind of network data, the Graph.Nodes function is a valuable tool to have in your toolbox.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)