power bi函数
Aggregate functions are one of the main building blocks in Power BI. Being used explicitly in measures, or implicitly defined by Power BI, there is no single Power BI report which doesn’t use some sort of aggregate functions.
聚合功能是Power BI中的主要构建块之一。 由于在度量中显式使用或由Power BI隐式定义,因此没有单个Power BI报告不使用某种聚合函数。
聚合函数是什么? (What are the aggregate functions?)
Aggregating means combining values in your data performing some mathematical operation. That can be SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT, COUNT DISTINCT, MEAN, STANDARD DEVIATION, etc.
汇总是指将数据中的值组合起来以执行一些数学运算。 可以是SUM,AVERAGE,MAXIMUM,MINIMUM,COUNT,COUNT DISTINCT,MEAN,标准偏差等。
However, in-depth observation of aggregate functions is not in the scope of this article. Here, I wanted to demonstrate how you can use aggregate functions in an unconventional way, since I believe it can be useful in some specific scenarios.
但是,对聚合函数的深入观察不在本文的范围之内。 在这里,我想演示如何以一种非常规的方式使用聚合函数,因为我认为它在某些特定情况下很有用。
聚合函数的默认行为 (Default behavior of aggregate functions)
By default, aggregations are being calculated on columns. Let’s take a look at following basic example:
默认情况下,聚合是在列上计算的。 让我们看下面的基本示例:
This is a typical example of SUM aggregate function. Numbers are being aggregated on Year and Month level, and finally, in the end, we can see the total of individual values in the table.
这是SUM聚合函数的典型示例。 数字在“年”和“月”级别上进行汇总,最后,最后,我们可以在表中看到各个值的总数。
We could also perform AVERAGE to find average values, MIN or MAX to find the minimum and maximum values, etc. Pretty straightforward and probably already known for most of the people who ever worked with Power BI or Excel.
我们还可以执行AVERAGE来查找平均值,使用MIN或MAX来查找最小值和最大值,等等。这非常简单,对于使用Power BI或Excel的大多数人来说可能已经众所周知。
行上的聚合-毫不动摇! (Aggregation on rows — without unpivoting!)
But, what if we wanted to perform aggregations on rows instead of columns? Is it possible to do that? And if yes, how?
但是,如果我们想对行而不是对列执行聚合呢? 有可能这样做吗? 如果是的话,怎么办?
Let’s head over to a Power BI and check immediately. I have an Excel file as a data source and a dummy table which contains data about the customer and first date within a single year when he made a purchase:
让我们转到Power BI并立即检查。 我有一个Excel文件作为数据源,还有一个虚拟表,其中包含有关客户和他在购买后一年内的首次约会的数据:
As you can see, some customers made a purchase in every single year, some have gaps, some came in later years, etc.
如您所见,有些客户每年都进行购买,有些客户有缺口,有些则是在以后几年等等。
Now, I want to retrieve the earliest date when a customer made a purchase, so I can later perform analysis based on that date (for example, to analyze how many customers made first purchase in February 2017).
现在,我想检索客户进行购买的最早日期,以便稍后可以基于该日期进行分析(例如,分析在2017年2月进行首次购买的客户数量)。
I know, most of you would probably go with Power Query transformation and Unpivoting years’ columns, something like this:
我知道,你们中的大多数人可能会使用Power Query转换和Unpivoting年的列,如下所示:
And you get a nice new look of the table, with all dates grouped by customer:
您会看到表格的新外观,所有日期均按客户分组:
However, an additional workload is necessary to build a separate column which will hold data about the earliest date (or MIN date) for every single customer, so we can later use this column for filtering purposes, or even for building a relationship to a date dimension.
但是,需要额外的工作量来构建一个单独的列,该列将保存有关每个客户的最早日期(或MIN日期)的数据,因此我们以后可以将该列用于过滤目的,甚至建立与日期的关系。 尺寸 。
What if I tell you that you can do this with a single line of code and without any additional transformations?
如果我告诉您可以用一行代码执行而无需任何其他转换,该怎么办?
First, I will close the Power Query editor and go straight to the Power BI Data view:
首先,我将关闭Power Query编辑器并直接进入Power BI Data视图:
You see that this table looks exactly the same as in Excel. Now, I choose to create a new column and, when prompted, enter following DAX code:
您会看到此表看起来与Excel中的表完全相同。 现在,我选择创建一个新列,并在出现提示时输入以下DAX代码:
First Purchase Date = MINX({Sheet2[2016],Sheet2[2017],Sheet2[2018],Sheet2[2019]},[Value])
Let’s stop here for the moment and explain what we are doing. So, we want to extract the minimum date from every single row. We could do that by using multiple nested IF statements and using MIN aggregate function. Since MIN function accepts only two arguments, we would have multiple levels of nested IF statements, which is quite ugly and pretty much hardly readable.
让我们暂时在这里停止并解释我们在做什么。 因此,我们要从每一行中提取最小日期。 我们可以通过使用多个嵌套的IF语句并使用MIN聚合函数来做到这一点。 由于MIN函数仅接受两个参数,因此我们将具有多层嵌套的IF语句,这很难看而且很难读。
The magic here is in the curly brackets! By using them, we are telling DAX that we want it to create a table from the list within the curly brackets, and using MINX iterator aggregate function, we are simply iterating through this table and pulling minimum value from it.
魔术在花括号中! 通过使用它们,我们告诉DAX我们希望它在大括号内的列表中创建一个表,并使用MINX迭代器聚合函数,我们仅在该表中进行迭代并从中获取最小值。
How cool and elegant is that! It worked like a charm and here is the resulting column:
那是多么的酷和优雅! 它像一种魅力一样工作,这是结果列:
You can easily spot that DAX returned expected values, so now we can use this column as an axis in our charts, create regular date hierarchies on it, or we can even create a relationship between First Purchase Date and date dimension in our data model if we like to.
您可以轻松地发现DAX返回了期望值,因此现在我们可以将该列用作图表中的轴,在其上创建常规的日期层次结构,或者在以下情况下甚至可以在数据模型中的“首次购买日期”和日期维度之间建立关系:我们喜欢。
结论 (Conclusion)
Power BI and DAX are full of hidden gems. In complete honesty, I have to admit that you might not face a scenario like this every single day, but in some specific situations, it’s good to know that you can perform aggregate functions on row level in a very simple, yet powerful manner — using a literally single line of code!
Power BI和DAX充满了隐藏的宝石。 老实说,我必须承认您可能不会每天都遇到这样的情况,但是在某些特定情况下,很高兴知道您可以以非常简单但功能强大的方式在行级别执行聚合函数-使用真正的单行代码!
翻译自: https://towardsdatascience.com/use-aggregate-functions-on-rows-in-power-bi-904724a54374
power bi函数
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388200.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!