Microsoft Power Fx Documentation

repository·main·Indexed 25 days ago

https://github.com/microsoft/power-fx

A strongly typed, declarative, and functional low-code programming language based on spreadsheet-like formulas. This documentation covers the core language implementation, the Intermediate Representation (IR) design for backend consumption, and the Enhanced Connector Protocol for connecting to tabular data via OData-based RESTful APIs. It includes details on available NuGet packages such as Microsoft.PowerFx.Core and Microsoft.PowerFx.Interpreter, as well as guidance on query delegation and metadata specifications.

Tokens
13.5K
Snippets
20
Records
77
Agent score
85%

What's inside Microsoft Power Fx

  1. Overview of Microsoft.PowerFx.Connectors

    main

    The Microsoft.PowerFx.Connectors library bridges Power Fx with REST APIs defined by OpenAPI/Swagger documents. It enables developers to treat Swagger operations and Common Data Model (CDP) datasets as first-class Power Fx symbols.

    Key capabilities include:

    1. Parsing: Converting an OpenApiDocument into ConnectorFunction/TexlFunction (for actions) or CdpTable objects (for tabular data).
    2. Binding: Registering these symbols into a PowerFxConfig or SymbolValues bag for engine resolution.
    3. Execution: Translating Power Fx FormulaValue arguments into HTTP requests, executing them via an HttpMessageInvoker, and deserializing the response back into FormulaValue.
  2. Overview of Microsoft Power Fx

    main
    Microsoft Power Fx is a low-code, general-purpose programming language based on spreadsheet-like formulas. It is a strongly typed, declarative, and functional language that supports imperative logic and state management. While it originated in Power Apps canvas apps, it is being extracted to be available as a standalone language for broader use.
  3. Understand Enhanced Connector Protocol

    main

    Enhanced connectors allow the Power Platform to connect to tabular data in external datasources by describing a dynamic set of tables, schemas, and capabilities. Unlike action connectors that use a fixed list of operations via OpenAPI, Enhanced connectors use a RESTful protocol based on OData and are augmented with metadata to be self-describing.

    Key components of an Enhanced connector:

    1. Metadata: The ability to describe the target datasource's structure.
    2. Transpiler: The ability to accept an OData request and convert it into a query for the underlying datasource (primarily for Read operations).
    3. CRUD Operations: Support for Create, Update, and Delete operations.
  4. Understand Tabular Connectors (CDP) in Power Fx

    main

    Tabular connectors use the Common Data Provider (CDP) protocol rather than Swagger operation lists. They rely on a fixed set of REST endpoints to enumerate datasets, discover tables, retrieve schemas, and perform read/write operations.

    In the Power Fx engine, these connectors return a CdpTableValue which implements IDelegatableTableValue. This allows Power Fx functions like Filter, Sort, FirstN, CountRows, and GroupBy to be translated into OData query parameters and delegated to the server instead of being processed in memory.

  5. Understand the Power Fx Intermediate Representation (IR) design

    main

    The Power Fx IR is a refined Intermediate Representation designed to simplify backend consumption (such as SQL for CDS or JSTranslator). It normalizes complex Power Fx logic into explicit nodes to prevent backends from having to reimplement core logic like coercion matrices or operator overloading.

    Key normalization features include:

    • Explicit Coercion: Coercion is represented as an explicit node using a single flattened enum, rather than requiring backends to implement a complex coercion matrix.
    • Operator Normalization: Operators that can function as both operators and functions (e.g., And, Or, Power, Concatenate) are normalized to function representations.
    • Symbol Binding: The As keyword is treated as a symbol binding mechanism that does not impact code generation.
    • Lazy Evaluation: Function parameters that are not always evaluated are represented using Lambda nodes.
  6. Consume Power Fx Daily Builds

    main

    Daily builds of Power Fx packages are published to Azure Artifacts. To consume the most recent packages, add the following package source to your NuGet configuration:

    https://pkgs.dev.azure.com/Power-Fx/7dd30b4a-31be-4ac9-a649-e6addd4d5b0a/_packaging/PowerFx/nuget/v3/index.json

    https://pkgs.dev.azure.com/Power-Fx/7dd30b4a-31be-4ac9-a649-e6addd4d5b0a/_packaging/PowerFx/nuget/v3/index.json
  7. Understand the Action Connector runtime invocation pipeline

    main

    When a Power Fx formula calls an action connector function, the following pipeline is executed:

    1. Parameter Binding: HttpFunctionInvoker.ConvertToNamedParameters converts positional FormulaValue[] arguments into a Dictionary<string, FormulaValue>, handling optional arguments and defaults.
    2. Request Building: HttpFunctionInvoker.BuildRequest constructs the HttpRequestMessage:
      • Parameters: Places values in Path, Query, Header, or Cookie based on the OpenAPI spec.
      • Formatting: Converts DateTimeValue to ISO 8601 UTC (yyyy-MM-ddTHH:mm:ss.fffZ) and DateValue to yyyy-MM-dd.
      • Body: Selects a FormulaValueSerializer based on the Content-Type (JSON, Form, Text, or Multipart).
      • Placeholders: Substitutes {connectionId}-style placeholders using GlobalContext.ConnectorValues.
    3. Execution: The request is sent via _httpClient.SendAsync.
    4. Decoding: HttpFunctionInvoker.DecodeResponseAsync inspects the status code. If successful, it uses FormulaValueJSON.FromJson to materialize the response against the function's ReturnType. On HTTP errors, it returns an ErrorValue containing an HttpExpressionError (unless throwOnError is configured).
  8. Report bugs and feature requests via GitHub Issues

    main
    To report bugs or request new features for Microsoft Power Fx, use the GitHub Issues tracker. Before creating a new issue, search the existing issues to ensure your topic has not already been reported to avoid duplicates.
  9. Configure Power Fx daily builds in Visual Studio

    main

    To use daily Power Fx builds in Visual Studio, you must manually add the NuGet package source to your IDE settings. This must be done on every machine used for development.

    1. Go to Tools > Options > NuGet Package Manager > Package Sources.
    2. Click the green plus icon to add a new source.
    3. Enter the following details:
      • Name: SDK
      • Source: https://pkgs.dev.azure.com/Power-Fx/7dd30b4a-31be-4ac9-a649-e6addd4d5b0a/_packaging/PowerFx/nuget/v3/index.json
    4. To install a package, open the Package Manager Console and use the Install-Package command.
    Install-Package Microsoft.PowerFx.Core
  10. End-to-end example: Register and run an Action Connector

    main

    This example demonstrates how to load an OpenAPI document, register it as an action connector in a PowerFxConfig, set up the required RuntimeConfig with a custom BaseRuntimeConnectorContext, and evaluate a formula using the RecalcEngine.

    using System;
    using System.IO;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.OpenApi.Readers;
    using Microsoft.PowerFx;
    using Microsoft.PowerFx.Connectors;
    using Microsoft.PowerFx.Types;
    
    public static class ActionConnectorSample
    {
        public static async Task RunAsync()
        {
            // 1. Load the swagger.
            using var stream = File.OpenRead("MyConnector.swagger.json");
            var openApiDoc = new OpenApiStreamReader().Read(stream, out _);
    
            // 2. Create a PowerFxConfig and register the connector.
            var config = new PowerFxConfig();
            var settings = new ConnectorSettings("MyConnector")
            {
                IncludeInternalFunctions = false,
                AllowUnsupportedFunctions = false,
            };
    
            // OpenApiParser.GetFunctions is called under the hood by AddActionConnector.
            IReadOnlyList<ConnectorFunction> functions =
                config.AddActionConnector(settings, openApiDoc, new ConsoleLogger());
    
            // 3. Build an HttpMessageInvoker that will actually make the network call.
            using var httpClient = new HttpClient();
    
            // 4. Create a RuntimeConfig with the connector runtime context.
            var runtimeCtx = new MyConnectorRuntimeContext("MyConnector", httpClient);
            var runtimeConfig = new RuntimeConfig().AddRuntimeContext(runtimeCtx);
    
            // 5. Evaluate a Power Fx expression that uses the generated function.
            var engine = new RecalcEngine(config);
            var result = await engine.EvalAsync(
                @"MyConnector.SendEmail({ to: ""a@b.com"", subject: ""Hi"" })",
                CancellationToken.None,
                options: new ParserOptions { AllowsSideEffects = true },
                runtimeConfig: runtimeConfig);
    
            Console.WriteLine(result.ToObject());
        }
    
        private sealed class MyConnectorRuntimeContext : BaseRuntimeConnectorContext
        {
            private readonly string _ns;
            private readonly HttpMessageInvoker _invoker;
            public MyConnectorRuntimeContext(string ns, HttpMessageInvoker invoker) { _ns = ns; _invoker = invoker; }
            public override HttpMessageInvoker GetInvoker(string @namespace) => _invoker;
            public override TimeZoneInfo TimeZoneInfo => TimeZoneInfo.Utc;
        }
    }
  11. Implement the Enhanced Connector Resource Hierarchy

    main

    Enhanced connectors follow a 2-tier namespace hierarchy for tabular data:

    • Dataset: A collection that exposes multiple tables (e.g., a SQL Database or a SharePoint Site).
    • Table: Contains rows and columns (e.g., a SQL Table or a SharePoint List).
    • Item: Represents a single row within a table.

    Example mapping:

    ConnectorDatasetTable
    SQLDatabaseTable
    SharePointSiteList