Skip to content

Instantly share code, notes, and snippets.

View ImkeF's full-sized avatar

Imke Feldmann ImkeF

View GitHub Profile
@ImkeF
ImkeF / Table.AddRollingSum
Last active March 12, 2019 22:17
Adds a column with a rolling sum to a table.
let func =
(Table as table, SortColumn as text, AmountColumn as text, NoOfMonthsToGoBack as number) =>
let
fnRunningTotal = (Table as table, SortColumn as text, AmountColumn as text) =>
let
// Sort table and buffer it
Sorted = Table.Buffer(Table.AddIndexColumn(Table.Sort(Table,{{SortColumn, Order.Ascending}}), "Index",1,1)),
// Select the Columns
@ImkeF
ImkeF / Export.CreateGist.pq
Created January 14, 2019 09:05
Creates a secret gist with the parameters provided. Optional parameter to make it public.
let func =
(Description as text, FunctionName as text, Content as text, AccessToken as text, optional public as any) =>
let
Public = if public = null then false else true,
GitRecord = [description=Description, public=Public, files=Record.FromTable(#table({"Name", "Value"}, {{FunctionName, [content=Content]}}))],
URL = "https://api.github.com/gists?access_token=" & AccessToken,
BinaryJson = Json.FromValue(GitRecord),
@ImkeF
ImkeF / Function.QuickBooksTimeActivity.pq
Last active November 18, 2019 20:08
Fetches all available time activity fields from the QuickBooks connector. Optional parameter allows filtering after a certain date.
let func =
(optional StartDate as date) =>
let
Query = QuickBooks.Tables(){[Key="entity"]}[Data],
DateString = Text.From(Date.Year(StartDate)) & "-" & Text.PadStart(Text.From(Date.Month(StartDate)),2,"0") & "-" & Text.PadStart(Text.From(Date.Day(StartDate)),2,"0"),
Source = if StartDate = null then Query("select * from TimeActivity") else Query("select * from TimeActivity where TxnDate > '" & DateString & "'") ,
#"Expanded Column1" = Table.ExpandRecordColumn(Source, "Column1", {"TxnDate", "NameOf", "EmployeeRef", "CustomerRef", "ItemRef", "BillableStatus", "Taxable", "HourlyRate", "BreakHours", "BreakMinutes", "StartTime", "EndTime", "domain", "sparse", "Id", "SyncToken", "MetaData", "Description", "Hours", "Minutes"}, {"TxnDate", "NameOf", "EmployeeRef", "CustomerRef", "ItemRef", "BillableStatus", "Taxable", "HourlyRate", "BreakHours", "BreakMinutes", "StartTime", "EndTime", "domain", "sparse", "Id", "SyncToken", "MetaData", "Description", "Hours", "Minutes"}),
#"Changed Type1" = Tab
@ImkeF
ImkeF / Table.ExportCsvPyhton.pq
Last active January 7, 2021 14:32
Exports table to csv using Python-script.
let func =
(SOURCE as table, PATH as text, optional mode as text, optional index as logical, optional header as logical, optional quoting as text, optional chunksize as number, optional decimal_as_point as text) =>
let
Source = Table.TransformColumnTypes(SOURCE, List.Transform(Table.ColumnNames(SOURCE), each {_, type text})),
Path = PATH,
index = if index = null then "None" else "True",
header = if header = null then "True" else "False",
quoting = if quoting = null then "csv.QUOTE_ALL" else quoting,
chunksize = if chunksize = null then "None" else chunksize,
@ImkeF
ImkeF / Table.GroupDynamicAggregation.pq
Last active November 18, 2019 20:11
Aggregates all columns from the <code>SourceTable</code> that are not included in <code>GroupColumnNames</code> with Sum as default operation. This can be changed in optional parameter <code>AggregationFunction</code>.
let func =
(SourceTable as table, GroupColumnNames as list, optional AggregationFunction as function, optional Type as type) =>
let
/* Debug parameters
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXIGYkMDIGFqoBSrgxAzAomZQcScgEwXIDYGiZkjxFyB2AQkZgEUiwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [GroupColA = _t, GroupColB = _t, SumCol1 = _t, SumCol2 = _t]),
SourceTable = Table.TransformColumnTypes(Source,{{"GroupColA", type text}, {"GroupColB", type text}, {"SumCol1", Int64.Type}, {"SumCol2", Int64.Type}}),
GroupColumns = {"GroupColA", "GroupColB"},
*/
MyType = if Type = null then type number else Type,
let func =
(string as text, separator as any) =>
let
/* Debug parameters
string = "Do I need gloves for Power Query?",
separator = {"s", "need ", "Do ", "?", "g", "for "},
*/
let func =
(string as text, optional PipeReplacement as text, optional DotsReplacement as text) =>
let
Source = string,
Pipe = if PipeReplacement is null then "|" else PipeReplacement,
Dots = if DotsReplacement is null then ".." else DotsReplacement,
SplitByPipe = Text.Split(Source, Pipe),
SplitByDots = List.Transform(SplitByPipe, each Text.Split(_, Dots)),
CreateLists = List.Transform(SplitByDots, each
let func =
(InputText as text, StartDelimiter as text, EndDelimiter as text, optional Alternative_Dummy as text) =>
let
// If the end delimiter contains the same characters than the start delimiter then that portion has to be replaced in the end parameter by a dummy. Otherwise the query fails.
AlternativeDummy = Alternative_Dummy,
Dummy = if AlternativeDummy is null then "*+*" else AlternativeDummy,
String = InputText,
ParaStart = StartDelimiter,
ParaEnd = EndDelimiter,
let
func = (Table as table, SortColumn as text, AmountColumn as text) =>
let
/* Debug parameters
Table = #"Price Paid",
SortColumn = "Date",
AmountColumn = "Price paid",
*/
let func =
(YList as list, NoOfIntervalls as number) =>
let
Source = Table.FromColumns({YList}),
xAxis = Table.AddIndexColumn(Source, "Index", 1, 1),
Rename1 = Table.RenameColumns(xAxis,{{"Column1", "y"}, {"Index", "x"}}),
AvgX = List.Average(Rename1[x]),
AvgY = List.Average(Rename1[y]),
x = Table.AddColumn(Rename1, "xX", each [x]-List.Average(Rename1[x])),
y = Table.AddColumn(x, "yY", each [y]-List.Average(x[y])),