了解 Power BI 视觉对象中的数据视图映射Understand data view mapping in Power BI visuals

本文讨论了数据视图映射,描述了数据角色如何彼此相关,并允许你为它们指定条件要求。This article discusses data view mapping and describes how data roles relate to each other and allow you to specify conditional requirements for them. 本文还介绍了每种 dataMappings 类型。The article also describes each dataMappings type.

每个有效的映射都会生成一个数据视图,但目前仅支持对每个视觉对象执行一次查询。Each valid mapping produces a data view, but we currently support performing only one query per visual. 你通常会仅获得一个数据视图。You ordinarily get only one data view. 然而,在某些情况下可以提供多个数据映射,这允许:However, you can provide multiple data mappings in certain conditions, which allow:

"dataViewMappings": [
    {
        "conditions": [ ... ],
        "categorical": { ... },
        "single": { ... },
        "table": { ... },
        "matrix": { ... }
    }
]

当且仅当在 dataViewMappings 中填充了有效映射时,Power BI 才会创建到数据视图的映射。Power BI creates a mapping to a data view if and only if the valid mapping is filled in dataViewMappings.

换句话说,categorical 可在 dataViewMappings 中定义,但其他映射(例如 tablesingle )可能不行。In other words, categorical might be defined in dataViewMappings but other mappings, such as table or single, might not be. 例如:For example:

"dataViewMappings": [
    {
        "categorical": { ... }
    }
]

Power BI 生成具有单个 categorical 映射的数据视图,并且 table 和其他映射未定义:Power BI produces a data view with a single categorical mapping, and table and other mappings are undefined:

{
    "categorical": {
        "categories": [ ... ],
        "values": [ ... ]
    },
    "metadata": { ... }
}

条件Conditions

本部分介绍特定数据映射的条件。This section describes conditions for a particular data mapping. 可以提供多组条件,如果数据与所述的一组条件相匹配,则视觉对象会将数据作为有效数据来接受。You can provide multiple sets of conditions and, if the data matches one of the described sets of conditions, the visual accepts the data as valid.

目前,对于每个字段,可以指定最小值和最大值。Currently, for each field, you can specify a minimum and maximum value. 该值表示可以绑定到该数据角色的字段数。The value represents the number of fields that can be bound to that data role.

备注

如果条件中省略了数据角色,那么可以绑定任意数量的字段。If a data role is omitted in the condition, it can have any number of fields.

示例 1Example 1

可以将多个字段拖入每个数据角色。You can drag multiple fields into each data role. 在本示例中,类别限一个数据字段,度量值限两个数据字段。In this example, you limit the category to one data field and the measure to two data fields.

"conditions": [
    { "category": { "max": 1 }, "y": { "max": 2 } },
]

示例 2Example 2

在本示例中,需要满足以下两个条件之一:In this example, either of two conditions is required:

  • 一个类别数据字段和两个度量值Exactly one category data field and exactly two measures
  • 两个类别和一个度量值。Exactly two categories and exactly one measure.
"conditions": [
    { "category": { "min": 1, "max": 1 }, "measure": { "min": 2, "max": 2 } },
    { "category": { "min": 2, "max": 2 }, "measure": { "min": 1, "max": 1 } }
]

单个数据映射Single data mapping

单个数据映射是数据映射的最简单形式。Single data mapping is the simplest form of data mapping. 它接受单个度量值字段并提供总计。It accepts a single measure field and gives you the total. 如果该字段为数值字段,则它将提供总和。If the field is numeric, it gives you the sum. 否则,它将提供非重复值的计数。Otherwise, it gives you a count of unique values.

要使用单个数据映射,则需要定义要映射的数据角色的名称。To use single data mapping, you need to define the name of the data role that you want to map. 此映射仅适用于单个度量值字段。This mapping works only with a single measure field. 如果分配了第二个字段,则不会生成任何数据视图,因此,最好包含将数据限制为单个字段的条件。If a second field is assigned, no data view is generated, so it's also a good practice to include a condition that limits the data to a single field.

备注

此数据映射不能与任何其他数据映射结合使用。This data mapping can't be used in conjunction with any other data mapping. 它旨在将数据限制为单个数值。It's meant to reduce data into a single numeric value.

示例 3Example 3

"dataViewMappings": {
    "conditions": [
        { "Y": { "max": 1 } }
    ],
    "single": {
        "role": "Y"
    }
}  

生成的数据视图仍将包含其他类型(表、类别等),但每个映射只包含单个值。The resulting data view still contains the other types (table, categorical, and so on), but each mapping contains only the single value. 最佳做法是只访问单个值。The best practice is to access the value only in single.

{
    "dataView": [
        {
            "metadata": null,
            "categorical": null,
            "matrix": null,
            "table": null,
            "tree": null,
            "single": {
                "value": 94163140.3560001
            }
        }
    ]
}

类别数据映射Categorical data mapping

类别数据映射用于获取一个或两个独立的数据组别。Categorical data mapping is used to get one or two independent groupings of data.

示例 4Example 4

下面是之前数据角色示例中的定义:Here is the definition from the previous example for data roles:

"dataRole":[
    {
        "displayName": "Category",
        "name": "category",
        "kind": "Grouping"
    },
    {
        "displayName": "Y Axis",
        "name": "measure",
        "kind": "Measure"
    }
]

下面是映射:Here is the mapping:

"dataViewMappings": {
    "categorical": {
        "categories": {
            "for": { "in": "category" }
        },
        "values": {
            "select": [
                { "bind": { "to": "measure" } }
            ]
        }
    }
}

这是一个简单的示例。It's a simple example. 内容是“映射我的 category 数据角色,以便我拖入 category 的每个字段的数据都映射到 categorical.categoriesIt reads "Map my category data role so that for every field I drag into category, its data is mapped to categorical.categories. 同时将 measure 数据角色映射到 categorical.values”。Also map my measure data role to categorical.values."

  • for...in:对于此数据角色中的所有项,将它们包含在数据查询中。for...in: For all the items in this data role, include them in the data query.
  • bind...to:生成与 for...in 相同的结果,但预期数据角色具有将其限制为单个字段的条件。bind...to: Produces the same result as in for...in, but expects that the data role will have a condition restricting it to a single field.

示例 5Example 5

本示例将使用前面示例中的前两个数据角色,另外定义 groupingmeasure2This example uses the first two data roles from the previous example and additionally defines grouping and measure2.

"dataRole":[
    {
        "displayName": "Category",
        "name": "category",
        "kind": "Grouping"
    },
    {
        "displayName": "Y Axis",
        "name": "measure",
        "kind": "Measure"
    },
    {
        "displayName": "Grouping with",
        "name": "grouping",
        "kind": "Grouping"
    },
    {
        "displayName": "X Axis",
        "name": "measure2",
        "kind": "Grouping"
    }
]

下面是映射:Here is the mapping:

"dataViewMappings":{
    "categorical": {
        "categories": {
            "for": { "in": "category" }
        },
        "values": {
            "group": {
                "by": "grouping",
                "select":[
                    { "bind": { "to": "measure" } },
                    { "bind": { "to": "measure2" } }
                ]
            }
        }
    }
}

此处的差别在于映射类别值的方式。Here the difference is in how we are mapping categorical.values. 现在内容是“映射我的 measuremeasure2 数据角色,以按照数据角色 grouping 进行分组”。We are saying that "Map my measure and measure2 data roles to be grouped by the data role grouping."

示例 6Example 6

下面是数据角色:Here are the data roles:

"dataRoles": [
    {
        "displayName": "Categories",
        "name": "category",
        "kind": "Grouping"
    },
    {
        "displayName": "Measures",
        "name": "measure",
        "kind": "Measure"
    },
    {
        "displayName": "Series",
        "name": "series",
        "kind": "Measure"
    }
]

下面是数据视图映射:Here is the data view mapping:

"dataViewMappings": [
    {
        "categorical": {
            "categories": {
                "for": {
                    "in": "category"
                }
            },
            "values": {
                "group": {
                    "by": "series",
                    "select": [{
                            "for": {
                                "in": "measure"
                            }
                        }
                    ]
                }
            }
        }
    }
]

类别数据视图可进行可视化,如下所示:The categorical data view could be visualized like this:

分类Categorical
年份Year 20132013 20142014 20152015 20162016
国家/地区Country
美国USA xx xx 125125 100100
加拿大Canada xx 5050 200200 xx
墨西哥Mexico 300300 xx xx xx
英国UK xx xx 7575 xx

Power BI 将其作为分类数据视图生成。Power BI produces it as the categorical data view. 这是一组类别。It's the set of categories.

{
    "categorical": {
        "categories": [
            {
                "source": {...},
                "values": [
                    "Canada",
                    "Mexico",
                    "UK",
                    "USA"
                ],
                "identity": [...],
                "identityFields": [...],
            }
        ]
    }
}

每个类别也会映射到一组值。Each category maps to a set of values as well. 其中的每个值都是按序列分组的,序列以年份的形式表示。Each of these values is grouped by series, which is expressed as years.

例如,2013 年加拿大销售额为 null,2014 年加拿大销售额为 50。For example, Canada sales in 2013 is null, Canada sales in 2014 is 50.

{
    "values": [
        {
            "source": {...},
            "values": [
                null,
                300,
                null,
                null
            ],
            "identity": [...],
        },
        {
            "source": {...},
            "values": [
                50,
                null,
                150,
                null
            ],
            "identity": [...],
        },
        {
            "source": {...},
            "values": [
                200,
                null,
                null,
                125
            ],
            "identity": [...],
        },
        {
            "source": {...},
            "values": [
                null,
                null,
                null,
                100
            ],
            "identity": [...],
        }
    ]
}

表数据映射Table data mapping

表数据视图是一种简单的数据映射。The table data view is a simple data mapping. 实质上,它是数据点的列表,可在其中聚合数值数据点。Essentially, it's a list of data points, where numeric data points could be aggregated.

示例 7Example 7

如果给定功能:With the given capabilities:

"dataRoles": [
    {
        "displayName": "Values",
        "name": "values",
        "kind": "Measure"
    }
]
"dataViewMappings": [
    {
        "table": {
            "rows": {
                "for": {
                    "in": "values"
                }
            }
        }
    }
]

可以按下方所示的方式可视化表数据视图:You can visualize the table data view as the following:

国家/地区Country 年份Year 销售Sales
美国USA 20162016 100100
美国USA 20152015 5050
加拿大Canada 20152015 200200
加拿大Canada 20152015 5050
墨西哥Mexico 20132013 300300
英国UK 20142014 150150
美国USA 20152015 7575

Power BI 以表数据视图的形式显示数据。Power BI displays your data as the table data view. 不应假定数据已排序。You shouldn't assume that the data is ordered.

{
    "table" : {
        "columns": [...],
        "rows": [
            [
                "Canada",
                2014,
                50
            ],
            [
                "Canada",
                2015,
                200
            ],
            [
                "Mexico",
                2013,
                300
            ],
            [
                "UK",
                2014,
                150
            ],
            [
                "USA",
                2015,
                100
            ],
            [
                "USA",
                2015,
                75
            ],
            [
                "USA",
                2016,
                100
            ]
        ]
    }
}

可以通过选择所需的字段,然后选择“求和”来聚合数据。You can aggregate the data by selecting the desired field and then selecting sum.

数据聚合

矩阵数据映射Matrix data mapping

矩阵数据映射与表数据映射类似,但前者按层次结构显示行。Matrix data mapping is similar to table data mapping, but the rows are presented hierarchically. 任意一个数据角色值均可用作列标题值。Any of the data role values can be used as a column header value.

{
    "dataRoles": [
        {
            "name": "Category",
            "displayName": "Category",
            "displayNameKey": "Visual_Category",
            "kind": "Grouping"
        },
        {
            "name": "Column",
            "displayName": "Column",
            "displayNameKey": "Visual_Column",
            "kind": "Grouping"
        },
        {
            "name": "Measure",
            "displayName": "Measure",
            "displayNameKey": "Visual_Values",
            "kind": "Measure"
        }
    ],
    "dataViewMappings": [
        {
            "matrix": {
                "rows": {
                    "for": {
                        "in": "Category"
                    }
                },
                "columns": {
                    "for": {
                        "in": "Column"
                    }
                },
                "values": {
                    "select": [
                        {
                            "for": {
                                "in": "Measure"
                            }
                        }
                    ]
                }
            }
        }
    ]
}

Power BI 创建分层数据结构。Power BI creates a hierarchical data structure. 树层次结构的根包括来自 Category 数据角色的“父级”列数据,以及来自数据角色表的“子级”列的子项 。The root of the tree hierarchy includes the data from the Parents column of the Category data role, with children from the Children column of the data role table.

数据集:Dataset:

父级Parents 子级Children 孙级Grandchildren ColumnsColumns Values
Parent1Parent1 Child1Child1 Grand child1Grand child1 Col1Col1 55
Parent1Parent1 Child1Child1 Grand child1Grand child1 Col2Col2 66
Parent1Parent1 Child1Child1 Grand child2Grand child2 Col1Col1 77
Parent1Parent1 Child1Child1 Grand child2Grand child2 Col2Col2 88
Parent1Parent1 Child2Child2 Grand child3Grand child3 Col1Col1 55
Parent1Parent1 Child2Child2 Grand child3Grand child3 Col2Col2 33
Parent1Parent1 Child2Child2 Grand child4Grand child4 Col1Col1 44
Parent1Parent1 Child2Child2 Grand child4Grand child4 Col2Col2 99
Parent1Parent1 Child2Child2 Grand child5Grand child5 Col1Col1 33
Parent1Parent1 Child2Child2 Grand child5Grand child5 Col2Col2 55
Parent2Parent2 Child3Child3 Grand child6Grand child6 Col1Col1 第 1 个1
Parent2Parent2 Child3Child3 Grand child6Grand child6 Col2Col2 22
Parent2Parent2 Child3Child3 Grand child7Grand child7 Col1Col1 77
Parent2Parent2 Child3Child3 Grand child7Grand child7 Col2Col2 第 1 个1
Parent2Parent2 Child3Child3 Grand child8Grand child8 Col1Col1 1010
Parent2Parent2 Child3Child3 Grand child8Grand child8 Col2Col2 1313

Power BI 的核心矩阵视觉对象将数据呈现为表。The core matrix visual of Power BI renders the data as a table.

矩阵视觉对象

视觉对象按以下代码所述获取其数据结构(此处仅显示前两个表行):The visual gets its data structure as described in the following code (only the first two table rows are shown here):

{
    "metadata": {...},
    "matrix": {
        "rows": {
            "levels": [...],
            "root": {
                "childIdentityFields": [...],
                "children": [
                    {
                        "level": 0,
                        "levelValues": [...],
                        "value": "Parent1",
                        "identity": {...},
                        "childIdentityFields": [...],
                        "children": [
                            {
                                "level": 1,
                                "levelValues": [...],
                                "value": "Child1",
                                "identity": {...},
                                "childIdentityFields": [...],
                                "children": [
                                    {
                                        "level": 2,
                                        "levelValues": [...],
                                        "value": "Grand child1",
                                        "identity": {...},
                                        "values": {
                                            "0": {
                                                "value": 5 // value for Col1
                                            },
                                            "1": {
                                                "value": 6 // value for Col2
                                            }
                                        }
                                    },
                                    ...
                                ]
                            },
                            ...
                        ]
                    },
                    ...
                ]
            }
        },
        "columns": {
            "levels": [...],
            "root": {
                "childIdentityFields": [...],
                "children": [
                    {
                        "level": 0,
                        "levelValues": [...],
                        "value": "Col1",
                        "identity": {...}
                    },
                    {
                        "level": 0,
                        "levelValues": [...],
                        "value": "Col2",
                        "identity": {...}
                    },
                    ...
                ]
            }
        },
        "valueSources": [...]
    }
}

数据缩减算法Data reduction algorithm

若要控制在数据视图中接收的数据量,可以应用数据缩减算法。To control the amount of data to receive in the data view, you can apply a data reduction algorithm.

默认情况下,所有 Power BI 视觉对象都应用了顶级数据缩减算法,并将“count”设置为 1000 个数据点 。By default, all Power BI visuals have the top data reduction algorithm applied with the count set to 1000 data points. 这相当于在 capabilities.json 中设置了以下属性 :It's the same as setting the following properties in the capabilities.json file:

"dataReductionAlgorithm": {
    "top": {
        "count": 1000
    }
}

可以将“count”值修改为不超过 30000 的任何整数值 。You can modify the count value to any integer value up to 30000. 基于 R 的 Power BI 视觉对象最多可支持 150000 行。R-based Power BI visuals can support up to 150000 rows.

数据缩减算法类型Data reduction algorithm types

有四种类型的数据缩减算法设置:There are four types of data reduction algorithm settings:

  • top:如果要将数据限制为从数据集顶部获取的值。top: If you want to limit the data to values taken from the top of the dataset. 将从数据集中获取顶部第一个“count”值 。The top first count values will be taken from the dataset.
  • bottom:如果要将数据限制为从数据集底部获取的值。bottom: If you want to limit the data to values taken from the bottom of the dataset. 将从数据集中获取最后一个“count”值。The last "count" values will be taken from the dataset.
  • sample:通过一个简单采样算法来缩减数据集,该方法将项数限制为“count”的数量 。sample: Reduce the dataset by a simple sampling algorithm, limited to a count number of items. 它意味着包含第一项和最后一项,以及一组“count”数量的项具有相等的间隔 。It means that the first and last items are included, and a count number of items have equal intervals between them. 例如,如果你有一个数据集 [0, 1, 2, ...100] 且“count” 值为 9,你将收到值 [0, 10, 20 ...100]。For example, if you have a dataset [0, 1, 2, ... 100] and a count of 9, you'll receive the values [0, 10, 20 ... 100].
  • window:一次加载一个“窗口”的数据点,其中包含“count”元素 。window: Loads one window of data points at a time containing count elements. 当前 topwindow 等效。Currently, top and window are equivalent. 我们正在努力做到完全支持窗口设置。We are working toward fully supporting a windowing setting.

数据缩减算法使用情况Data reduction algorithm usage

数据缩减算法可用于分类、表或矩阵数据视图映射。The data reduction algorithm can be used in categorical, table, or matrix data view mapping.

可以将算法设置为 categories 和/或 values 的组部分,用于类别数据映射。You can set the algorithm into categories and/or group section of values for categorical data mapping.

示例 8Example 8

"dataViewMappings": {
    "categorical": {
        "categories": {
            "for": { "in": "category" },
            "dataReductionAlgorithm": {
                "window": {
                    "count": 300
                }
            }  
        },
        "values": {
            "group": {
                "by": "series",
                "select": [{
                        "for": {
                            "in": "measure"
                        }
                    }
                ],
                "dataReductionAlgorithm": {
                    "top": {
                        "count": 100
                    }
                }  
            }
        }
    }
}

可以将数据缩减算法应用于数据视图映射表的 rows 部分。You can apply the data reduction algorithm to the rows section of the Data View mapping table.

示例 9Example 9

"dataViewMappings": [
    {
        "table": {
            "rows": {
                "for": {
                    "in": "values"
                },
                "dataReductionAlgorithm": {
                    "top": {
                        "count": 2000
                    }
                } 
            }
        }
    }
]

可以将数据缩减算法应用于数据视图映射矩阵的 rowscolumns 部分。You can apply the data reduction algorithm to the rows and columns sections of the Data View mapping matrix.