Share via


使用目標來選取要處理的資料欄位

在 Power BI 編輯模式中,[ 欄位] 窗格 會顯示模型上具有所有資料欄位的所有資料表。 您可以在資料欄位中排序、篩選或配量資料,以影響資料在報表、頁面或視覺效果中的顯示方式。

Power BI 內嵌分析篩選器、交叉分析篩選器或排序需要目標物件,以指定要處理的資料欄位。 當您:使用 ITarget 來提供目標資料欄位:

目標型別

資料欄位可以是資料行、階層層級、 匯總資料 行或階層層級,或匯出 量值。 介面的目標型別 ITarget 包括:

  • IColumnTarget
  • IHierarchyLevelTarget
  • IMeasureTarget
  • IColumnAggrTarget
  • IHierarchyLevelAggrTarget

資料行

使用 IColumnTarget 以資料表中的指定資料行為目標:

target: {
    $schema: "http://powerbi.com/product/schema#column",
    table: "<table name>",
    column: "<column name>"
}

階層層級

使用 IHierarchyLevelTarget ,以資料表中階層內的指定階層層級為目標:

target: {
    $schema: "http://powerbi.com/product/schema#hierarchyLevel",
    table: "<table name>",
    hierarchy: "<hierarchy name>",
    hierarchyLevel: "<hierarchy level name>"
}

Measure

使用 IMeasureTarget 以資料表中的指定量值為目標。 如果目標系結至視覺效果,且其值會顯示為總計百分比,則 的值 percentOfGrandTotal 會設定為 true。

target: {
    $schema: "http://powerbi.com/product/schema#measure",
    table: "<table name>",
    measure: "<measure name>",
    percentOfGrandTotal?: boolean
}

彙總函式

您可以指定資料行和階層層級目標的彙總函式。 變數 aggregationFunction 是選擇性的,如果您未提供變數,資料欄位會使用預設 Sum 匯總函數。 的其他支援值為 aggregationFunction

  • Avg
  • DoNotSummarize
  • Count
  • CountNonNull
  • Max
  • Median
  • Min
  • StandardDeviation
  • Variance

匯總資料行

使用 IColumnAggrTarget 在資料表中指定的資料行上指定彙總函式。 如果目標系結至視覺效果,且其值會顯示為總計的百分比,則 'percentOfGrandTotal' 的值會設定為 true。

target: {
    $schema: "http://powerbi.com/product/schema#columnAggr",
    table: "<table name>",
    column: "<column name>",
    aggregationFunction: "<aggregation function>", // Optional, default is Sum.
    percentOfGrandTotal?: boolean
}

匯總階層層級

使用 IHierarchyLevelAggrTarget 在資料表中階層內的指定階層層級上指定匯總函數。 如果目標系結至視覺效果,且其值會顯示為總計的百分比,則 'percentOfGrandTotal' 的值會設定為 true。

target: {
    $schema: "http://powerbi.com/product/schema#hierarchyLevelAggr",
    table: "<table name>",
    hierarchy: "<hierarchy name>",
    hierarchyLevel: "<hierarchy level name>",
    aggregationFunction: "<aggregation function>", // Optional, default is Sum.
    percentOfGrandTotal?: boolean
}

範例

下列程式碼範例會 target 使用 物件來指定篩選準則應該處理的資料欄位。

資料行目標範例

下列程式碼範例會將具有資料行目標 的基本篩選 套用至報表。 此程式碼會作用於地理資料表的 [區域]資料行,如此一來,報表中只會顯示[西部] 區域中的資料。

let filter = {
    $schema: "http://powerbi.com/product/schema#basic",
    target: {
        $schema: "http://powerbi.com/product/schema#column",
        table: "Geo",
        column: "Region"
    },
    operator: "In",
    values: ["West"]
};

階層層級目標範例

下列程式碼範例會將具有階層層級目標的基本篩選套用至報表。 此程式碼會作用於Sales資料表之 OrderDate階層的Month層級,因此只有西部區域的資料會顯示在報表中的該階層層級之下。

let filter = {
    $schema: "http://powerbi.com/product/schema#basic",
    target: {
        $schema: "http://powerbi.com/product/schema#hierarchyLevel",
        table: "Sales",
        hierarchy: "OrderDate",
        hierarchyLevel: "Month"
    },
    operator: "In",
    values: ["West"]
};

量值目標範例

下列程式碼範例會將具有量值目標的 進階篩選 套用至視覺效果。 此程式碼會作用於SalesFact資料表中的[總類別量] 量值,因此只有在值不是空白時才會顯示。

let filter = {
    $schema: "http://powerbi.com/product/schema#advanced",
    target: {
        $schema: "http://powerbi.com/product/schema#measure",
        table: "SalesFact",
        measure: "Total Category Volume"
    },
    filterType: models.FilterType.AdvancedFilter,
    logicalOperator: "And",
    conditions: [{
        operator: "IsNotBlank"
    }]
}

匯總資料行目標範例

下列程式碼會定義資料行匯總目標,這是Sales資料表之Store資料行中值的總和:

let columnAggregation = {
    $schema: "http://powerbi.com/product/schema#columnAggr",
    table: "Store",
    column: "Sales",
    aggregationFunction: "Sum"
};

下一步