简体   繁体   English

在 Power BI 中对两列进行分组后创建一列值总和

[英]Create a column of sum of values after grouping two columns in Power BI

I am trying to sum values of third column based on first two columns and enter in new column.我正在尝试根据前两列对第三列的值求和并输入新列。

Day       Product  type price  total
1/1/2019  A1       T1   3      8
1/1/2019  A1       T2   5      8
1/2/2019  A2       T1   2      3
1/2/2019  A2       T2   1      3
1/1/2019  B1       T1   4      12
1/1/2019  B1       T2   7      12
1/2/2019  B2       T1   3      5
1/2/2019  B2       T2   2      5
1/3/2019  A1       T2   2      8
1/4/2019  A2       T1   9      11
1/3/2019  B1       T1   6      11
1/3/2019  B1       T2   5      11
1/4/2019  B2       T1   4      4

Total is sum of price regardless of type and unique as combination of date Product.总计是价格的总和,无论类型如何,并且作为日期产品的组合是唯一的。 check these excel columns检查这些 excel 列

Go to the Edit Queries > Add Column > Custom Column and use something like this: Go 到Edit Queries > Add Column > Custom Column并使用如下内容:

= if [Product] = "A1" and [type] = "T1" then [price] * [total] else [price] * [total] * 2

This calcualation is just an example how its done because you didnt provide any information what your criterias are to sum the values in the third column.此计算只是其完成方式的一个示例,因为您没有提供任何信息,因为您没有提供任何信息来总结第三列中的值。 But with this example you should be able to create your new column by yourself.但是通过这个示例,您应该能够自己创建新列。

It is normally not recommended to add a column for summarized values.通常不建议为汇总值添加列。 Summarization is supposed to be done with measures .总结应该是用措施来完成的。

It is very easy to get the Total for each Day and Product.很容易获得每一天和每一产品的总数。 First you will define a measure.首先,您将定义一个度量。 In the Modeling tab, click New Measure and type Total = SUM(Sales[Price]) .在 Modeling 选项卡中,单击 New Measure 并输入Total = SUM(Sales[Price]) I'm assuming the name of your table to be "Sales", so you need to replace it with your own table name.我假设您的表格名称为“Sales”,因此您需要将其替换为您自己的表格名称。

Then in the report, choose an appropriate visualization and drag and drop Day, Product, and Total.然后在报告中,选择适当的可视化并拖放 Day、Product 和 Total。 The measure Total calculates the sum of Price for each Day and Product on the fly.度量 Total 即时计算每一天的价格和产品的总和。

It is also possible to keep the Total of Day and Product in a column inside the model.也可以在 model 内的列中保留总天数和产品。 However, this is not a best practice .但是,这不是最佳做法 Before doing this, try to find a way with measures, and only do this if you are an experienced user and you know there is some good reason to do this.在执行此操作之前,请尝试找到一种措施方法,并且仅当您是经验丰富的用户并且您知道这样做有充分理由时才执行此操作。

In this case, in the Modeling tab, click New Column and input this formula.在这种情况下,在“建模”选项卡中,单击“新建列”并输入此公式。

Total of Day and Product = CALCULATE(
    SUM(Sales[Price]),
    ALLEXCEPT(Sales, Sales[Day], Sales[Product])
)

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

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