power bi函数_在Power BI中的行上使用聚合函数

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:

默认情况下,聚合是在列上计算的。 让我们看下面的基本示例:

Image for post

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文件作为数据源,还有一个虚拟表,其中包含有关客户和他在购买后一年内的首次约会的数据:

Image for post

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年的列,如下所示:

Image for post

And you get a nice new look of the table, with all dates grouped by customer:

您会看到表格的新外观,所有日期均按客户分组:

Image for post

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视图:

Image for post

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])
Image for post

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:

那是多么的酷和优雅! 它像一种魅力一样工作,这是结果列:

Image for post

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,一经查实,立即删除!

相关文章

广义估计方程估计方法_广义估计方程简介

广义估计方程估计方法A key assumption underpinning generalized linear models (which linear regression is a type of) is the independence of observations. In longitudinal data this will simply not hold. Observations within an individual (between time points) …

Unity3d鼠标点击屏幕来控制人物的走动

今天呢,我们来一起实现一个在RPG中游戏中十分常见的功能,通过鼠标点击屏幕来控制人物的走动。首先来说一下原理,当我们点击屏幕时,我们按照一定的方法,将屏幕上的二维坐标转化为三维坐标,然后我们从摄像机位…

Java中的ReentrantLock和synchronized两种锁定机制的对比

2019独角兽企业重金招聘Python工程师标准>>> 多线程和并发性并不是什么新内容,但是 Java 语言设计中的创新之一就是,它是第一个直接把跨平台线程模型和正规的内存模型集成到语言中的主流语言。核心类库包含一个 Thread 类,可以用它…

大数定理 中心极限定理_中心极限定理:直观的遍历

大数定理 中心极限定理One of the most beautiful concepts in statistics and probability is Central Limit Theorem,people often face difficulties in getting a clear understanding of this and the related concepts, I myself struggled understanding this during my…

探索性数据分析(EDA)-不要问如何,不要问什么

数据科学 , 机器学习 (Data Science, Machine Learning) This is part 1 in a series of articles guiding the reader through an entire data science project.这是一系列文章的第1部分 ,指导读者完成整个数据科学项目。 I am a new writer on Medium…

IDEA 插件开发入门教程

2019独角兽企业重金招聘Python工程师标准>>> IntelliJ IDEA 是目前最好用的 JAVA 开发 IDE,它本身的功能已经非常强大了,但是每个人的需求不一样,有些需求 IDEA 本身无法满足,于是我们就需要自己开发插件来解决。工欲善…

安卓代码还是xml绘制页面_我们应该绘制实际还是预测,预测还是实际还是无关紧要?

安卓代码还是xml绘制页面Plotting the actual and predicted data is frequently used for visualizing and analyzing how the actual data correlate with those predicted by the model. Ideally, this should correspond to a slope of 1 and an intercept of 0. However, …

Mecanim动画系统

本期教程和大家分享Mecanim动画系统的重定向特性,Mecanim动画系统是Unity3D推出的全新的动画系统,具有重定向、可融合等诸多新特性,通过和美工人员的紧密合作,可以帮助程序设计人员快速地设计出角色动画。一起跟着人气博主秦元培学…

【嵌入式硬件Esp32】Ubuntu 1804下ESP32交叉编译环境搭建

一、ESP32概述EPS32是乐鑫最新推出的集成2.4GWi-Fi和蓝牙双模的单芯片方案,采用台积电(TSMC)超低功耗的40nm工艺,拥有最佳的功耗性能、射频性能、稳定性、通用性和可靠性,适用于多种应用和不同的功耗要求。 ESP32搭载低功耗的Xtensa LX6 32bi…

你认为已经过时的C语言,是如何影响500万程序员的?...

看招聘职位要c语言的占比真不多了,是否c语言真得落伍了? 看一下许多招聘平台有关于找纯粹的c语言开发的占比确实没有很多,都被Java,php,python等等語言刷屏。这对于入门正在学习c语言的小白真他妈就是惊天霹雳&#xf…

云尚制片管理系统_电影制片厂的未来

云尚制片管理系统Data visualization is a key step of any data science project. During the process of exploratory data analysis, visualizing data allows us to locate outliers and identify distribution, helping us to control for possible biases in our data ea…

JAVA单向链表实现

JAVA单向链表实现 单向链表 链表和数组一样是一种最常用的线性数据结构,两者各有优缺点。数组我们知道是在内存上的一块连续的空间构成,所以其元素访问可以通过下标进行,随机访问速度很快,但数组也有其缺点,由于数组的…

201771010102 常惠琢《面向对象程序设计(java)》第八周学习总结

1、实验目的与要求 (1) 掌握接口定义方法; (2) 掌握实现接口类的定义要求; (3) 掌握实现了接口类的使用要求; (4) 掌握程序回调设计模式; (5) 掌握Comparator接口用法; (6) 掌握对象浅层拷贝与深层拷贝方法&#xff1b…

新版 Android 已支持 FIDO2 标准,免密登录应用或网站

谷歌刚刚宣布了与 FIDO 联盟达成的最新合作,为 Android 用户带来了无需密码、即可登录网站或应用的便捷选项。 这项服务基于 FIDO2 标准实现,任何运行 Android 7.0 及后续版本的设备,都可以在升级最新版 Google Play 服务后,通过指…

t-sne原理解释_T-SNE解释-数学与直觉

t-sne原理解释The method of t-distributed Stochastic Neighbor Embedding (t-SNE) is a method for dimensionality reduction, used mainly for visualization of data in 2D and 3D maps. This method can find non-linear connections in the data and therefore it is hi…

Android Studio如何减小APK体积

最近在用AndroidStudio开发一个小计算器,代码加起来还不到200行。但是遇到一个问题,导出的APK文件大小竟然达到了1034K。这不科学,于是就自己动手精简APK。下面我们大家一起学习怎么缩小一个APK的大小,以hello world为例。 新建工…

js合并同类数组里面的对象_通过同类群组保留估算客户生命周期价值

js合并同类数组里面的对象This is Part I of the two-part series dedicated to estimating customer lifetime value. In this post, I will describe how to estimate LTV, on a conceptual level, in order to explain what we’re going to be doing in Part II with the P…

C#解析HTML

第一种方法:用正则表达式来分析 [csharp] view plaincopy 转自网上的一个实例:所有的href都抽取出来: using System; using System.Net; using System.Text; using System.Text.RegularExpressions; namespace HttpGet { c…

com编程创建快捷方式中文_如何以编程方式为博客创建wordcloud?

com编程创建快捷方式中文Recently, I was in need of an image for our blog and wanted it to have some wow effect or at least a better fit than anything typical we’ve been using. Pondering over ideas for a while, word cloud flashed in my mind. 💡Us…

ETL技术入门之ETL初认识

ETL技术入门之ETL初认识 分类: etl2014-07-10 23:11 3021人阅读 评论(2) 收藏 举报数据仓库商业价值etlbi目录(?)[-] ETL是什么先说下背景知识下面给下ETL的详细解释定义现在来看下kettle的transformation文件一个最简单的E过程例子windows环境 上图左边的是打开表…