Table.TransformColumnTypes

語法

Table.TransformColumnTypes(
    table as table,
    typeTransformations as list,
    optional culture as any
) as table

關於

使用可選的文化特性,將轉換操作應用到指定的欄位,以傳回資料表。

  • table:要轉換的輸入數據表。
  • typeTransformations:要套用的類型轉換。 單一轉換的格式為 { 資料行名稱,類型值 }。 轉換清單可用來一次變更多個數據行的類型。 如果欄位不存在,就會引發錯誤。
  • culture:(選擇性)轉換數據行類型時要使用的文化特性(例如,“en-US”。 如果為 culture指定記錄,它可以包含下列欄位:
    • Culture:轉換數據行類型時要使用的文化特性(例如,“en-US”。
    • MissingField:若欄位不存在,則會產生錯誤,除非該欄位提供替代行為(例如, MissingField.UseNullMissingField.Ignore)。

typeTransformations 參數中的型別值可以是 any、所有 number 類型、text、所有 datetimedatetimedatetimezoneduration 類型、logicalbinarylistrecordtablefunction 類型對此參數無效。

列出在 typeTransformations 中的每個欄位,對應於指定型別值的 .From 方法通常用於執行轉換。 例如,若 Currency.Type 欄位給出型別值,轉換函 Currency.From 數會套用該欄位中的每個值。

範例 1

將第一個數據列中的數位值轉換成文字值。

使用方式

let
    Source = #table(type table [a = number, b = number],
    {
        {1, 2},
        {3, 4}
    }),
    #"Transform Column" = Table.TransformColumnTypes(
        Source,
        {"a", type text}
    )
in
    #"Transform Column"

輸出

#table(type table [a = text, b = number],
{
    {"1", 2},
    {"3", 4}
})

範例 2

將表格中的日期轉換為對應的法文文字。

使用方式

let
    Source = #table(type table [Company ID = text, Country = text, Date = date],
    {
        {"JS-464", "USA", #date(2024, 3, 24)},
        {"LT-331", "France", #date(2024, 10, 5)},
        {"XE-100", "USA", #date(2024, 5, 21)},
        {"RT-430", "Germany", #date(2024, 1,18)},
        {"LS-005", "France", #date(2023, 12, 31)},
        {"UW-220", "Germany", #date(2024, 2, 25)}
    }),
    #"Transform Column" = Table.TransformColumnTypes(
        Source,
        {"Date", type text},
        "fr-FR"
    )
in
    #"Transform Column"

輸出

#table(type table [Company ID = text, Country = text, Date = text],
{
    {"JS-464", "USA", "24/03/2024"},
    {"LT-331", "France", "05/10/2024"},
    {"XE-100", "USA", "21/05/2024"},
    {"RT-430", "Germany", "18/01/2024"},
    {"LS-005", "France", "31/12/2023"},
    {"UW-220", "Germany", "25/02/2024"}
})

範例 3

將表格中的日期轉換為德文文字對應,並將表格中的值轉換為百分比。

使用方式

let
    Source = #table(type table [Date = date, Customer ID = text, Value = number],
    {
        {#date(2024, 3, 12), "134282", .24368},
        {#date(2024, 5, 30), "44343", .03556},
        {#date(2023, 12, 14), "22", .3834}
    }),
    #"Transform Columns" = Table.TransformColumnTypes(
        Source,
        {{"Date", type text}, {"Value", Percentage.Type}},
        "de-DE")
in
    #"Transform Columns"

輸出

#table(type table [Date = text, Customer ID = text, Value = Percentage.Type],
{
    {"12.03.2024", "134282", .24368},
    {"30.05.2024", "44343", .03556},
    {"14.12.2023", "22", .3834}
})

範例 4

套用記錄值的 culture轉換。

使用方式

let
    Source = #table(type table [Company ID = text, Country = text, Date = date],
    {
        {"JS-464", "USA", #date(2024, 3, 24)},
        {"LT-331", "France", #date(2024, 10, 5)},
        {"XE-100", "USA", #date(2024, 5, 21)},
        {"RT-430", "Germany", #date(2024, 1,18)},
        {"LS-005", "France", #date(2023, 12, 31)},
        {"UW-220", "Germany", #date(2024, 2, 25)}
    }),
    #"Transform Column" = Table.TransformColumnTypes(
        Source,
        {{"Date", type text}, {"NewColumn", type number}},
        [Culture="fr-FR", MissingField=MissingField.UseNull]
    )
in
    #"Transform Column"

輸出

#table(type table [Company ID = text, Country = text, Date = text, NewColumn = number],
{
    {"JS-464", "USA", "24/03/2024", null},
    {"LT-331", "France", "05/10/2024", null},
    {"XE-100", "USA", "21/05/2024", null},
    {"RT-430", "Germany", "18/01/2024", null},
    {"LS-005", "France", "31/12/2023", null},
    {"UW-220", "Germany", "25/02/2024", null}
})