英文:
I want to add columns across filtered rows in power query
问题
我有一个Excel数据集,看起来像这样:
Str_ID | Store_Name | Sales_Volume | Sales incl VAT | Sales ex VAT |
---|---|---|---|---|
24 | A Staff Shop | 10000 | 329031 | 438708 |
34 | A Dotcom | 560000 | 18425736 | 24567648 |
75 | B Staff Shop | 45000 | 1480639.5 | 1974186 |
65 | B Dotcom | 780000 | 25664418 | 34219224 |
45 | C Staff Shop | 34500 | 1135156.95 | 1513542.6 |
595 | C Dotcom | 789230 | 25968113.61 | 34624151.48 |
9650 | D | 54053 | 1778511.264 | 2371348.352 |
2302 | E | 23453 | 771676.4043 | 1028901.872 |
242 | F | 23121 | 760752.5751 | 1014336.767 |
我想要删除员工商店并将相应的列值添加到相应的Dotcom商店。因此,A Staff Shop的相应Dotcom将是A Dotcom。输出应如下所示:
Str_ID | Store_Name | Sales_Volume | Sales incl VAT | Sales ex VAT |
---|---|---|---|---|
34 | A Dotcom | 570000 | 18754767 | 25006356 |
65 | B Dotcom | 825000 | 27145057.5 | 36193410 |
595 | C Dotcom | 823730 | 27103270.56 | 36137694.08 |
9650 | D | 54053 | 1778511.264 | 2371348.352 |
2302 | E | 23453 | 771676.4043 | 1028901.872 |
242 | F | 23121 | 760752.5751 | 1014336.767 |
请注意,商店的名称仅用于说明,而不是单个字符。此外,数据集中有超过30列。
在Excel中,我筛选了员工商店的列,并在新标签中记录了它们的名称,然后手动搜索每个名称,使用小计功能并替换了Dotcom商店的值,删除了员工商店。数据每个月都会更新,新的商店会添加进来。我正在寻找一种自动化此过程的方法,任何帮助都将不胜感激。我也向chatgpt寻求帮助,但提到的步骤只会导致错误。
英文:
I have a dataset in Excel which looks like this:
Str_ID | Store_Name | Sales_Volume | Sales incl VAT | Sales ex VAT |
---|---|---|---|---|
24 | A Staff Shop | 10000 | 329031 | 438708 |
34 | A Dotcom | 560000 | 18425736 | 24567648 |
75 | B Staff Shop | 45000 | 1480639.5 | 1974186 |
65 | B Dotcom | 780000 | 25664418 | 34219224 |
45 | C Staff Shop | 34500 | 1135156.95 | 1513542.6 |
595 | C Dotcom | 789230 | 25968113.61 | 34624151.48 |
9650 | D | 54053 | 1778511.264 | 2371348.352 |
2302 | E | 23453 | 771676.4043 | 1028901.872 |
242 | F | 23121 | 760752.5751 | 1014336.767 |
I want to remove the staff shops and add the corresponding column values to their corresponding dotcom stores. So, A Staff Shop's corresponding dotcom would be A dotcom. The output should look this:
Str_ID | Store_Name | Sales_Volume | Sales incl VAT | Sales ex VAT |
---|---|---|---|---|
34 | A Dotcom | 570000 | 18754767 | 25006356 |
65 | B Dotcom | 825000 | 27145057.5 | 36193410 |
595 | C Dotcom | 823730 | 27103270.56 | 36137694.08 |
9650 | D | 54053 | 1778511.264 | 2371348.352 |
2302 | E | 23453 | 771676.4043 | 1028901.872 |
242 | F | 23121 | 760752.5751 | 1014336.767 |
Please note the name of the stores are for illustration only and not single characters. Also, there are more than 30 columns in the dataset.
In Excel, I filtered the column for the staff shops and noted their names in a new tab, then manually searched each name and used subtotal and replaced the dotcom store values, deleting the staff shops.
The data is updated every month with new stores added. I am looking for a way to automate this, and any help would be appreciated. I also went to chatgpt with this, but the steps mentioned only lead to errors.
答案1
得分: 0
以下是翻译好的代码部分:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Filtered Rows" = Table.Distinct(Table.SelectRows(Source, each not Text.Contains([Store_Name], "Staff Shop")), {"Store_Name"}),
#"Replaced Value" = Table.ReplaceValue(Source, "Staff Shop", "Dotcom", Replacer.ReplaceText, {"Store_Name"}),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value", {"Str_ID"}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Store_Name"}, "Attribute", "Value"),
#"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value", List.Sum),
#"Merged Queries" = Table.NestedJoin(#"Pivoted Column", {"Store_Name"}, #"Filtered Rows", {"Store_Name"}, "Table1", JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Str_ID"}, {"Str_ID"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Table1", Table.ColumnNames(Source))
in #"Reordered Columns"
alternate version that might work faster for large data sets
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Filtered Rows" = Table.Distinct(Table.SelectRows(Source, each not Text.Contains([Store_Name], "Staff Shop")), {"Store_Name"}),
#"Replaced Value" = Table.ReplaceValue(Source, "Staff Shop", "Dotcom", Replacer.ReplaceText, {"Store_Name"}),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value", {"Str_ID"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Store_Name"}, {{"data", each let
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(_, {"Store_Name"}, "Attribute", "Value")
in Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value", List.Sum)
, type table }}),
List = List.RemoveItems(List.Union(List.Transform(#"Grouped Rows"[data], each Table.ColumnNames(_)), {"Store_Name"})),
#"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "data", List, List),
#"Merged Queries" = Table.NestedJoin(#"Expanded Data", {"Store_Name"}, #"Filtered Rows", "Table1", JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Str_ID"}, {"Str_ID"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Table1", Table.ColumnNames(Source))
in #"Reordered Columns"
英文:
Try
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Filtered Rows" = Table.Distinct(Table.SelectRows(Source, each not Text.Contains([Store_Name], "Staff Shop")), {"Store_Name"}),
#"Replaced Value" = Table.ReplaceValue(Source,"Staff Shop","Dotcom",Replacer.ReplaceText,{"Store_Name"}),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value",{"Str_ID"}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Store_Name"}, "Attribute", "Value"),
#"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value", List.Sum),
#"Merged Queries" = Table.NestedJoin(#"Pivoted Column", {"Store_Name"}, #"Filtered Rows", {"Store_Name"}, "Table1", JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Str_ID"}, {"Str_ID"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Table1",Table.ColumnNames(Source))
in #"Reordered Columns"
alternate version that might work faster for large data sets
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Filtered Rows" = Table.Distinct(Table.SelectRows(Source, each not Text.Contains([Store_Name], "Staff Shop")), {"Store_Name"}),
#"Replaced Value" = Table.ReplaceValue(Source,"Staff Shop","Dotcom",Replacer.ReplaceText,{"Store_Name"}),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value",{"Str_ID"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Store_Name"}, {{"data", each let
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(_, {"Store_Name"}, "Attribute", "Value")
in Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value", List.Sum)
, type table }}),
List = List.RemoveItems(List.Union(List.Transform(#"Grouped Rows"[data], each Table.ColumnNames(_))),{"Store_Name"}),
#"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "data", List,List),
#"Merged Queries" = Table.NestedJoin(#"Expanded Data", {"Store_Name"}, #"Filtered Rows", "Table1", JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Str_ID"}, {"Str_ID"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Table1",Table.ColumnNames(Source))
in #"Reordered Columns"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论