简体   繁体   English

基于计算字段的 Power BI/PowerPivot 模型中的动态分组

[英]Dynamic Grouping in Power BI/PowerPivot Model based on a calculated field

We are trying to create a model in Excel/Power BI (using Power Query or Power Pivot or anything that would work) in order to classify a customer by its best product (based on a ranking system).我们正在尝试在 Excel/Power BI 中创建模型(使用 Power Query 或 Power Pivot 或任何可行的方法),以便按客户的最佳产品(基于排名系统)对其进行分类。

The first approach we applied was to count the customers per minimum ranking (or per best product brand).我们采用的第一种方法是按最低排名(或按最佳产品品牌)计算客户数。 (inspired from the blog https://stackoverflow.com/questions/15742186/powerpivot-dax-dynamic-ranking-per-group-min-per-group ) (灵感来自博客https://stackoverflow.com/questions/15742186/powerpivot-dax-dynamic-ranking-per-group-min-per-group

Below the steps we did exactly: - In PowerPivot Model, we created Classification and Customers table like in the example further below.下面是我们完全执行的步骤: - 在 PowerPivot 模型中,我们创建了分类客户表,如下面的示例所示。

  • In the same model, we added a calculated column with the following formula to obtain the minimum rank per customer .在同一个模型中,我们添加了一个计算列,使用以下公式来获得每个客户最低排名

     =MINX ( FILTER ( ALLSELECTED ( Customers ); [Customer_ID] = EARLIEST ( [Customer_ID] ) ); [Ranking] )
  • Within a pivot table in Excel, we've put the calculated column in rows.在 Excel 的数据透视表中,我们将计算的列放在行中。

  • Then, we've used a Count distinct aggregation of the customers in the pivot table values.然后,我们在数据透视表值中使用了客户Count 不同聚合  This gave me the first desired result. ——这给了我第一个想要的结果。 (below example Pivot_Table.Selection1) (以下示例 Pivot_Table.Selection1)

Now, the issue comes when we want to add more analysis axis .现在,当我们想要添加更多分析轴时,问题就来 For example, besides the product brand, we want to have the Product type in columns, and we want our measure to be recalculated every time I add/delete an axis.例如,除了产品品牌之外,我们希望在列中包含产品类型,并且我们希望每次添加/删除轴时重新计算我们的度量 In other words, we want to have a distinct customer count per best product and per Product Type.换句话说,我们希望每个最佳产品和每个产品类型都有不同的客户数量。 In addition, we want the second attribute (axis) to be variable and the grouping or the distinct count per group to be dynamic.此外,我们希望第二个属性(轴)是可变的,并且分组或每组的不同计数是动态的。

Example :示例

Let's suppose we have the tables Classification and Customers in our Model:假设我们的模型中有分类表和客户表:

在此处输入图片说明 在此处输入图片说明

In the first approach we tried, we got the following table: Pivot_Table.Selection1:在我们尝试的第一种方法中,我们得到了下表:Pivot_Table.Selection1:

在此处输入图片说明

Now when we add the analysis axis, we would like to have the following example: Pivot_Table.Selection2:现在,当我们添加分析轴时,我们希望有以下示例:Pivot_Table.Selection2:

在此处输入图片说明

But we are having this:但是我们有这个:

在此处输入图片说明

As you can see, there should be one customer for the Group “Mercedes” and one for “Renault”, since depending on the product type, the top Truck for customer A is Renault and its top Car is “Mercedes”.如您所见,“Mercedes”组应该有一个客户,“Renault”组应该有一个客户,因为根据产品类型,客户 A 的顶级卡车是雷诺,其顶级汽车是“梅赛德斯”。 However, in the pivot table, the Mercedes group is shown as Truck (which doesn't even exist in our dataset).然而,在数据透视表中,梅赛德斯组显示为卡车(在我们的数据集中甚至不存在)。

在此处输入图片说明

Edit编辑

I'm open for any suggestion, not only Power Pivot, but also Power Query (M functions) or Power BI or whatever could work.我愿意接受任何建议,不仅是 Power Pivot,还有 Power Query(M 函数)或 Power BI 或任何可行的建议。

Finally I think I understood your problem, a customer can have different Product_Brand values, you want to count only those Product_Brand which its ranking is the minimum.最后我想我明白了你的问题,一个客户可以有不同的 Product_Brand 值,你只想计算其排名最低的那些 Product_Brand。

In that case, this is a possible solution:在这种情况下,这是一个可能的解决方案:

Create a calculated column called Minimum Rank in the Customer table.Customer表中创建一个名为Minimum Rank的计算列。

=
CALCULATE (
    MIN ( [Ranking] );
    FILTER ( Customer; [Customer_ID] = EARLIER ( Customer[Customer_ID] ) )
)

Then create a measure, lets say Customer ID Distinct Count to count those rows where the Rank is equal to the minimum for that customer.然后创建一个度量,比如Customer ID Distinct Count来计算那些 Rank 等于该客户最小值的行。

Customer ID Distinct Count :=
CALCULATE (
    DISTINCTCOUNT ( Customer[Customer_ID] );
    FILTER ( Customer; [Ranking] = [Minimum Rank] )
)

You will get something like this:你会得到这样的东西:

在此处输入图片说明 在此处输入图片说明

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

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