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.pswp.cn/news/388200.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388200.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388200.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

關于如何在Python中使用靜態、類或抽象方法的權威指南

Python中方法的工作方式 方法是存儲在類屬性中的函數,你可以用下面這種方式聲明和訪問一個函數 >>> class Pizza(object):... def __init__(self, size):... self.size size... def get_size(self):... return self.size...>&…

廣義估計方程估計方法_廣義估計方程簡介

廣義估計方程估計方法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) …

css二

結構性偽類:nth-child(index)系列1.:first-child2.:last-child3.nth-last-child(index)4.only-child :nth-of-type(index)系列1.first-of-type2.last-of-type3.nth-last-type(index)4.only-of-type :not偽類處理導航欄最后一個豎劃線a:not(:last-of-type) :empty偽類 選中所有內…

Unity3d鼠標點擊屏幕來控制人物的走動

今天呢,我們來一起實現一個在RPG中游戲中十分常見的功能,通過鼠標點擊屏幕來控制人物的走動。首先來說一下原理,當我們點擊屏幕時,我們按照一定的方法,將屏幕上的二維坐標轉化為三維坐標,然后我們從攝像機位…

Java中的ReentrantLock和synchronized兩種鎖定機制的對比

2019獨角獸企業重金招聘Python工程師標準>>> 多線程和并發性并不是什么新內容,但是 Java 語言設計中的創新之一就是,它是第一個直接把跨平臺線程模型和正規的內存模型集成到語言中的主流語言。核心類庫包含一個 Thread 類,可以用它…

10.15 lzxkj

幾天前寫的,忘了放了,在此填坑 10月16的題我出的不寫題解了 lzxkj 題目背景 眾所不周知的是, 酒店之王 xkj 一個經常迷失自我的人 有一天, 當起床鈴再一次打響的時候, TA 用 O(1)的時間在 TA 那早就已經生銹的大腦中自…

大數定理 中心極限定理_中心極限定理:直觀的遍歷

大數定理 中心極限定理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…

萬惡之源 - Python數據類型二

列表 列表的介紹 列表是python的基礎數據類型之一 ,其他編程語言也有類似的數據類型. 比如JS中的數 組, java中的數組等等. 它是以[ ]括起來, 每個元素用 , 隔開而且可以存放各種數據類型: lst [1,a,True,[2,3,4]]列表相比于字符串,不僅可以存放不同的數據類型.而且可…

230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BSTs total elements.Example 1: Input: root [3,1,4,null,2], k 13/ \1 4\2 Output: 1 Example 2: Input: root …

探索性數據分析(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…

unity3d 攝像機跟隨鼠標和鍵盤的控制

鼠標控制: using UnityEngine; using System.Collections; public class shubiao : MonoBehaviour { //public Transform firepos; public int Ball30; public int CurBall1; public Rigidbody projectile; public Vector3 point; public float time100f; public…

《必然》九、享受重混盛宴,是每個人的機會

今天說的是《必然》的第七個關鍵詞,過濾Filtering。1我們需要過濾如今有一個問題,彌漫在我們的生活當中,困擾著所有人。那就是“今天我要吃什么呢?”同樣的,書店里這么多的書,我要看哪一本呢?網…

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…

換熱站起停條件

循環泵 自動條件: 一、循環泵啟動條件 兩臺泵/三臺泵: 1)本循環泵在遠程狀態 2)本循環泵自動狀態 3)本循環泵沒有故障 4)二次網的回水壓力(測量值)>設定值 5)…

云尚制片管理系統_電影制片廠的未來

云尚制片管理系統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單向鏈表實現 單向鏈表 鏈表和數組一樣是一種最常用的線性數據結構,兩者各有優缺點。數組我們知道是在內存上的一塊連續的空間構成,所以其元素訪問可以通過下標進行,隨機訪問速度很快,但數組也有其缺點,由于數組的…