简体   繁体   English

如何使用 DAX 在 Power BI 上进行分组

[英]How to group by on Power BI using DAX

there!那里!

I have two tables in Power BI, like this:我在 Power BI 中有两个表,如下所示:

COUNTRIES国家

COD      COUNTRY
1        BRAZIL
2        ARGENTINA
3        CHILE
4        BRASIL
5        COLOMBIA
6        ARGENTINA
7        URUGUAI

SALES销售量

COD     DATE
1       2021-01-02
2       2021-10-01
3       2019-09-04
1       2018-07-05
7       2019-04-10

There's a relationship between the two tables, on COD column.在 COD 列上,两个表之间存在关系。

I need to count how many countries(column "COUNTRY" from the table "COUNTRIES") have a status CHURN.我需要计算有多少国家(表“COUNTRIES”中的“COUNTRY”列)具有 CHURN 状态。 It's considered CHURN when their latest order from the table "SALES" has more than 180 days, using today as reference.当他们在“销售”表中的最新订单超过 180 天时,将其视为客户流失,使用今天作为参考。

So I know that I need to group by the MAX date per Country and do a DATEDIFF, and then, do a COUNT.所以我知道我需要按每个国家/地区的 MAX 日期分组并执行 DATEDIFF,然后执行 COUNT。 I'm trying using ALL,SUMMARIZE, but I'm not finding a way to solve this problem.我正在尝试使用 ALL,SUMMARIZE,但我没有找到解决此问题的方法。 May you guys help me, please?请大家帮帮我好吗?

Are you able to add a calculated column to store the max sales date for each country in your COUNTRIES table?您是否可以添加一个计算列来将每个国家/地区的最大销售日期存储在您的 COUNTRIES 表中? Either in Power BI or directly in your database.在 Power BI 中或直接在您的数据库中。 If so, here's one solution with 2 steps.如果是这样,这是一个包含 2 个步骤的解决方案。

Create a MaxSalesDate column in your COUNTRIES table.在 COUNTRIES 表中创建 MaxSalesDate 列。 DAX for a calculated column below:下面计算列的 DAX:

MaxSalesDate = 
VAR COD = COUNTRIES[COD]

RETURN MAXX(FILTER(SALES, SALES[COD] = COD), SALES[DATE])

在此处输入图像描述

Create a measure that counts the number of MaxSalesDate values that are older than 180 days old:创建一个度量超过 180 天的 MaxSalesDate 值的数量:

CountCHURN = COUNTX(COUNTRIES, IF(DATEDIFF(COUNTRIES[MaxSalesDate], TODAY(), Day) > 180, 1))

在此处输入图像描述

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

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