Delen via


Use targets to select which data field to act on

In Power BI edit mode, the Fields pane shows all the tables on the model with all their data fields. You can sort, filter, or slice data in the data fields to affect how the data shows in reports, pages, or visuals.

A Power BI embedded analytics filter, slicer, or sort requires a target object that specifies which data field to act on. Use ITarget to provide the target data field when you:

Target types

A data field can be a column, a hierarchy level, an aggregated column or hierarchy level, or a calculated measure. Target types for the ITarget interface include:

  • IColumnTarget
  • IHierarchyLevelTarget
  • IMeasureTarget
  • IColumnAggrTarget
  • IHierarchyLevelAggrTarget

Column

Use IColumnTarget to target the specified column in a table:

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

Hierarchy level

Use IHierarchyLevelTarget to target the specified hierarchy level within a hierarchy in a table:

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

Measure

Use IMeasureTarget to target the specified measure in a table. The value of percentOfGrandTotal is set to true if the target is bound to a visual and its values are shown as a percent of the grand total.

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

Aggregation functions

You can specify aggregation functions for column and hierarchy level targets. The aggregationFunction variable is optional, and if you don't supply it, data fields use the default Sum aggregation function. Other supported values for aggregationFunction are:

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

Aggregated column

Use IColumnAggrTarget to specify an aggregation function on the specified column in a table. The value of 'percentOfGrandTotal' is set to true if the target is bound to a visual and its values are shown as a percent of the grand total.

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

Aggregated hierarchy level

Use IHierarchyLevelAggrTarget to specify an aggregation function on the specified hierarchy level within a hierarchy in a table. The value of 'percentOfGrandTotal' is set to true if the target is bound to a visual and its values are shown as a percent of the grand total.

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
}

Examples

The following code examples use the target object to specify the data field that a filter should act on.

Column target example

The following code example applies a basic filter with a column target to a report. The code acts on the Region column of the Geo table so that only data in the West region shows in the report.

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"]
};

Hierarchy level target example

The following code example applies a basic filter with a hierarchy level target to a report. The code acts on the Month level of the OrderDate hierarchy of the Sales table so that only data in the West region shows under that hierarchy level in the report.

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"]
};

Measure target example

The following code example applies an advanced filter with a measure target to a visual. The code acts on the Total Category Volume measure in the SalesFact table so it only appears when the value is not blank.

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"
    }]
}

Aggregated column target example

The following code defines a column aggregation target that is the sum of the values in the Store column of the Sales table:

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