power bi中的切片器
Just recently, while presenting my session: “Magnificent 7 — Simple tricks to boost your Power BI Development” at the New Stars of Data conference, one of the questions I’ve received was:
就在最近,在“新数据之星”会议上介绍我的会议: “壮丽的7 —促进Power BI开发的简单技巧”时,我收到的问题之一是:
根据页面上的切片器选择,是否可以显示实际的活动过滤器? (Is there a way to show the actual active filters as a result of the slicer choices on the page?)
I’ve already answered the question in this article, but then I thought: maybe more people search for the solution to this problem, so I decided to write a short post to explain in detail how you can achieve this.
我已经在本文中回答了这个问题,但是后来我想:也许更多的人正在寻找解决这个问题的方法,所以我决定写一篇简短的文章来详细说明如何实现此目的。
As usual, I will use a sample Contoso database for demo purposes:
和往常一样,我将使用示例Contoso数据库进行演示:
DAX是您的朋友! (DAX is your friend!)
This is the starting point. You can notice three slicers on the report canvas, and let’s say that I want to show my users which brands are selected within Brands slicer.
这是起点。 您可以在报表画布上注意到三个切片器,并且假设我要向用户显示在“品牌”切片器中选择了哪些品牌。
The first step is to create a DAX measure that will capture all selected values in the slicer. You can use two different DAX functions to obtain the values: VALUES() or DISTINCT(). VALUES() function is more complex since you can use both table name and column name as an argument, but let’s focus here on the column name as an argument.
第一步是创建一个DAX度量,它将捕获切片器中的所有选定值。 您可以使用两个不同的DAX函数来获取值: VALUES()或DISTINCT() 。 VALUES()函数更加复杂,因为您可以将表名和列名都用作参数,但是在这里,我们将重点放在列名作为参数上。
Basically, VALUES() will return all distinct values from the column we passed, including blanks (if exist)! On the other hand, DISTINCT() will return all distinct values but ignoring blank values. Which one you want to use, depends mostly on the business request (if your users want to see numbers for blanks or not). Personally, I prefer to use VALUES(), because it gives me the full picture.
基本上, VALUES()将返回我们传递的列中的所有不同值,包括空格(如果存在)! 另一方面, DISTINCT()将返回所有不同的值,但忽略空白值。 您要使用哪一个,主要取决于业务请求(如果您的用户希望查看是否为空白的数字)。 就个人而言,我更喜欢使用VALUES() ,因为它可以给我完整的画面。
So, I will create the following measure:
因此,我将创建以下措施:
Selected Brands = VALUES('Product'[BrandName])
Now, when I put this measure in the Card visual, let’s see what happens:
现在,当我将此度量放入Card视觉中时,让我们看看会发生什么:
Oops, that gives me an error! Error message explains that the calculation expects a single value. The problem is that VALUES() and DISTINCT() return TABLE! However, it’s not a “normal” table, it’s virtual table created on the fly by DAX engine, so we should apply some additional calculations in order to extract single values from it.
糟糕,这给了我一个错误! 错误消息说明计算需要单个值。 问题是VALUES()和DISTINCT()返回TABLE! 但是,它不是“普通”表,而是由DAX引擎动态创建的虚拟表,因此我们应该应用一些其他计算,以便从中提取单个值。
迭代器功能可以解救! (Iterator functions to the rescue!)
Iterator functions do what their name says — they iterate over the table and apply the calculation row by row! Iterator functions have X in the end: SUMX, AVERAGEX, COUNTX…In our scenario, we need to iterate over our distinct values and concatenate them into our string, which will be later displayed in the report. To achieve that, we will use CONCATENATEX() function.
迭代器函数按其名称所述执行操作-遍历表并逐行应用计算! 迭代器函数的末尾有X:SUMX,AVERAGEX,COUNTX…在我们的方案中,我们需要遍历不同的值并将它们连接到我们的字符串中,然后将其显示在报告中。 为此,我们将使用CONCATENATEX()函数。
This function accepts three arguments: the first is a table that we want to iterate on (in our case, a virtual table created using VALUES() function), then expression we are applying row by row on this table, and finally delimiter we want to use for separating extracted values.
该函数接受三个参数:第一个是要迭代的表(在本例中,是使用VALUES()函数创建的虚拟表),然后在表中逐行应用表达式,最后使用所需的定界符用于分离提取的值。
Selected Brands = CONCATENATEX(
VALUES('Product'[BrandName]),
'Product'[BrandName],
",")
In my example, I’m using comma as a delimiter, but you can also use others, such as a semicolon, etc. Now, when I look again in my report, I can see that I got desired results:
在我的示例中,我使用逗号作为定界符,但是您也可以使用其他字符,例如分号等。现在,当我再次查看报表时,可以看到获得了预期的结果:
And, if I choose only few brands within the slicer, my card will adjust to reflect the changes:
而且,如果我在切片器中只选择了少数几个品牌,我的卡将进行调整以反映更改:
结合多种措施 (Combining multiple measures)
You can create the same measure for other slicers also, and then put all the results in the table visual. So, I will create the measure for Year slicer:
您也可以为其他切片器创建相同的度量,然后将所有结果显示在表格中。 因此,我将为Year slicer创建度量:
Selected Year = CONCATENATEX(
VALUES(Dates[Year])
,Dates[Year]
,",")
Now, when I drag bit my measures into the table fields, I see both of my selected slicers’ values at one place:
现在,当我将度量值拖到表字段中时,我会在一个位置看到两个选择的切片器的值:
奖励想法! (Bonus idea!)
If you have many slicers in your report, and you want to display all of the selections, but you don’t want to waste space on your report canvas, you can create a bookmark containing the table with all selected values, and then just display the bookmark on user’s request.
如果报表中有许多切片器,并且要显示所有选择,但又不想浪费报表画布上的空间,则可以创建一个包含表的书签,其中包含所有选定值,然后仅显示根据用户要求添加书签。
Something like this:
像这样:
How cool is that! So, you don’t need to worry about bloating your report space with this table. You can show/hide it using bookmarks and actions.
多么酷啊! 因此,您不必担心使用此表会增加报告空间。 您可以使用书签和操作显示/隐藏它。
翻译自: https://towardsdatascience.com/display-selected-slicers-in-power-bi-a99d81500e76
power bi中的切片器
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389621.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!