Power Query The Essentials
Power Query is Microsofts dataconnectivity and datapreparation engine that lives inside Excel, Power BI Desktop, and several other Microsoft products. It allows you to discover, connect, combine, and refine data from a wide variety of sources without writing code. The result is a clean, structured set of tables ready for analysis, reporting, or further transformation.
While the user interface is based on a series of clicks and dialogs, Power Query records every step as M code (the language behind the engine). This means you can view, edit, and versioncontrol the underlying script if you need to.
Open Excel (or Power BI Desktop) and use the Data tab Get Data. From there you can select the connector you need for example From File From Workbook, From Database From SQL Server, or From Web.
After the connection dialog, the Power Query Editor appears. The main areas are:
Select the columns you want to keep, rightclick and choose Remove Other Columns. The generated step looks like:
Table.SelectColumns(Source, {"OrderID","Customer","Date","Total"}) Use the filter icons on column headers or the Keep Rows menu. Example:
Table.SelectRows(PreviousStep, each [Status] = "Closed")
Power Query automatically detects types, but you can enforce a type with Table.TransformColumnTypes:
Table.TransformColumnTypes(PreviousStep, {{"Date", type date}, {"Total", type number}}) Choose Home Merge Queries to combine two tables on a common key. The step resembles:
Table.NestedJoin(Orders, "CustomerID", Customers, "ID", "CustomerData", JoinKind.LeftOuter)
Append is useful when you have similar tables from different files. The M code uses Table.Combine:
Table.Combine({January, February, March}) Transform a set of rows into columns (pivot) or reverse it (unpivot). Example of unpivoting:
Table.UnpivotOtherColumns(Source, {"ID","Name"}, "Attribute", "Value") You can create a Parameter (Home Manage Parameters) and reference it in a source step. This is handy for dynamic file paths or server names. Example:
Source = Csv.Document(File.Contents(ParameterFilePath),[Delimiter=",", Columns=5, Encoding=1252])
Define a function once and invoke it for each row, similar to a userdefined function in Excel. A simple function that adds tax might look like:
let AddTax = (price as number) as number => price * 1.07in AddTax
Apply it with Table.AddColumn:
Table.AddColumn(PreviousStep, "PriceWithTax", each AddTax([Price]))
Power Query can call REST APIs with Web.Contents. For instance, pulling JSON data from a public endpoint:
let Source = Web.Contents("https://api.example.com/data"), Json = Json.Document(Source), Table = Record.ToTable(Json)in Table Table.Buffer unless you need to force caching.When you finish shaping data in Power BI Desktop, click Close & Apply. The model stores the query definitions. In the Power BI Service you can schedule automatic refreshes (daily, hourly, etc.) as long as the data source credentials are provided.
In Excel, after loading a query to a worksheet or data model, you can refresh it manually (Data Refresh) or set it to refresh on open (Connection Properties Refresh control).
Because each query is stored as M code, you can copy the code into a .txt file and place it under Git. In Power BI, the Export to .pbix file contains the full model, but for pure query versioning many teams keep a separate repository of M scripts.
