excel - Aggregation/Summation of text and numeric fields
问题描述
Raw data
SiteName-----Agency------Staff Numbers
Site1-----------A1------------10
Site1-----------A1------------12
Site1-----------A1------------11
Site1-----------A2-------------5
Wondering how I can get the following in my pivot report;
Site1-------------A1/A2---------33/5
a summation/aggregation of both the "Agency" and "Staff Numbers";
Note that I have successfully aggregated on the "Agency" field (text) using concatenatex with a "/" delimiter, but when it comes to the "Staff Numbers" field (numeric) I am not getting any summation of the staff numbers.
I get;
Site1--------A1/A2----------10/12/11/5--------> this is undesirable
I want;
Site1--------A1/A2----------33/5---------------> this is desirable
解决方案
CONCATANATEX
is a DAX function, isn't it?
In any event, you can do this in Power Query with a nested Table.Group
function.
To use Power Query
- Select some cell in your Data Table
Data => Get&Transform => from Table/Range
- When the PQ Editor opens:
Home => Advanced Editor
- Make note of the Table Name in Line 2
- Paste the M Code below in place of what you see
- Change the Table name in line 2 back to what was generated originally.
- Read the comments and explore the
Applied Steps
to understand the algorithm
M Code
let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"SiteName", type text}, {"Agency", type text}, {"Staff Numbers", Int64.Type}}),
//Group by SiteName
#"Grouped Rows" = Table.Group(#"Changed Type", {"SiteName"}, {
//then subGroup by Agency
{"Agency", each Table.Group(_,"Agency", {
{"Staff Numbers", each List.Sum(_[Staff Numbers])}
})}
}),
//Combine the agencies/staff
agencies = Table.AddColumn(#"Grouped Rows", "Agencies", each Text.Combine([Agency][Agency],"/")),
staff = Table.AddColumn(agencies, "Staff Numbers", each
Text.Combine(
List.Transform([Agency][Staff Numbers], each Text.From(_)),
"/")),
//remove unneeded column
#"Removed Columns" = Table.RemoveColumns(staff,{"Agency"})
in
#"Removed Columns"
推荐阅读
- php - htaccess 无法将文章 ID 重定向到使用 php slug 函数生成的文章 slug
- firebase - Firebase Stream 功能无法正常工作
- php - 具有多个条件的 laravel 查询更新
- powerbi - Microsoft Power BI:折线图过去 13 周的数据 3 年
- vba - 在列中查找第 n 次出现 - Excel VBA
- c++ - Windows Sockets 理解接收和发送
- r - Convert latitude and longitude to Spatial Points and project them over another map in R
- python - 有没有办法在没有任何导入包的情况下在 python 中显示简单的图形可视化
- python - 映射器代码使用 unix 管道运行,但不使用 hadoop 流。错误不适用。流式传输命令失败
- r - max.col(R语言)的反函数是什么