简体   繁体   English

使用切片器 Power BI 突出显示

[英]Highlighting with slicer Power BI

I have a Dashboard with different visuals.我有一个具有不同视觉效果的仪表板。 Data is made up of different values for insurance companies.数据由保险公司的不同值组成。 I want my slicers/filters to not filter all data, but to only highlight the chosen company.我希望我的切片器/过滤器不过滤所有数据,而只突出显示所选公司。

For example, in my slicer I choose the insurance ABN.例如,在我的切片器中,我选择了保险 ABN。 Instead of showing me the value for ABN only in my visuals, I want all other values to still be visible and ABN's value to be highlighted in the visuals.我希望所有其他值仍然可见,并在视觉效果中突出显示 ABN 的值,而不是仅在我的视觉效果中向我显示 ABN 的值。

Does anyone know how to do this?有谁知道如何做到这一点?

You can use conditional formatting to achieve this.您可以使用条件格式来实现这一点。 Lets say that we will change the background color to "highlight" a row (or cells, to be precise).假设我们将更改背景颜色以“突出显示”一行(或准确地说是单元格)。

First, we need a slicer, which will not filter our data.首先,我们需要一个切片器,它不会过滤我们的数据。 We can do this by duplicating our source table, removing the unnecessary columns and making sure there is no relationship between the source and the duplicate.我们可以通过复制源表、删除不必要的列并确保源表和副本之间没有关系来做到这一点。 So if we have a source table, named Table , like this:因此,如果我们有一个名为Table的源Table ,如下所示:

在此处输入图片说明

Right click on it and select Duplicate :右键单击它并选择Duplicate

在此处输入图片说明

Then right click the title of the column you want to keep and select Remove Other Columns to get a list of company names only (you may also remove the duplicates, but it's not required).然后右键单击要保留的列的标题并选择“ Remove Other Columns以仅获取公司名称列表(您也可以删除重复项,但这不是必需的)。 Then in the model delete the relation between both tables:然后在模型中删除两个表之间的关系:

在此处输入图片说明

Now you can place a table showing company name and sales from your data source, and a slicer for company name from the duplicate table.现在,您可以从数据源放置一个显示公司名称和销售额的表,并从重复表中放置一个公司名称切片器。 At this point selecting values in the slicer should not affect the table.此时在切片器中选择值不应影响表格。

Now you need to capture the value of the slicer and use it in a measure, which will determine should current row be highlighted or not.现在您需要捕获切片器的值并在度量中使用它,这将确定是否应突出显示当前行。 You can use SELECTEDVALUE for that, but note that it will give you a value only if there is a one selected in the slicer.您可以为此使用SELECTEDVALUE ,但请注意,只有在切片器中选择了一个值时,它才会为您提供一个值。 If you want to support highlighting of more than one company, it gets a bit more complicated.如果你想支持突出显示不止一家公司,它会变得有点复杂。

Make a new measure in your source table, like this:在源表中创建一个新度量,如下所示:

Measure = IF(HASONEVALUE('Table (2)'[Company name]);
    IF(SELECTEDVALUE('Table (2)'[Company name]) = MAX('Table'[Company name]); 1; 0);
    IF(ISFILTERED('Table (2)'[Company name]) && COUNTROWS(FILTER('Table (2)'; 'Table (2)'[Company name] = MAX('Table'[Company name]))); 1; 0))

In case there is only one value selected in the slicer (see HASONEVALUE ), then our measure will return 1 (highlight) or 0 (don't), comparing it with the current row.如果切片器中只选择了一个值(请参阅HASONEVALUE ),那么我们的度量将返回 1(突出显示)或 0(不),并将其与当前行进行比较。

Otherwise (ie there is no selection in the slicer, or there are 2 or more companies selected), then we will look at the filtered list of companies ( Table (2) ) - if it contains current row, then 1 (highlight), otherwise 0 (don't).否则(即切片器中没有选择,或者选择了 2 个或更多公司),那么我们将查看过滤后的公司列表( Table (2) )-如果它包含当前行,则为 1(突出显示),否则为 0(不要)。 But we will also handle the case, where there is no value selected in the slicer.但是我们也会处理切片器中没有选择值的情况。 In this case the list will contain all the companies, ie all rows will be highlighted.在这种情况下,列表将包含所有公司,即所有行都将突出显示。 Here comes ISFILTERED .来了ISFILTERED And at the end, if the list is filtered and current row exists in the filtered list, then 1 (highlight), otherwise 0 (don't).最后,如果列表被过滤并且当前行存在于过滤列表中,则为 1(突出显示),否则为 0(不)。

Now, you need to use this measure to change the background of the column - right click each column in your table and select Conditional formatting -> Background color :现在,您需要使用此度量来更改列的背景 - 右键单击​​表中的每一列并选择Conditional formatting -> Background color

在此处输入图片说明

Then format by rules, where Measure >= 1 like this:然后按规则格式化,其中 Measure >= 1 像这样:

在此处输入图片说明

Now, when there is no selection in the slicer, there are no rows highlighted in the table:现在,当切片器中没有选择时,表中没有突出显示的行:

在此处输入图片说明

If you select one company, it is highlighted:如果您选择一家公司,它会突出显示:

在此处输入图片说明

It also work if there are multiple companies selected:如果选择了多个公司,它也适用:

在此处输入图片说明

Thank you Andrey for your step-by-step explanation which as been incredible helpful.感谢安德烈 (Andrey) 的分步解释,这非常有帮助。 I'd like to follow up with a further question, particularly regarding the comment below.我想跟进一个进一步的问题,特别是关于下面的评论。

"You can use SELECTEDVALUE for that, but note that it will give you a value only if there is a one selected in the slicer. If you want to support highlighting of more than one company, it gets a bit more complicated." “您可以为此使用 SELECTEDVALUE,但请注意,只有在切片器中选择了一个时,它才会为您提供一个值。如果您想支持突出显示多个公司,它会变得更加复杂。”

In my model, I've linked a third table (Table (3)) to Table (2) with a many to one relationship with Table (2).在我的模型中,我将第三个表(表 (3))链接到表 (2),并与表 (2) 建立了多对一的关系。 Therefore when I click on Table (3), it will filter Table (2), which acts as a slicer for Table (1).因此,当我单击表 (3) 时,它将过滤表 (2),它充当表 (1) 的切片器。

When only 1 value is filtered in Table (2), it conditionally formats the cells in Table (1).当表 (2) 中仅过滤 1 个值时,它有条件地格式化表 (1) 中的单元格。 However, when more than 1 value is filtered in Table (2), conditional formatting fails.但是,当表 (2) 中过滤了 1 个以上的值时,条件格式将失败。

As I'm looking to avoid manually selecting multiple values in the slicer (Table (2)), I was wondering if there's a workaround for SELECTEDVALUE such that it is able to conditionally format when I filter more than 1 value in Table (2).由于我希望避免在切片器中手动选择多个值(表 (2)),我想知道是否有 SELECTEDVALUE 的解决方法,以便当我在表 (2) 中过滤超过 1 个值时,它能够有条件地格式化.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM