[英]How to add custom column for previous day sales for each product in Power Query
I have this table:我有这张桌子:
Date![]() |
Product![]() |
Sales![]() |
---|---|---|
1/3/2021 ![]() |
Apple![]() |
10 ![]() |
1/3/2021 ![]() |
Lemon![]() |
20 ![]() |
1/3/2021 ![]() |
Mango![]() |
30 ![]() |
1/4/2021 ![]() |
Apple![]() |
15 ![]() |
1/4/2021 ![]() |
Lemon![]() |
25 ![]() |
1/5/2021 ![]() |
Apple![]() |
20 ![]() |
1/5/2021 ![]() |
Lemon![]() |
20 ![]() |
1/5/2021 ![]() |
Mango![]() |
35 ![]() |
And I want to add a new column like this:我想添加一个这样的新列:
Date![]() |
Product![]() |
Sales![]() |
Prev Day Sales![]() |
---|---|---|---|
1/3/2021 ![]() |
Apple![]() |
10 ![]() |
null ![]() |
1/3/2021 ![]() |
Lemon![]() |
20 ![]() |
null ![]() |
1/3/2021 ![]() |
Mango![]() |
30 ![]() |
null ![]() |
1/4/2021 ![]() |
Apple![]() |
15 ![]() |
10 ![]() |
1/4/2021 ![]() |
Lemon![]() |
25 ![]() |
20 ![]() |
1/5/2021 ![]() |
Apple![]() |
20 ![]() |
15 ![]() |
1/5/2021 ![]() |
Lemon![]() |
20 ![]() |
25 ![]() |
1/5/2021 ![]() |
Mango![]() |
35 ![]() |
null ![]() |
You could add a column with previousDate and then join the table itself by Date-PreviousDate fields, then extract only the Sales field an rename it at wish.您可以添加一个包含 previousDate 的列,然后通过 Date-PreviousDate 字段连接表本身,然后仅提取 Sales 字段并随意重命名。
This should be null for days where no sales had taken place in the previous date.这应该是 null 表示上一个日期没有发生销售的天数。
Add a custom column with previous date as Date.AddDays([Date],-1) Then, in Home/Transform/Merge queries and select the same table to merge.添加一个自定义列,之前的日期为 Date.AddDays([Date],-1) 然后,在 Home/Transform/Merge 查询和 select 相同的表中进行合并。 Select the "Previous Date" field from the original and merge with the "Date" field on the second.
Select 原始的“上一个日期”字段并与第二个上的“日期”字段合并。 Then CTRL-Click the field "Product" to use it also in the merge.
然后 CTRL-单击“产品”字段以在合并中使用它。 Use a left outer join, so no rows are added.
使用左外连接,因此不会添加任何行。 Expand the Sales column in the merged result and, here you go!
展开合并结果中的销售列,然后就可以了! Greetings!
问候!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.