In this task, you import data into your Excel workbook from the sample Northwind OData feed at http://services.odata.org/Northwind/Northwind.svc,expand the Order_Details table, remove columns, calculate a line total,transform an OrderDate, group rows by ProductID and Year, rename the query,and disable query download to the Excel workbook.
Step 1: Connect to an OData Feed
-
Select Data > Get Data >From Other Sources > From OData Feed.
-
In the OData Feed dialog box, enter the URL for the Northwind OData feed.
-
Select OK.
-
In the Navigator pane, double click the Orders table.
Step 2: Expand an Order_Details table
In this step, you expand the Order_Details table that is related to the Orders table, to combine the ProductID, UnitPrice, and Quantity columns from Order_Details into the Orders table. The Expand operation combines columns from a related table into a subject table. When the query runs, rows from the related table (Order_Details) are combined into rows with the primary table (Orders).
In Power Query, a column containing a related table has the value RecordorTable in the cell. These are called structured columns. Recordindicatesa single related record and represents aone-to-one relationship with the current data or primary table. Tableindicatesa related table and represents a one-to-many relationship with the current or primary table. A structured column represents a relationship in a data source that has a relational model. For example,a structured column indicates an entity with a foreign key association in an OData feed or foreign key relationship in a SQL Server database.
After you expand the Order_Details table, three new columns and additional rows are added to the Orders table, one for each row in the nested or related table.
-
In Data Preview, scroll horizontally to the Order_Details column.
-
In the Order_Details column, select the expand icon ().
-
In the Expand drop-down:
See AlsoConnect to a Power Query data source - Dynamics 365 Customer InsightsPower Query Folder connector - Power QueryMerge queries (Power Query) - Microsoft SupportCreate a Data Model in Excel-
Select (Select All Columns) to clear all columns.
-
Select ProductID, UnitPrice, and Quantity.
-
Select OK.
Note:In Power Query, you can expand tables linked from a column and aggregate the columns of the linked table before expanding the data in the subject table. For more information about how to perform aggregate operations, see Aggregate data from a column.
-
Step 3: Remove other columns to only display columns of interest
In this step you remove all columns except OrderDate, ProductID, UnitPrice, and Quantity columns.
-
In DataPreview, select the following columns:
-
Select the first column,OrderID.
-
Shift+Click the last column,Shipper.
-
Ctrl+Click the OrderDate, Order_Details.ProductID, Order_Details.UnitPrice, and Order_Details.Quantity columns.
-
-
Right-click on a selected column header, and select Remove Other Columns.
Step 4: Calculate the line total for each Order_Details row
In this step, you create a Custom Column to calculate the line total for each Order_Details row.
-
In Data Preview, select the table icon () at the top-left corner of the preview.
-
Click Add Custom Column.
-
In the Custom Column dialog box, in the Custom column formula box, enter [Order_Details.UnitPrice] * [Order_Details.Quantity].
-
In the New column name box, enter Line Total.
-
Select OK.
Step 5: Transform an OrderDate year column
In this step, you transform the OrderDate column to render the order date year.
-
In DataPreview, right-click the OrderDate column, and select Transform > Year.
-
Rename the OrderDate column to Year:
-
Double-Click the OrderDate column, and enter Year or
-
Right-Click on the OrderDate column, select Rename, and enter Year.
-
Step 6: Group rows by ProductID and Year
-
In Data Preview, select Year and Order_Details.ProductID.
-
Right-Click one of the headers, and select Group By.
-
In the Group By dialog box:
-
In the New column name textbox, enter Total Sales.
-
In the Operation drop-down, select Sum.
-
In the Column drop-down, select Line Total.
-
-
Select OK.
Step 7: Rename a query
Before you import the sales data into Excel, rename the query:
-
In the Query Settings pane, in the Namebox enter Total Sales.
Results: Final query for Task 2
After you perform each step, you will have a Total Sales query over the Northwind OData feed.
Summary: Power Query steps created in Task 2
As you perform query activities in Power Query, query steps are created and listed in the Query Settings pane, in the Applied Stepslist. Each query step has a corresponding Power Query formula, also known as the "M" language. For more information about Power Query formulas, see Learn about Power Query formulas.
Task | Query step | Formula |
---|---|---|
Connect to an OData feed | Source | = OData.Feed("http://services.odata.org/Northwind/Northwind.svc", null, [Implementation="2.0"]) |
Select a table | Navigation | = Source{[Name="Orders"]}[Data] |
Expand the Order_Details table | Expand Order_Details | = Table.ExpandTableColumn(Orders, "Order_Details", {"ProductID", "UnitPrice", "Quantity"}, {"Order_Details.ProductID", "Order_Details.UnitPrice", "Order_Details.Quantity"}) |
Remove other columns to only display columns of interest | RemovedColumns | = Table.RemoveColumns(#"Expand Order_Details",{"OrderID", "CustomerID", "EmployeeID", "RequiredDate", "ShippedDate", "ShipVia", "Freight", "ShipName", "ShipAddress", "ShipCity", "ShipRegion", "ShipPostalCode", "ShipCountry", "Customer", "Employee", "Shipper"}) |
Calculate the line total for each Order_Details row | Added Custom | = Table.AddColumn(RemovedColumns, "Custom", each [Order_Details.UnitPrice] * [Order_Details.Quantity]) = Table.AddColumn(#"Expanded Order_Details", "Line Total", each [Order_Details.UnitPrice] * [Order_Details.Quantity]) |
Change to a more meaningful name, Lne Total | Renamed Columns | = Table.RenameColumns(InsertedCustom,{{"Custom", "Line Total"}}) |
Transform the OrderDate column to render the year | Extracted Year | = Table.TransformColumns(#"Grouped Rows",{{"Year", Date.Year, Int64.Type}}) |
Change to more meaningful names, OrderDate and Year | Renamed Columns 1 | Table.RenameColumns (TransformedColumn,{{"OrderDate", "Year"}}) |
Group rows by ProductID and Year | GroupedRows | = Table.Group(RenamedColumns1, {"Year", "Order_Details.ProductID"}, {{"Total Sales", each List.Sum([Line Total]), type number}}) |