Skip to content

Commit

Permalink
Merge pull request #7 from tycho01/master
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
IvanBond authored Sep 21, 2017
2 parents dd9b00a + 9395fba commit 4369e3f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
16 changes: 8 additions & 8 deletions Load.pq
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Allows dynamically loading an M function from a text file (extension: .m) in a given folder for use in Power Query. This allows you to easily reuse a set of functions in multiple workbooks without having to sync each change to all files using it.
Allows dynamically loading an M function from a text file (extension: .pq) in a given folder for use in Power Query. This allows you to easily reuse a set of functions in multiple workbooks without having to sync each change to all files using it.

The point here is that by separating universally useful functions from an individual workbook, you will feel encouraged to use more modular code, solving each common sub-problem only once, rather than remaining stuck in 'vanilla' M and resolving the same problems repeatedly.

Expand All @@ -10,11 +10,11 @@ Nevertheless, if the function in question has already been imported into the wor
Using Load() would not only allow you to use functions in their intended naming conventions (i.e. Text.ReplaceAll rather than with the period replaced by an underscore), but would technically also allow you to add additional wrapper functions around your code, which could be used to enable persistent memoization (using say Redis) or code profiling calls... though presumably no-one has done this so far yet.

Parameters:
fnName: name of the text file you wish to load without the .m extension
fnName: name of the text file you wish to load without the .pq extension
optional BasePath: the file path to look in for the text file; default path hardcoded

Usage:
// loads the function Type.ToText from file 'Type.ToText.m' in the load path
// loads the function Type.ToText from file 'Type.ToText.pq' in the load path
let
Type.ToText = Load("Type.ToText")
in
Expand All @@ -41,15 +41,15 @@ let

BasePath = if (BasePath <> null) then BasePath else DefaultPath,
Path = BasePath & (if Text.End(BasePath, 1) <> "\" then "\" else ""),
File = Path & fnName & ".m",
File = Path & fnName & ".pq",

Function = try Expression.Evaluate(Text.Replace(fnName, ".", "_"), #shared) //if already imported into the workbook just use the existing one
otherwise try Expression.Evaluate(Text.FromBinary(Binary.Buffer(File.Contents(File))), #shared) //if not imported yet try loading it from the text file in the folder
otherwise Expression.Evaluate( Text.FromBinary(Binary.Buffer(Web.Contents(GitHubPath & fnName & ".m"))), #shared) // if folder not found - take from GitHubPath
otherwise Expression.Evaluate( Text.FromBinary(Binary.Buffer(Web.Contents(GitHubPath & fnName & ".pq"))), #shared) // if folder not found - take from GitHubPath
in
Function
/*
Here comes old Load.m function:
Here comes old Load.pq function:

(fnName as text, optional BasePath as text) as function =>

Expand All @@ -59,8 +59,8 @@ let
//DefaultPath = "D:\pquery",
BasePath = if (BasePath<>null) then BasePath else DefaultPath,
Path = BasePath & (if Text.End(BasePath, 1) <> "\" then "\" else ""),
File = Path & fnName & ".m",
AltFile = Path & Text.Replace(fnName, "_", ".") & ".m", //just in case...
File = Path & fnName & ".pq",
AltFile = Path & Text.Replace(fnName, "_", ".") & ".pq", //just in case...
Source = Text.FromBinary(Binary.Buffer(
try File.Contents(File)
otherwise File.Contents(AltFile)
Expand Down
10 changes: 5 additions & 5 deletions LoadFunctionFromGithub.pq
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Allows dynamically loading an M function from a text file (extension: .m) for use in Power Query.
Allows dynamically loading an M function from a text file (extension: .pq) for use in Power Query.
This allows you to easily reuse a set of functions in multiple workbooks without having to sync each change to
all files using it.

Expand All @@ -20,11 +20,11 @@ also allow you to add additional wrapper functions around your code, which could
persistent memoization (using say Redis) or code profiling calls... though presumably no-one has done this so far yet.

Parameters:
fnName: name of the text file you wish to load without the .m extension
fnName: name of the text file you wish to load without the .pq extension
optional BasePath: the file path to look in for the text file; default path hardcoded

Usage:
// loads the function Type.ToText from file 'Type.ToText.m' in the load path
// loads the function Type.ToText from file 'Type.ToText.pq' in the load path
let
Type.ToText = Load("Type.ToText")
in
Expand All @@ -51,9 +51,9 @@ let

BasePath = if (BasePath <> null) then BasePath else DefaultPath,
Path = BasePath & (if Text.End(BasePath, 1) <> "\" then "\" else ""),
File = Path & fnName & ".m",
File = Path & fnName & ".pq",

Function = try Expression.Evaluate(Text.FromBinary(Binary.Buffer(File.Contents(File))), #shared)
otherwise Expression.Evaluate( Text.FromBinary(Binary.Buffer(Web.Contents(GitHubPath & fnName & ".m"))), #shared)
otherwise Expression.Evaluate( Text.FromBinary(Binary.Buffer(Web.Contents(GitHubPath & fnName & ".pq"))), #shared)
in
Function
2 changes: 1 addition & 1 deletion LoadPath.example.pq
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Excel.CurrentWorkbook(){[Name="Table1"]}[Content]{0}[Path]
// Copy this file to `LoadPath.m`, and replace its contents with your query path, like this:
// Copy this file to `LoadPath.pq`, and replace its contents with your query path, like this:
// "D:\pquery\"
10 changes: 5 additions & 5 deletions Old.Load.pq
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Allows dynamically loading an M function from a text file (extension: .m) in a given folder for use in Power Query. This allows you to easily reuse a set of functions in multiple workbooks without having to sync each change to all files using it.
Allows dynamically loading an M function from a text file (extension: .pq) in a given folder for use in Power Query. This allows you to easily reuse a set of functions in multiple workbooks without having to sync each change to all files using it.

The point here is that by separating universally useful functions from an individual workbook, you will feel encouraged to use more modular code, solving each common sub-problem only once, rather than remaining stuck in 'vanilla' M and resolving the same problems repeatedly.

Expand All @@ -10,11 +10,11 @@ Nevertheless, if the function in question has already been imported into the wor
Using Load() would not only allow you to use functions in their intended naming conventions (i.e. Text.ReplaceAll rather than with the period replaced by an underscore), but would technically also allow you to add additional wrapper functions around your code, which could be used to enable persistent memoization (using say Redis) or code profiling calls... though presumably no-one has done this so far yet.

Parameters:
fnName: name of the text file you wish to load without the .m extension
fnName: name of the text file you wish to load without the .pq extension
optional BasePath: the file path to look in for the text file; defaults to the path specified in the LoadPath query.

Usage:
// loads the function Type.ToText from file 'Type.ToText.m' in the load path
// loads the function Type.ToText from file 'Type.ToText.pq' in the load path
let
Type.ToText = Load("Type.ToText")
in
Expand All @@ -38,8 +38,8 @@ let
//DefaultPath = "D:\pquery",
BasePath = if (BasePath<>null) then BasePath else DefaultPath,
Path = BasePath & (if Text.End(BasePath, 1) <> "\" then "\" else ""),
File = Path & fnName & ".m",
AltFile = Path & Text.Replace(fnName, "_", ".") & ".m", //just in case...
File = Path & fnName & ".pq",
AltFile = Path & Text.Replace(fnName, "_", ".") & ".pq", //just in case...
Source = Text.FromBinary(Binary.Buffer(
try File.Contents(File)
otherwise File.Contents(AltFile)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a collection of functions written in the M language for use in Microsoft

## Introduction

Rather than manually adding the functions to different Excel workbooks, users can instead leave their files in any directory, and either batch import them into your workbook using Excel 2016 VBA (see my sample workbook [here](http://1drv.ms/1GmrhDl)), or by dynamically loading them into Power Query using something like the `Load()` function (see `Load.m`). To use Load() you'll need to add it to every applicable workbook though. You can hard-code in the path to the folder where you put the functions from this repository, or set it in the specified cell in the above-mentioned sample workbook for use in that.
Rather than manually adding the functions to different Excel workbooks, users can instead leave their files in any directory, and either batch import them into your workbook using Excel 2016 VBA (see my sample workbook [here](http://1drv.ms/1GmrhDl)), or by dynamically loading them into Power Query using something like the `Load()` function (see `Load.pq`). To use Load() you'll need to add it to every applicable workbook though. You can hard-code in the path to the folder where you put the functions from this repository, or set it in the specified cell in the above-mentioned sample workbook for use in that.

To manually add the Load query, click 'From Other Sources' in the Power Query ribbon tab, select 'Blank Query' (bottom option), open the Advanced Editor in the View tab, and paste in the below snippet (after adjusting file path). Click Done, name the query 'Load', and click 'Apply & Close' in the Home tab.

Expand All @@ -21,9 +21,9 @@ Admittedly, Microsoft languages have rarely been known for encouraging open-sour
### to use M code in workbooks without having to import every query/function:

* [get](https://github.com/tycho01/pquery/archive/master.zip) and unzip this repo, or in case you'd like to contribute back, open a command prompt in your desired location (in Windows Explorer type `cmd` in the address bar) and run command `git clone https://github.com/tycho01/pquery.git`.
* copy [`LoadPath.example.m`](https://github.com/tycho01/pquery/blob/master/LoadPath.example.m) as `LoadPath.m` and replace its entire content with the path where you put the query files; e.g. `"D:\pquery\"`.
* manually import the `Load.m` and `LoadPath.m` functions into your workbook, keeping their names as `Load` and `LoadPath`.
* now use the Load function to load queries from the folder you specified. i.e. if you write `Text_Between = Load("Text.Between"),`, it's going to give you the function located at `YOUR_PATH\Text.Between.m`.
* copy [`LoadPath.example.pq`](https://github.com/tycho01/pquery/blob/master/LoadPath.example.pq) as `LoadPath.pq` and replace its entire content with the path where you put the query files; e.g. `"D:\pquery\"`.
* manually import the `Load.pq` and `LoadPath.pq` functions into your workbook, keeping their names as `Load` and `LoadPath`.
* now use the Load function to load queries from the folder you specified. i.e. if you write `Text_Between = Load("Text.Between"),`, it's going to give you the function located at `YOUR_PATH\Text.Between.pq`.

### to allow sharing your workbook:

Expand Down
File renamed without changes.

0 comments on commit 4369e3f

Please sign in to comment.