Skip to content

Instantly share code, notes, and snippets.

View ImkeF's full-sized avatar

Imke Feldmann ImkeF

View GitHub Profile
let func =
(function as function) =>
let
function_ = function,
Metadata = (functionType) as record =>
let
Help = Value.Metadata(if functionType is type then functionType else Value.Type(functionType))
in
Help,
(Filename as text) =>
let
// Unz-function from: https://querypower.com/2017/03/22/extracting-power-queries-in-m/
Unz = (binaryZip,fileName) =>
let
//shorthand
UInt32 = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32,ByteOrder.LittleEndian),
UInt16 = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16,ByteOrder.LittleEndian),
@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
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 =
(List as list) =>
let
List = List.Buffer(List),
LA =
List.Accumulate(
List.Skip(List),
[Table = Table.FromColumns({List}), Counter = 2],
let func =
(PeriodStartForecast as number, PeriodEndForecast as number, OriginalForecast as table) =>
let
/* Debug Parameters
PeriodEndForecast = PeriodEndForecast,
PeriodStartForecast = PeriodStartForecast,
OriginalForecast = LU_Original_Forecast_Data,
*/
@ImkeF
ImkeF / ExportCsv.pq
Last active March 26, 2019 20:13
M functions
// Exports the current table to csv. Pass full PathFile specification as parameter
(path as text, dataset as table) =>
let
ConvertedPath = Text.Replace(path, "\", "/"),
RScript = R.Execute("write.csv(dataset,"""&ConvertedPath&""")
output <- dataset
",[dataset=dataset]),
output = RScript{[Name="output"]}[Value]
in
output
// splits text where every character before the last character is a number
// matching string will be the first item of the new text, but that can be modified by filling up instead
let
Source = "Appl2e Pe3ar Banan2a Toma4to Potato",
ToList = Text.Split(Source, " "),
#"Converted to Table" = Table.FromColumns({ToList}),
PatternCheck = Table.AddColumn(#"Converted to Table",
"Pattern",
each if List.Contains({"0".."9"}, Text.At([Column1], Text.Length([Column1])-2))
then [Column1]