Power Query: Complete Guide

RANAJOY SAHARANAJOY SAHA
9 min read

What is Power Query?

Power Query is a powerful data connection and transformation tool available in Microsoft Excel, Power BI, and other Microsoft applications. It allows users to extract, transform, and load (ETL) data from various sources into a format suitable for analysis.

Key Features

1. Data Source Connectivity

  • Multiple Sources: Connect to databases (SQL Server, Oracle, MySQL), cloud services (Azure, SharePoint), files (Excel, CSV, JSON, XML), web pages, and more

  • Real-time Refresh: Set up automatic data refresh schedules

  • Authentication: Supports various authentication methods including OAuth, API keys, and Windows authentication

2. Data Transformation Capabilities

  • Column Operations: Add, remove, rename, split, merge columns

  • Row Filtering: Filter data based on conditions, remove duplicates, sort

  • Data Type Changes: Convert text to numbers, dates, etc.

  • Calculations: Create custom columns with formulas

  • Grouping and Aggregation: Group data and perform calculations like sum, count, average

3. M Language

  • Formula Language: Power Query uses M language for advanced transformations

  • Custom Functions: Create reusable functions for complex operations

  • Advanced Logic: Implement conditional logic, loops, and error handling

Getting Started

In Excel

  1. Access Power Query:

    • Go to Data tab → Get Data (in Excel 2016+)

    • Or Data tab → From Other Sources → From Microsoft Query (older versions)

  2. Power Query Editor Interface (Based on official Microsoft documentation):

The Power Query editor has five distinct components:

A. The Ribbon - Contains multiple tabs:

  • Home: Data source connections, merge/append queries, refresh options

  • Transform: Data type changes, text operations, grouping, pivoting

  • Add Column: Create calculated columns, conditional columns, custom formulas

  • View: Toggle between data preview, schema view, diagram view, advanced editor

  • Help: Documentation links and support resources

B. Queries Pane (Left side):

Queries
├── 📁 Other Queries
│   ├── 🔍 Query1 (Customers)
│   └── 🔍 Query2 (Products)
└── 📁 Data Sources
    └── 🔗 Northwind OData
  • Shows all available queries in your project

  • Can be organized into folders

  • Expandable/collapsible with icon in top-right corner

C. Current View (Center - main working area):

  • Data Preview: Shows actual data in rows and columns

  • Schema View: Column names and data types only (recommended for column operations)

  • Diagram View: Visual flowchart of query steps and dependencies

D. Query Settings Pane (Right side):

Query Settings
┌─────────────────────────┐
│ Properties              │
│ Name: Customer Analysis │
│ Description: [empty]    │
│                         │
│ Applied Steps           │
│ ✓ Source               │
│ ✓ Promoted Headers     │
│ ✓ Changed Type         │
│ ✓ Filtered Rows        │
│ ✓ Added Custom Column  │
└─────────────────────────┘
  • Shows current query name and description

  • Lists all transformation steps applied

  • Each step can be selected to preview data at that point

E. Status Bar (Bottom):

  • Shows execution time, row count, column count

  • Contains view toggle buttons (Data/Schema/Diagram)

  • Processing status indicators

Common Data Sources and Connection Examples

1. Excel Files

Connection Process:

Data → Get Data → From File → From Workbook
Browse to Excel file → Navigator opens → Select worksheet → Transform Data

Navigator Dialog (based on Microsoft documentation):

┌────────────────────────────────────────────────────────────────┐
│ Navigator                                                  [×] │
├────────────────────────────────────────────────────────────────┤
│ Display Options: [Tables and Worksheets ▼] [Search: ____]     │
├──────────────────────┬─────────────────────────────────────────┤
│ 📊 Sheet1            │ Data Preview:                           │
│ 📊 Sheet2            │ ┌────────┬─────────┬─────────┬──────────┐ │
│ 📋 Table1            │ │ Name   │ Sales   │ Date    │ Region   │ │
│ 📋 Table2            │ ├────────┼─────────┼─────────┼──────────┤ │
│                      │ │ John   │ 1000    │ 1/1/23  │ North    │ │
│                      │ │ Mary   │ 1500    │ 1/2/23  │ South    │ │
│                      │ │ Bob    │ 1200    │ 1/3/23  │ East     │ │
│                      │ └────────┴─────────┴─────────┴──────────┘ │
├──────────────────────┴─────────────────────────────────────────┤
│ [Load ▼] [Transform Data] [Cancel]                              │
└────────────────────────────────────────────────────────────────┘
  • Left pane shows all sheets and tables with distinct icons

  • Right pane shows live preview of selected data source

  • Can search for specific objects using the search box

2. CSV Files

Connection Process:

Data → Get Data → From File → From Text/CSV
Select file → Preview shows delimiter detection → Transform Data

Interface Elements: File origin dropdown, delimiter options, data type detection preview.

3. Database Connection (SQL Server)

Connection Process:

Data → Get Data → From Database → From SQL Server Database
Enter server name → Select database → Choose tables → Transform Data

Authentication Options: Windows authentication, database credentials, or advanced options for connection strings.

4. Web Data

Connection Process:

Data → Get Data → From Other Sources → From Web
Enter URL → Web preview → Select tables → Transform Data

Common Transformations with Step-by-Step Examples

Example 1: Cleaning Sales Data

Scenario: You have a sales dataset with inconsistent formatting that needs cleaning.

Raw Data Appearance:

| Customer Name  | Sales Amount | Date      | Region    |
|----------------|--------------|-----------|-----------|
| john smith     | $1,500.00    | 01-Jan-23 | north     |
| MARY JOHNSON   | 2500         | 1/2/2023  | South     |
| Bob Wilson     | $3,200.50    | 2023-01-03| EAST      |

Transformation Steps:

  1. Promote Headers (if data doesn't start in row 1):

     Home tab → Use First Row as Headers
    
  2. Clean Text Columns:

     Select Customer Name column → Transform tab → Format → Proper Case
     Result: "John Smith", "Mary Johnson", "Bob Wilson"
    
  3. Remove Currency Symbols:

     Select Sales Amount column → Transform tab → Replace Values
     Find: "$" | Replace with: "" (empty)
     Find: "," | Replace with: "" (empty)
    
  4. Change Data Types:

     Select Sales Amount column → Data type dropdown → Decimal Number
     Select Date column → Data type dropdown → Date
    
  5. Standardize Region Names:

     Select Region column → Transform tab → Format → Proper Case
    

Applied Steps Panel Shows:

Applied Steps:
✓ Source
✓ Promoted Headers  
✓ Changed Customer Name to Proper Case
✓ Replaced "$" in Sales Amount
✓ Replaced "," in Sales Amount
✓ Changed Type (Sales Amount to Decimal)
✓ Changed Type (Date to Date)
✓ Changed Region to Proper Case

Example 2: Combining Multiple Files

Scenario: Monthly sales files that need to be combined into one dataset.

Process:

  1. Get Data from Folder:

     Data → Get Data → From File → From Folder
     Select folder containing Excel files → Combine → Transform Data
    
  2. Combine Files Dialog:

     ┌─────────────────────────────────────────────┐
     │ Combine Files                               │
     │                                             │
     │ Example File: [January 2023.xlsx ▼]        │
     │                                             │
     │ Parameter: [Sheet1 ▼]                      │
     │                                             │
     │ Preview:                                    │
     │ ┌─────────┬─────────┬─────────┬──────────┐ │
     │ │ Product │ Sales   │ Month   │ Year     │ │
     │ │ A       │ 1000    │ Jan     │ 2023     │ │
     │ │ B       │ 1500    │ Jan     │ 2023     │ │
     │ └─────────┴─────────┴─────────┴──────────┘ │
     │                                             │
     │ [OK] [Cancel]                              │
     └─────────────────────────────────────────────┘
    

Example 3: Creating Calculated Columns

Add Custom Column Example:

Add Column tab → Custom Column

Column Name: Total with Tax
Formula: [Sales Amount] * 1.08

Conditional Column Example:

Add Column tab → Conditional Column

Column Name: Performance Category
Conditions:
If [Sales Amount] >= 2000 then "High"
else if [Sales Amount] >= 1000 then "Medium"  
else "Low"

Advanced Features

1. Merge Queries (Similar to VLOOKUP)

Process:

Home tab → Merge Queries → Select tables and join columns
Choose join type (Left, Right, Inner, Full Outer)
Expand merged column to show related data

Visual Representation:

Table 1 (Sales):          Table 2 (Products):
┌────────┬─────────┐      ┌────────┬─────────────┐
│ Prod_ID│ Sales   │      │ Prod_ID│ ProductName │
├────────┼─────────┤  →   ├────────┼─────────────┤
│ 001    │ 1500    │      │ 001    │ Widget A    │
│ 002    │ 2000    │      │ 002    │ Widget B    │
└────────┴─────────┘      └────────┴─────────────┘

Result after merge:
┌────────┬─────────┬─────────────┐
│ Prod_ID│ Sales   │ ProductName │
├────────┼─────────┼─────────────┤
│ 001    │ 1500    │ Widget A    │
│ 002    │ 2000    │ Widget B    │
└────────┴─────────┴─────────────┘

2. Append Queries (Union)

Use Case: Combining tables with same structure

Home tab → Append Queries → Select queries to combine
Result: Single table with all rows from source tables

3. Pivot and Unpivot Operations

Pivot Example (Wide format):

Original:                    Pivoted:
┌─────────┬────────┬────────┐    ┌─────────┬─────┬─────┬─────┐
│ Product │ Month  │ Sales  │    │ Product │ Jan │ Feb │ Mar │
├─────────┼────────┼────────┤    ├─────────┼─────┼─────┼─────┤
│ A       │ Jan    │ 100    │ →  │ A       │ 100 │ 150 │ 200 │
│ A       │ Feb    │ 150    │    │ B       │ 120 │ 180 │ 220 │
│ A       │ Mar    │ 200    │    └─────────┴─────┴─────┴─────┘
│ B       │ Jan    │ 120    │
└─────────┴────────┴────────┘

Unpivot Example (Long format):

Transform tab → Unpivot Columns → Select columns to unpivot

M Language Basics

Simple M Code Example:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Sales", type number},
        {"Date", type date}
    }),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", 
        each [Sales] > 1000),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", 
        "Tax", each [Sales] * 0.08)
in
    #"Added Custom"

Custom Function Example:

// Function to calculate commission
(SalesAmount as number) as number =>
    if SalesAmount > 5000 then SalesAmount * 0.05
    else if SalesAmount > 2000 then SalesAmount * 0.03
    else SalesAmount * 0.01

Best Practices

1. Query Organization

  • Naming Convention: Use descriptive names for queries

  • Grouping: Organize related queries in folders

  • Documentation: Add descriptions to complex transformations

2. Performance Optimization

  • Query Folding: Let database do the work when possible

  • Early Filtering: Filter data as early as possible in the process

  • Avoid Volatile Functions: Minimize use of functions that prevent query folding

3. Error Handling

// Example error handling in M
try [Sales Amount] * 0.08 otherwise 0

4. Data Refresh Management

  • Refresh Settings: Configure automatic refresh schedules

  • Data Source Management: Centralize connection settings

  • Parameter Usage: Use parameters for flexible data sources

Common Issues and Solutions

1. Data Type Errors

Problem: Text that looks like numbers won't calculate Solution:

Transform tab → Data Type → Number
Or use: Value.FromText([Column Name])

2. Null Values

Problem: Calculations fail due to null values Solution:

Transform tab → Replace Values → Replace null with 0
Or in M: if [Column] = null then 0 else [Column]

3. Date Format Issues

Problem: Dates not recognized properly Solution:

Transform tab → Data Type → Date
Or specify culture in M: Date.FromText([Date], "en-US")

Integration with Other Tools

Power BI

  • Seamless Integration: Same interface as Excel Power Query

  • Enhanced Features: Additional connectors and transformations

  • Dataflows: Reusable ETL processes across multiple reports

Power Automate

  • Automated Refresh: Trigger Power Query refreshes

  • Data Flow: Integrate with automated workflows

Azure Data Factory

  • Enterprise Scale: Power Query Online for large-scale ETL

  • Cloud Integration: Direct connection to Azure services

Keyboard Shortcuts

ActionShortcut
Refresh PreviewCtrl + R
Close & LoadCtrl + Shift + Enter
Duplicate QueryCtrl + D
Rename QueryF2
Remove StepDelete
Formula BarCtrl + Shift + F

Conclusion

Power Query transforms the way you work with data by providing a user-friendly interface for complex data operations. Whether you're cleaning messy datasets, combining multiple sources, or creating sophisticated data models, Power Query offers the tools and flexibility needed for modern data analysis.

The key to mastering Power Query is practice with real datasets and gradually learning the M language for advanced scenarios. Start with simple transformations and build complexity as you become more comfortable with the interface and concepts.

0
Subscribe to my newsletter

Read articles from RANAJOY SAHA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RANAJOY SAHA
RANAJOY SAHA