[ Power Query ] #_cheatsheet />
Loading Data
● Load from CSV: =
[Link]([Link]("path/to/[Link]"),[Delimiter=",",
Columns=5, Encoding=1252, QuoteStyle=[Link]])
● Load from Excel: =
[Link]([Link]("path/to/[Link]"), null, true)
● Load from JSON: = [Link]([Link]("path/to/[Link]"))
● Load from Web: = [Link]("[Link]
● Load from Folder: = [Link]("path/to/folder")
● Load from SQL Database: = [Link]("server", "database")
Data Transformation
● Remove Columns: = [Link](Source,{"Column1",
"Column2"})
● Rename Columns: = [Link](Source,{{"oldName",
"newName"}})
● Change Data Type: = [Link](Source,{{"Column",
[Link]}})
● Filter Rows: = [Link](Source, each [Column] > 100)
● Sort Rows: = [Link](Source,{{"Column", [Link]}})
● Add Custom Column: = [Link](Source, "Custom", each
[Column1] + [Column2])
● Merge Queries: =
[Link](Source1,{"KeyColumn"},Source2,{"KeyColumn"},"NewCo
lumnName",[Link])
● Append Queries: = [Link]({Source1, Source2})
● Group Rows: = [Link](Source, {"KeyColumn"}, {{"NewColumn",
each [Link](_), type number}})
● Pivot Columns: = [Link](Source,
[Link](Source[ColumnToPivot]), "ColumnToPivot",
"ValueColumn")
● Unpivot Columns: = [Link](Source, {"Column1",
"Column2"}, "Attribute", "Value")
By: Waleed Mousa
● Extract Text Before Delimiter: = [Link](Source,
{"Column", each [Link](_, "-"), type text})
● Extract Text After Delimiter: = [Link](Source,
{"Column", each [Link](_, "-"), type text})
● Split Column by Delimiter: = [Link](Source, "Column",
[Link]("-", [Link]))
● Replace Values: = [Link](Source, "oldValue",
"newValue", [Link], {"Column"})
● Fill Down: = [Link](Source,{"Column"})
● Fill Up: = [Link](Source,{"Column"})
● Trim Text: = [Link](Source,{{"Column", [Link],
type text}})
● Make Uppercase: = [Link](Source,{{"Column",
[Link], type text}})
● Make Lowercase: = [Link](Source,{{"Column",
[Link], type text}})
● Add Prefix: = [Link](Source,{{"Column", each
"Prefix_" & _, type text}})
● Add Suffix: = [Link](Source,{{"Column", each _ &
"_Suffix", type text}})
● Round Numbers: = [Link](Source,{{"Column",
[Link], type number}})
● Remove Duplicates: = [Link](Source, {"Column"})
● Remove Blank Rows: = [Link](Source, each not
[Link]([Link]([Link](_), {null,
""})))
● Expand Table Column: = [Link](Source, "Column",
{"SubColumn1", "SubColumn2"})
● Aggregate Column: = [Link](Source, "Column",
{{"Column", [Link], "Sum"}})
● Parse JSON Column: = [Link](Source,{{"Column",
[Link], type any}})
● Calculate Age: = [Link](Source, "Age", each
[Link]([Link]()) - [Link]([BirthDate]))
Date Operations
By: Waleed Mousa
● Add Days: = [Link](Source, "NewDate", each
[Link]([DateColumn], 10))
● Subtract Days: = [Link](Source, "NewDate", each
[Link]([DateColumn], -10))
● Start of Month: = [Link](Source, "StartOfMonth", each
[Link]([DateColumn]))
● End of Month: = [Link](Source, "EndOfMonth", each
[Link]([DateColumn]))
● Difference Between Dates: = [Link](Source, "DaysDiff",
each [Link]([DateColumn2] - [DateColumn1]))
● Extract Year: = [Link](Source, "Year", each
[Link]([DateColumn]))
● Extract Month: = [Link](Source, "Month", each
[Link]([DateColumn]))
● Extract Day: = [Link](Source, "Day", each
[Link]([DateColumn]))
● Day of Week: = [Link](Source, "Weekday", each
[Link]([DateColumn]))
● Week of Year: = [Link](Source, "WeekOfYear", each
[Link]([DateColumn]))
Text Operations
● Length of Text: = [Link](Source, "Length", each
[Link]([TextColumn]))
● Extract Substring: = [Link](Source, "Substring", each
[Link]([TextColumn], start, length))
● Find Text Position: = [Link](Source, "Position", each
[Link]([TextColumn], "searchText", 1, -1))
● Replace Text: =
[Link](Source,"oldText","newText",[Link],
{"TextColumn"})
● Split Text at Position: = [Link](Source, "TextColumn",
[Link]({start, length}), {"Text1", "Text2"})
● Combine Text: = [Link](Source, {"TextColumn1",
"TextColumn2"}, [Link](" ",
[Link]), "CombinedColumn")
By: Waleed Mousa
Logical Operations
● Conditional Columns: = [Link](Source, "Category", each if
[Column] > 100 then "High" else "Low")
● Switch Statement: = [Link](Source, "Category", each
switch([Condition], 1, "One", 2, "Two", "Other"))
● Logical And: = [Link](Source, each [Column1] > 100 and
[Column2] < 50)
● Logical Or: = [Link](Source, each [Column1] > 100 or
[Column2] < 50)
● Logical Not: = [Link](Source, each not [Column] =
"Value")
Advanced Data Transformation
● Custom Function Call: = [Link](Source, "CustomResult",
each MyCustomFunction([Column]))
● Iterate Over Rows: = [Link](Source, each [Column1] +
[Column2])
● Normalize Data: = [Link](Source,{{"Column", each (_
- [Link](Source[Column])) / ([Link](Source[Column]) -
[Link](Source[Column])), type number}})
● Standardize Data: = [Link](Source,{{"Column", each
(_ - [Link](Source[Column])) /
[Link](Source[Column]), type number}})
● Discretize Column: = [Link](Source,{{"Column", each
if _ > threshold then "Above" else "Below", type text}})
Data Aggregation
● Sum Column: = [Link](Source, "Column")
● Average Column: = [Link](Source, "Column")
● Min and Max: = [Link](Source, "Column"), = [Link](Source,
"Column")
● Count Rows: = [Link](Source)
● Count Unique Values: = [Link](Source, {"Column"}, {{"Count",
each [Link](_), [Link]}})
By: Waleed Mousa
Query Management
● Reference Other Query: = #"Other Query Name"
● Set Data Type: = [Link](Source,{{"Column", type
type}})
● Parameter Reference: = #"Parameter Name"
● Invoke Custom Function: = MyFunction(parameters)
Data Integration
● Merge Tables (Inner Join): = [Link](Source1, "KeyColumn",
Source2, "KeyColumn", [Link])
● Merge Tables (Left Outer Join): = [Link](Source1, "KeyColumn",
Source2, "KeyColumn", [Link])
● Merge Tables (Full Outer Join): = [Link](Source1, "KeyColumn",
Source2, "KeyColumn", [Link])
● Append Queries: = [Link]({Source1, Source2})
Data Cleaning
● Remove Empty Rows: = [Link](Source, each not
[Link]([Link]([Link](_), {"",
null})))
● Remove Rows With Errors: = [Link](Source,
{"Column1", "Column2"})
● Replace Errors: = [Link](Source, {{"Column", 0}})
● Capitalize Each Word: = [Link](Source,{{"Column",
[Link], type text}})
● Remove Non-Printable Characters: =
[Link](Source,{{"Column", [Link], type text}},
{"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"})
Time Series Operations
● Time Series Decomposition: = [Link](Source,
"DateColumn", "ValueColumn", [Link])
By: Waleed Mousa
● Moving Average: = [Link](Source, "MovingAverage", each
[Link]([Link]([Link](Source[ValueColumn], _[Index] -
windowSize), windowSize)))
● Cumulative Sum: = [Link](Source, "CumulativeSum", each
[Link]([Link](Source[ValueColumn], _[Index] + 1)))
● Year-to-Date Sum: = [Link](Source, "YTD", each
[Link]([Link](Source, each [DateColumn] <=
_[DateColumn] and [Link]([DateColumn]) =
[Link](_[DateColumn]))[ValueColumn]))
Statistical Operations
● Standard Deviation: = [Link](Source, "StdDev", each
[Link](Source[Column]))
● Variance: = [Link](Source, "Variance", each
[Link](Source[Column]))
● Correlation: = [Link](Source, "Correlation", each
[Link](Source[Column1], Source[Column2]))
● Linear Regression Coefficients: = [Link](Source,
"ValueColumn", {"Predictor1", "Predictor2"})
Data Export
● Export to CSV: =
[Link]([Link](Source,{{"Column", type
text}}), "[Link]", [Delimiter=','])
● Export to Excel: =
[Link]([Link](Source,{{"Column", type
text}}))
● Write to JSON: = [Link](Source)
● Export to XML: = [Link](Source)
Custom M Functions
● Define a Custom Function: = (x as number) => x * x
● Invoke a Custom Function: = [Link](Source, "Squared", each
CustomFunction([Column]))
● Parameterized Queries: = (param as text) => ...
By: Waleed Mousa
Performance Optimization
● Buffer Table: = [Link](Source)
● Buffer List: = [Link](Source[Column])
● Disable Load: Right-click on a query in the Query Editor and
select "Enable Load" to toggle off.
● Query Folding: Ensuring steps are compatible with query folding
for database sources.
Debugging and Error Handling
● View Native Query: Right-click on a step against a database source
and select "View Native Query".
● Try Otherwise: = try Source otherwise errorRecord
● Add Error Column: = [Link](Source, "Error", each try
[Column] otherwise "Error")
Miscellaneous
● Create a Date Table: = [Link]([Link]("start_date"),
[Link]("end_date"))
● Dynamic Date Filters: = [Link](Source, each [DateColumn]
>= [Link]([Link]()))
● Text to List: = [Link]("string")
● List to Text: = [Link]([Link](Source, each
[Link](_)), ",")
● Index Column: = [Link](Source, "Index", 0, 1,
[Link])
By: Waleed Mousa