Guide

Connect your data to Power BI

Load orders, customers, calls and transactions into Power BI Desktop with a read-only API key. No connector needed.

  1. 1Create a read-only API key

    Start in sandbox. Go to Settings → Apps & API → API keys and create a key with only the view scopes below. The key is shown once and reads only your workspace.

    Scopes
    sales.orders.view
    crm.contacts.view
    crm.opportunities.view
    call.calls.view
    payment.transactions.view

    API keys →Sandbox →

  2. 2Create parameters

    In Power Query, open Manage Parameters and create 3 parameters. Do not share a file that contains your API key.

    Parameters
    BaseUrl    = https://api-dev.nextx.vn
    ApiKey     = <your API key>
    WindowDays = 35
  3. 3Create the data function

    Create a Blank Query, open Advanced Editor, paste the code and name it fnNextX. It follows pages and honours Retry-After on 429.

    fnNextX
    // Query name: fnNextX. Needs the BaseUrl and ApiKey parameters.
    (path as text, columns as list, optional filters as nullable record) as table =>
    let
        Filters = if filters = null then [] else filters,
        PageSize = 100,
        Fetch = (page as number, tries as number) as record =>
            let
                Resp = Web.Contents(BaseUrl, [
                    RelativePath = path,
                    Query = Filters & [pageNumber = Text.From(page), pageSize = Text.From(PageSize)],
                    Headers = [#"X-Api-Key" = ApiKey, Accept = "application/json"],
                    ManualStatusHandling = {429, 500, 502, 503, 504},
                    IsRetry = tries > 0
                ]),
                Meta = Value.Metadata(Resp),
                Status = Meta[Response.Status],
                Wait = try Number.From(Meta[Headers][#"Retry-After"]) otherwise Number.Power(2, tries + 1),
                Body =
                    if Status = 200 then Json.Document(Resp)
                    else if tries >= 5 then error Error.Record("API", "HTTP " & Text.From(Status), path)
                    else Function.InvokeAfter(() => @Fetch(page, tries + 1), #duration(0, 0, 0, Wait))
            in
                Body,
        Pages = List.Generate(
            () => [n = 1, body = Fetch(1, 0)],
            each List.Count([body][items]) > 0,
            each [n = [n] + 1, body = Fetch([n] + 1, 0)],
            each [body][items]
        ),
        Rows = List.Combine(Pages),
        Result = Table.FromRecords(Rows, columns, MissingField.UseNull),
        Unique = Table.Distinct(Result, {"id"})
    in
        Unique
  4. 4Load tables and refresh

    Create the orders query from the sample and repeat for other tables. When you publish to Power BI Service, choose Anonymous, turn on Skip test connection, and set the parameters in Settings. Refresh once a day.

    orders
    // Query name: orders. Reloads the last WindowDays days.
    let
        Today = Date.From(DateTimeZone.SwitchZone(DateTimeZone.UtcNow(), 7)),
        From = Date.AddDays(Today, -Number.From(WindowDays)),
        Columns = {"id", "code", "orderType", "status", "salesCustomerId", "branchId", "branchName",
                   "subtotal", "discountAmount", "taxAmount", "shippingFee", "total", "paidAmount",
                   "debtAmount", "paymentMethod", "channelCode", "orderDate", "createdAt",
                   "confirmedAt", "cancelledAt", "itemCount"},
        Source = fnNextX("api/v1/sales/orders", Columns, [
            dateFrom = Date.ToText(Date.AddDays(From, -1), "yyyy-MM-dd") & "T17:00:00Z",
            dateTo = Date.ToText(Today, "yyyy-MM-dd") & "T16:59:59Z"
        ]),
        ToVnTime = (x) => if x = null then null
            else DateTime.From(DateTimeZone.SwitchZone(DateTimeZone.From(x), 7)),
        LocalTime = Table.TransformColumns(Source, {
            {"orderDate", ToVnTime, type datetime},
            {"createdAt", ToVnTime, type datetime},
            {"confirmedAt", ToVnTime, type datetime},
            {"cancelledAt", ToVnTime, type datetime}
        }),
        Typed = Table.TransformColumnTypes(LocalTime, {
            {"subtotal", type number}, {"discountAmount", type number}, {"taxAmount", type number},
            {"shippingFee", type number}, {"total", type number}, {"paidAmount", type number},
            {"debtAmount", type number}, {"itemCount", Int64.Type}
        })
        // orderDate is empty on regular orders. The API filter uses the same rule.
        Effective = Table.AddColumn(Typed, "effectiveOrderDate",
            each if [orderDate] <> null then [orderDate] else [createdAt], type datetime),
        NetRevenue = Table.AddColumn(Effective, "netRevenue",
            each ([total] ?? 0) - ([taxAmount] ?? 0) - ([shippingFee] ?? 0), type number)
    in
        NetRevenue

    Rate limit →Data model →