共用方式為


Function.ScalarVector

語法

Function.ScalarVector(scalarFunctionType as type, vectorFunction as function) as function

關於

傳回類型 scalarFunctionType 的純量函式,此函式會以單一資料列的引數叫用 vectorFunction,並傳回其單一輸出。 此外,當標量函式在輸入數據表的每個數據列上重複套用時,例如在 Table.AddColumn中, vectorFunction 則會針對所有輸入只套用一次。

vectorFunction 會接收一個資料表,其資料行名稱和位置都與 scalarFunctionType 的參數相符。 此表格中的每一列都包含呼叫純量函式的參數,而這些欄位則對應 scalarFunctionType 的參數。

vectorFunction 必須傳回與輸入資料表長度相同的清單,而每個位置的項目必須與在相同位置之輸入資料列上評估純量函式的結果相同。

輸入資料表預計會以流式傳輸進來,因此 vectorFunction 預期會隨著輸入進來時流式傳輸其輸出,一次只處理一個輸入的塊。 特別是,vectorFunction 不能列舉其輸入資料表超過一次。

範例 1

將輸入表的兩欄相乘,方法是以 100 個批次處理輸入。

使用方式

let
    Compute.ScoreScalar = (left, right) => left * right,
    // When Function.ScalarVector batching kicks in, we'll receive all
    // of the inputs for the entire table here at once.
    Compute.ScoreVector = (input) => let
        chunks = Table.Split(input, 100),
        scoreChunk = (chunk) => Table.TransformRows(chunk, each Compute.ScoreScalar([left], [right]))
      in
        List.Combine(List.Transform(chunks, scoreChunk)),
    Compute.Score = Function.ScalarVector(
        type function (left as number, right as number) as number, 
        Compute.ScoreVector
    ),
    Final = Table.AddColumn(
        Table.FromRecords({
            [a = 1, b = 2],
            [a = 3, b = 4]
        }),
        "Result",
        each Compute.Score([a], [b])
    )
in
    Final

Output


Table.FromRecords({
    [a = 1, b = 2, Result = 2],
    [a = 3, b = 4, Result = 12]
})

範例 2

以兩個批次計算測試分數,並填入批次識別碼欄位,以用來驗證批次處理是否如預期般運作。

使用方式

let
  _GradeTest = (right, total) => Number.Round(right / total, 2),
  _GradeTests = (inputs as table) as list => let
    batches = Table.Split(inputs, 2),
    gradeBatch = (batch as table) as list =>
      let
        batchId = Text.NewGuid()
      in
        Table.TransformRows(batch, each [Grade = _GradeTest([right], [total]), BatchId = batchId])
  in
    List.Combine(List.Transform(batches, gradeBatch)),
  GradeTest = Function.ScalarVector(type function (right as number, total as number) as number, _GradeTests),
  Tests = #table(type table [Test Name = text, Right = number, Total = number],
    {
      {"Quiz 1", 3, 4},
      {"Test 1", 17, 22},
      {"Quiz 2", 10, 10}
    }),
  // To break batching, replace [Right] with {[Right]}{0}.
  TestsWithGrades = Table.AddColumn(Tests, "Grade Info", each GradeTest([Right], [Total]), type record),
  // To verify batching, also expand BatchId.
  Final = Table.ExpandRecordColumn(TestsWithGrades, "Grade Info", {"Grade"})
in
  Final

Output

#table(
    type table [Test Name = text, Right = number, Total = number, Grade = number],
    {
      {"Quiz 1", 3, 4, 0.75},
      {"Test 1", 17, 22, 0.77},
      {"Quiz 2", 10, 10, 1}
    }
)