如何使用 power query 折叠 excel 中的多行单元格值
How to collapse multiple rows of cell values in excel using powerquery
如何折叠多行数据以使其与其唯一 ID 相匹配。数据集是从网络动态加载的。它按日期排序,以便每个新条目更新前面的条目。
注:
我既不想删除某些空值,也不想通过旋转忽略它们,因为其中一些空值是尚未填充的数据。
我在sheet上有一个数据行散乱的示例文件 1.我想得到的解决方案在sheet上 2.如何实现?这是最复杂的任务。如果这个挑战得到解决,我会很高兴。
这是 link:
https://ibb.co/3kVkcBL
或类似的东西
这是一种方法:
阅读代码注释以更好地理解算法
编辑: 修复缺少成绩的问题
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"ID", Int64.Type}, {"NAME", type text}, {"CLASS", type text},
{"CACHEMISTRY", Int64.Type}, {"EXAM CHEMISTRY", Int64.Type},
{"TOTALCHEM", Int64.Type}, {"GRADECHEM", type text}, {"CABIOLOGY", Int64.Type},
{"EXAMBIOLOGY", Int64.Type}, {"TOTALBIOLOGY", Int64.Type}, {"GRADEBIOLOGY", type text},
{"Date", type datetimezone}}),
//There some "string" nulls. Will replace them with real nulls so we can "fill up"
replaceNulls = Table.ReplaceValue(#"Changed Type","null",null,Replacer.ReplaceValue,Table.ColumnNames(#"Changed Type")),
//Group by Name and ID
// then fill up all the columns and return the last row only
group = Table.Group(replaceNulls,{"ID","NAME"},{
{"newTable", each Table.Last(Table.FillDown(Table.FillUp(_,Table.ColumnNames(_)),Table.ColumnNames(_)))}
}),
//Expand the results
columnsToExpand = List.RemoveFirstN(Table.ColumnNames(replaceNulls),2),
#"Expanded newTable" = Table.ExpandRecordColumn(group, "newTable",
columnsToExpand,columnsToExpand),
//Set Data Types
// Note that Date column is typed as text
// This is so it will display near properly in Excel (it will be missing the "T"
// You may need to modify this depending on whether this is satisfactory or not
typeIt = Table.TransformColumnTypes(#"Expanded newTable",{
{"CLASS", type text},
{"CACHEMISTRY", Int64.Type}, {"EXAM CHEMISTRY", Int64.Type},
{"TOTALCHEM", Int64.Type}, {"GRADECHEM", type text}, {"CABIOLOGY", Int64.Type},
{"EXAMBIOLOGY", Int64.Type}, {"TOTALBIOLOGY", Int64.Type}, {"GRADEBIOLOGY", type text},
{"Date", type text}})
in
typeIt
注:
存在一些不一致的差异。其中一些可能是拼写错误,其他我不确定。这些都可以解决,但是你需要建立一致的规则
- 似乎混合了字符串“null”和实际的 null 值。我将它们全部转换为实际的 null 值,但这很容易更改。
- Ben Victor EXAMBIOLOGY 在工作表 1 上是 40,但在工作表 2 上是 60
- Ben Victor Datetime 在 sheet 1 上是
2022-03-16T15:14:00.000+01:00
和 2022-04-14T11:47:00.000+01:00
,但在 sheet 2 上是 2022-03-16T11:47:00.000+01:00
。应该是哪个?
原始 Table 来自 Google Drive
运行 以上查询后的结果
请注意,日期的格式与您显示的不同,但这是一个简单的修复
如何折叠多行数据以使其与其唯一 ID 相匹配。数据集是从网络动态加载的。它按日期排序,以便每个新条目更新前面的条目。
注:
我既不想删除某些空值,也不想通过旋转忽略它们,因为其中一些空值是尚未填充的数据。
我在sheet上有一个数据行散乱的示例文件 1.我想得到的解决方案在sheet上 2.如何实现?这是最复杂的任务。如果这个挑战得到解决,我会很高兴。
这是 link: https://ibb.co/3kVkcBL
或类似的东西
这是一种方法:
阅读代码注释以更好地理解算法
编辑: 修复缺少成绩的问题
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"ID", Int64.Type}, {"NAME", type text}, {"CLASS", type text},
{"CACHEMISTRY", Int64.Type}, {"EXAM CHEMISTRY", Int64.Type},
{"TOTALCHEM", Int64.Type}, {"GRADECHEM", type text}, {"CABIOLOGY", Int64.Type},
{"EXAMBIOLOGY", Int64.Type}, {"TOTALBIOLOGY", Int64.Type}, {"GRADEBIOLOGY", type text},
{"Date", type datetimezone}}),
//There some "string" nulls. Will replace them with real nulls so we can "fill up"
replaceNulls = Table.ReplaceValue(#"Changed Type","null",null,Replacer.ReplaceValue,Table.ColumnNames(#"Changed Type")),
//Group by Name and ID
// then fill up all the columns and return the last row only
group = Table.Group(replaceNulls,{"ID","NAME"},{
{"newTable", each Table.Last(Table.FillDown(Table.FillUp(_,Table.ColumnNames(_)),Table.ColumnNames(_)))}
}),
//Expand the results
columnsToExpand = List.RemoveFirstN(Table.ColumnNames(replaceNulls),2),
#"Expanded newTable" = Table.ExpandRecordColumn(group, "newTable",
columnsToExpand,columnsToExpand),
//Set Data Types
// Note that Date column is typed as text
// This is so it will display near properly in Excel (it will be missing the "T"
// You may need to modify this depending on whether this is satisfactory or not
typeIt = Table.TransformColumnTypes(#"Expanded newTable",{
{"CLASS", type text},
{"CACHEMISTRY", Int64.Type}, {"EXAM CHEMISTRY", Int64.Type},
{"TOTALCHEM", Int64.Type}, {"GRADECHEM", type text}, {"CABIOLOGY", Int64.Type},
{"EXAMBIOLOGY", Int64.Type}, {"TOTALBIOLOGY", Int64.Type}, {"GRADEBIOLOGY", type text},
{"Date", type text}})
in
typeIt
注:
存在一些不一致的差异。其中一些可能是拼写错误,其他我不确定。这些都可以解决,但是你需要建立一致的规则
- 似乎混合了字符串“null”和实际的 null 值。我将它们全部转换为实际的 null 值,但这很容易更改。
- Ben Victor EXAMBIOLOGY 在工作表 1 上是 40,但在工作表 2 上是 60
- Ben Victor Datetime 在 sheet 1 上是
2022-03-16T15:14:00.000+01:00
和2022-04-14T11:47:00.000+01:00
,但在 sheet 2 上是2022-03-16T11:47:00.000+01:00
。应该是哪个?
原始 Table 来自 Google Drive
运行 以上查询后的结果
请注意,日期的格式与您显示的不同,但这是一个简单的修复