Power Query Exercise
USE POWER QUERY TO TRANSLATE THE FOLLOWING
SQL QUERIES
Practice with AdventureWorks2022 databases from the link
https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-
ver16&tabs=ssms
Copy your M scripts as the solution in this file and upload!
1.
SELECT *
FROM humanresources.employee
ORDER BY jobtitle;
M script:
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
HumanResources_Employee = Source{[Schema="HumanResources",Item="Employee"]}[Data],
#"Sorted Rows" = Table.Sort(HumanResources_Employee,{{"JobTitle", Order.Ascending}})
in
#"Sorted Rows"
2.
SELECT salesorderid,customerid,orderdate,subtotal,
(taxamt*100)/subtotal AS Tax_percent
FROM sales.salesorderheader
ORDER BY subtotal desc;
M script:
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
Sales_SalesOrderHeader = Source{[Schema="Sales",Item="SalesOrderHeader"]}[Data],
#"Added Custom" = Table.AddColumn(Sales_SalesOrderHeader, "Tax_percent", each
([TaxAmt]*100) / ([SubTotal])),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Tax_percent",
"SalesOrderID", "CustomerID", "SubTotal", "TaxAmt"}),
#"Sorted Rows" = Table.Sort(#"Removed Other Columns",{{"SubTotal", Order.Descending}})
in
#"Sorted Rows"
3.
SELECT customerid,sum(freight) as total_freight
FROM sales.salesorderheader
group by customerid
ORDER BY customerid ASC;
M script:
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
Sales_SalesOrderHeader = Source{[Schema="Sales",Item="SalesOrderHeader"]}[Data],
#"Grouped Rows" = Table.Group(Sales_SalesOrderHeader, {"CustomerID"}, {{"Total_freight", each
List.Sum([Freight]), type number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"CustomerID", Order.Ascending}})
in
#"Sorted Rows"
4.
SELECT productid, sum(quantity) AS total_quantity
FROM production.productinventory
WHERE shelf IN ('A','C','H')
GROUP BY productid
HAVING SUM(quantity)>500
ORDER BY productid;
2
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
Production_ProductInventory = Source{[Schema="Production",Item="ProductInventory"]}[Data],
#"Filtered Rows" = Table.SelectRows(Production_ProductInventory, each [Shelf] = "A" or [Shelf] = "C"
or [Shelf] = "H"),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"ProductID"}, {{"Total_quantity", each
List.Sum([Quantity]), type number}}),
#"Filtered Rows1" = Table.SelectRows(#"Grouped Rows", each [Total_quantity] > 500),
#"Sorted Rows" = Table.Sort(#"Filtered Rows1",{{"ProductID", Order.Ascending}})
in
#"Sorted Rows"
5.
SELECT p.BusinessEntityID, FirstName, LastName, PhoneNumber AS
Person_Phone
FROM Person.Person AS p
JOIN Person.PersonPhone AS ph
ON p.BusinessEntityID = ph.BusinessEntityID
WHERE LastName LIKE 'L%'
ORDER BY LastName, FirstName;
let
Source = Table.NestedJoin(#"Person Person", {"BusinessEntityID"}, #"Person PersonPhone",
{"BusinessEntityID"}, "Person PersonPhone", JoinKind.Inner),
#"Expanded Person PersonPhone" = Table.ExpandTableColumn(Source, "Person PersonPhone",
{"PhoneNumber"}, {"Person PersonPhone.PhoneNumber"}),
#"Renamed Columns" = Table.RenameColumns(#"Expanded Person PersonPhone",{{"Person
PersonPhone.PhoneNumber", "Person_Phone"}}),
#"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each Text.StartsWith([LastName], "L")),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"FirstName", Order.Ascending}, {"LastName",
Order.Ascending}}),
3
#"Removed Other Columns" = Table.SelectColumns(#"Sorted Rows",{"LastName", "BusinessEntityID",
"FirstName", "Person_Phone"})
in
#"Removed Other Columns"
6.
From the table Person.BusinessEntityAddress use Power Query to retrieve the
number of employees for each City. Return city and number of employees. Sort
the result in ascending order on city.
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
Person_BusinessEntityAddress = Source{[Schema="Person",Item="BusinessEntityAddress"]}[Data],
#"Expanded Person.Address" = Table.ExpandRecordColumn(Person_BusinessEntityAddress,
"Person.Address", {"City"}, {"Person.Address.City"}),
#"Grouped Rows" = Table.Group(#"Expanded Person.Address", {"Person.Address.City"},
{{"Count_employee", each Table.RowCount(_), Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Person.Address.City", Order.Ascending}})
in
#"Sorted Rows"
7.
SELECT DATE_PART('year',OrderDate) AS "Year"
,SUM(TotalDue) AS "Order Amount"
FROM Sales.SalesOrderHeader
GROUP BY DATE_PART('year',OrderDate)
ORDER BY DATE_PART('year',OrderDate);
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
Sales_SalesOrderHeader = Source{[Schema="Sales",Item="SalesOrderHeader"]}[Data],
#"Added Custom" = Table.AddColumn(Sales_SalesOrderHeader, "Year", each Date.Year([OrderDate])),
#"Grouped Rows" = Table.Group(#"Added Custom", {"Year"}, {{"Order Amount", each
List.Sum([TotalDue]), type number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Year", Order.Ascending}})
4
in
#"Sorted Rows"
8.
SELECT ContactTypeID, Name
FROM Person.ContactType
WHERE Name LIKE '%Manager%'
ORDER BY Name DESC;
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
Person_ContactType = Source{[Schema="Person",Item="ContactType"]}[Data],
#"Removed Other Columns" = Table.SelectColumns(Person_ContactType,{"ContactTypeID", "Name"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Other Columns", each Text.Contains([Name],
"Manager")),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Name", Order.Descending}})
in
#"Sorted Rows"
9.
SELECT ProductID, Name
FROM Production.Product
WHERE Name LIKE 'Lock Washer%'
ORDER BY ProductID;
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
Production_Product = Source{[Schema="Production",Item="Product"]}[Data],
#"Removed Other Columns" = Table.SelectColumns(Production_Product,{"ProductID", "Name"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Other Columns", each Text.StartsWith([Name],
"Lock Washer")),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"ProductID", Order.Ascending}})
in
#"Sorted Rows"
5
10.
SELECT BusinessEntityID, JobTitle, HireDate
FROM HumanResources.Employee
ORDER BY DATE_PART('year',HireDate);
let
Source = Sql.Database("DESKTOP-L8EAF26", "AdventureWorks2022"),
HumanResources_Employee = Source{[Schema="HumanResources",Item="Employee"]}[Data],
#"Removed Other Columns" = Table.SelectColumns(HumanResources_Employee,{"BusinessEntityID",
"JobTitle", "HireDate"}),
#"Added Custom" = Table.AddColumn(#"Removed Other Columns", "DATE_PART('year',HireDate)",
each Date.Year([HireDate])),
#"Sorted Rows" = Table.Sort(#"Added Custom",{{"DATE_PART('year',HireDate)", Order.Ascending}}),
#"Removed Other Columns1" = Table.SelectColumns(#"Sorted Rows",{"BusinessEntityID", "JobTitle",
"HireDate"})
in
#"Removed Other Columns1"