Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar committed Apr 15, 2024
2 parents 90af8bf + 47d3a57 commit ca4efba
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 8 deletions.
34 changes: 34 additions & 0 deletions Functions/List/List.DotProduct.pq
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
let
// Define metadata for the function, describing its purpose and usage.
metaDocumentation = type function (
list1 as (type list),
list2 as (type list)
) as number meta [
Documentation.Name = "List.DotProduct",
Documentation.Author = "Alexis Olson",
Documentation.LongDescription =
// This is the description of the documentation, it only accepts a handful of HTML tags for formatting.
"
Returns the dot product of two lists.
<p></p>
<li><b>Author: </b>Alexis Olson</li>
<li><b>LinkedIn: </b>https://www.linkedin.com/in/alexis-olson-81726818/</li>
",
Documentation.Examples = {
[
Description = " Compute the dot product of {1, 3, -5} and {4, -2, -1}.",
Code = "List.DotProduct({1, 3, -5}, {4, -2, -1})",
Result = "3 = (1 * 4) + (3 * -2) + (-5 * -1)"
]
}
],
myFunction =
(list1 as list, list2 as list) as number =>
let
Zip = List.Zip({list1, list2}),
Result = List.Sum(List.Transform(Zip, List.Product))
in
Result
in
// Apply the function metadata to myFunction.
Value.ReplaceType(myFunction, metaDocumentation)
53 changes: 53 additions & 0 deletions Functions/List/List.Norm.pq
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
let
// Define metadata for the function, describing its purpose and usage.
metaDocumentation = type function (
inputList as (type list meta [
Documentation.FieldCaption = "List input"
]),
optional p as (type number meta [
Documentation.FieldCaption = "Select any p > 0 to use the Lp norm"
])
) as number meta [
Documentation.Name = "List.Norm",
Documentation.Author = "Alexis Olson",
Documentation.LongDescription =
// This is the description of the documentation, it only accepts a handful of HTML tags for formatting.
"
Returns the norm of the list.
<p>The default is the standard L² Euclidean norm but the optional argument allows any Lp norm.</p>
<li><b>Author: </b>Alexis Olson</li>
<li><b>LinkedIn: </b>https://www.linkedin.com/in/alexis-olson-81726818/</li>
",
Documentation.Examples = {
[
Description = " Default L² (Euclidean) norm. ",
Code = "List.Norm({1, 2, 3})",
Result = "3.7416573867739413 = √(1² + 2² + 3²)"
],
[
Description = " L¹ (Taxicab or Manhattan) norm. ",
Code = "List.Norm({1, -1, 1, 0}, 1)",
Result = "3 = |1| + |-1| + |1| + |0|"
],
[
Description = " L∞ (infinity or maximum) norm. ",
Code = "List.Norm({3, -4}, Number.PositiveInfinity)",
Result = "4"
]
}
],
myFunction =
(L as list, optional p as number) as number =>
let
AbsL = List.Transform(L, Number.Abs),
PowerP = List.Transform(AbsL, each Number.Power(_, p ?? 2)),
RootP = Number.Power(List.Sum(PowerP), 1 / (p ?? 2)),
Result =
if p = Number.PositiveInfinity
then List.Max(AbsL)
else RootP
in
Result
in
// Apply the function metadata to myFunction.
Value.ReplaceType(myFunction, metaDocumentation)
10 changes: 9 additions & 1 deletion M.pq
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,27 @@ let
List.Count = List.Count,
List.Dates = List.Dates,
List.Distinct = List.Distinct,
List.DotProduct = List.DotProduct,
List.First = List.First,
List.IsEmpty = List.IsEmpty,
List.MatchesAny = List.MatchesAny,
List.Max = List.Max,
List.Median = List.Median,
List.Min = List.Min,
List.Product = List.Product,
List.RemoveItems = List.RemoveItems,
List.RemoveNulls = List.RemoveNulls,
List.StandardDeviation = List.StandardDeviation,
List.Sum = List.Sum,
List.Transform = List.Transform,
List.Type = List.Type,
List.Zip = List.Zip,
Logical.Type = Logical.Type,
Number.Abs = Number.Abs,
Number.From = Number.From,
Number.IntegerDivide = Number.IntegerDivide,
Number.PositiveInfinity = Number.PositiveInfinity,
Number.Power = Number.Power,
Number.Type = Number.Type,
Order.Descending = Order.Descending,
Record.Field = Record.Field,
Expand Down Expand Up @@ -157,7 +164,8 @@ let
Time.Type = Time.Type,
Type.Is = Type.Is,
Value.ReplaceType = Value.ReplaceType,
Value.Type = Value.Type
Value.Type = Value.Type,
microsoft.com = microsoft.com
]
)
in
Expand Down
2 changes: 2 additions & 0 deletions M_Creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
'Formatting.IsMultiLine',
'gorilla.bi',
'List.Flatten',
'List.Norm',
'List.DotProduct'
'microsoft.com',
'odata.nextLink',
'Table.ToM',
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,22 @@ This library is "imported" into Power Query by following these steps:
Please contribute in the folder category that best suits. If the folder does not exist, create it.

The functions you create will only work if all the Power Query native functions used are declared in the [**M.pq**](M.pq) file. You can run the [M_Creator.py](M_Creator.py) Python script to locate all the functions via regex and rewrite an M.pq file.
### How can you contribute?

- New functions.
- Upgrading existing functions.
- Adding documentation to existing functions.
- Solve open issues.

### Contributing guidelines
1. Make sure to add the corresponding credits to your code. Plagiarism will not be tolerated.
2. **Document your functions,** for this you can use the file [M_FxDocTemplate.pq](https://github.com/OscarValerock/PowerQueryFunctions/blob/main/M_FxDocTemplate.pq "M_FxDocTemplate.pq")
3. Comment your code.
4. Have fun! 🎉
3. Do not duplicate code. Example: if you have a better date table, upgrade the existing instead of creating a new one.
4. Comment your code.
5. Have fun! 🎉

## Other libraries
Below, you will find other handy PowerQuery libraries that could come in handy:
Below, you will find other handy PowerQuery libraries:
1. [M-tools](https://github.com/acaprojects/m-tools/tree/master) by Kim Burgess
2. [M](https://github.com/ImkeF/M) by Imke Feldmann
3. [M Custom Functions](https://github.com/tirnovar/m-custom-functions) by Štěpán Rešl
Expand All @@ -48,8 +55,9 @@ Below, you will find other handy PowerQuery libraries that could come in handy:
## License
This project is licensed under the [MIT License](LICENSE).
<!--stackedit_data:
eyJoaXN0b3J5IjpbMTEyODYyNzc5OSw4Njc4Njc5MDYsNTEyOD
UyMTQsLTYzMDQ5ODQ2Niw2OTkyOTczMjIsLTExNjM3OTIyNjYs
MTA5NjIyODU5NywyMTA0NzczNywxMzE1ODE4NTQ2LDExODI5OD
I5ODYsLTUzMjA2MDQzLC0xNjc1NDM3Njg4XX0=
eyJoaXN0b3J5IjpbLTE0OTg3NDM1MjYsLTEyNzAxODE3NjksMT
EyODYyNzc5OSw4Njc4Njc5MDYsNTEyODUyMTQsLTYzMDQ5ODQ2
Niw2OTkyOTczMjIsLTExNjM3OTIyNjYsMTA5NjIyODU5NywyMT
A0NzczNywxMzE1ODE4NTQ2LDExODI5ODI5ODYsLTUzMjA2MDQz
LC0xNjc1NDM3Njg4XX0=
-->

0 comments on commit ca4efba

Please sign in to comment.