Code Monkey home page Code Monkey logo

m-tools's Introduction

M-tools

A collection a useful tools for building queries in the Power Query Formula Language ("M") used by Microsoft Power BI.

Usage

Power BI does not (appear) to currently support external dependencies so setup is a little rudimentary.

  1. Create a new blank query.
  2. Copy the content of M.pq to this query using the advanced editor.
  3. Name the query M.

Functions may then be access from other queries as records on M. For example, Pipe may be invoked as:

M[Pipe]({functionA, functionB, ...})

Contributing

Do not directly edit M.pq. This is built from the components defined in src/. It is provided, preassembled for convenience only.

To add functions, create new *.pq files within src/ containing the expression body and any relevant documentation (see the other files for reference). When compiled, the expression will be bound to the name of the file.

Building

runhaskell build.hs

M Language specification

Type system

Internal function references

m-tools's People

Contributors

kimburgess avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

m-tools's Issues

A better partial function ?

Unlike your partial code, the code below will return a function with multiple parameters rather than a single parameter of type list. Perhaps you would like to add it to your module.

shared
Record.RemoveFirstN = (record as record,numToRemove as number) =>
    let
        FieldNames = Record.FieldNames(record),
        RemoveFieldNames = List.FirstN(FieldNames,numToRemove)
    in
        Record.RemoveFields(record,RemoveFieldNames);

shared
_.Partial = 
        (f as function, a as list) => 
            let
                GetMinNumberOfArgs = (numArgs as number, numRequired as number, numFixed as number) as number =>
                    let 
                        NumRemaining = numArgs - numFixed,
                        MinArgs = if numArgs=numRequired then NumRemaining else
                            if numRequired < numFixed then 0
                            else
                                numRequired - numFixed
                    in
                        MinArgs,
                GetPartialSignature = (functionType as type,numFixed as number,parameters as record) =>
                    let
                        ReturnType = Type.FunctionReturn(functionType),
                        RemainingParameters = Record.RemoveFirstN(parameters,numFixed)
                    in
                        [ReturnType = ReturnType,Parameters = RemainingParameters],
                CreatePartialFunction = (newFunctionType as type,fixed as list) => 
                    let
                        ToExecute = (remaining as list) => Function.Invoke(f, fixed & remaining),
                        Partial = Function.From(newFunctionType,ToExecute)
                    in
                        Partial,

                NumFixed = List.Count(a),
                FunctionType = Value.Type(f),
                NumRequiredParameters = Type.FunctionRequiredParameters(FunctionType),
                Parameters = Type.FunctionParameters(FunctionType),
                

                MinNumberOrArgs = GetMinNumberOfArgs(Record.FieldCount(Parameters),NumRequiredParameters, NumFixed),
                NewFunctionType = Type.ForFunction(GetPartialSignature(FunctionType,NumFixed,Parameters),MinNumberOrArgs),

                PartialFunction = CreatePartialFunction(NewFunctionType,a)
            in
                PartialFunction;

Accompanying tests ( needs some additional set up code as per power query unit testing

shared MyExtension.UnitTest =
[
    
    TestFn =  (a as number, b as number, optional opt as number) => 
                let 
                    NotOptional = if opt <> null then opt else 0,
                    Result = a + b + NotOptional
                in
                        Result,
    PartialTest1 = _.Partial(TestFn,{9}),
    PartialTest2 = _.Partial(TestFn,{9,1}),
   
    facts =
    {        
        Fact("Partial fixed 1 parameter - execute don't provide optional",
             10,
             PartialTest1(1)
		),
        Fact("Partial fixed 1 parameter - execute provide optional",
             12,
             PartialTest1(1,2)
		),
        Fact("Partial fixed 2 parameters - execute provide optional",
             13,
             PartialTest2(3)
		),
        Fact("Partial fixed 2 parameters - execute don't provide optional",
             10,
             PartialTest2()
		),
        Fact("Should throw when don't provide required",
             "1 arguments were passed to function which expects between 2 and 3.",
             let
                 Errored = try
                    PartialTest1()
             in
                 Errored[Error][Message]
		),
        Fact("Should throw when provide incorrect parameter type",
             "We cannot convert the value ""incorrect type"" to type Number.",
             let
                 Errored = try
                    PartialTest1("incorrect type")
             in
                 Errored[Error][Message]
		),
        Fact("Record.RemoveFirstN 1",
             [Second="Second",Third="Third"],
             Record.RemoveFirstN([First="First",Second="Second",Third="Third"],1)
		),
        Fact("Record.RemoveFirstN 2",
             [Third="Third"],
             Record.RemoveFirstN([First="First",Second="Second",Third="Third"],2)
		)
    },

    report = Facts.Summarize(facts)
][report];


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.