更新了一下代码,实现 小计 及 总计。
是这个效果吗? 好就上花!
- let
- 源 = Table.SelectRows(
- Excel.Workbook(File.Contents("C:\Users\飞飞\Desktop\VBA\12-11-1.xlsx"), null, true),
- each [Name] <> "结果"
- )[Data],
- Tab1 = Table.SelectRows(
- Table.SelectColumns(
- Table.Combine(List.Transform(源, each Table.PromoteHeaders(_))),
- {"机构", "身份证号码", "收入"}
- ),
- each [机构] <> null
- ),
- fx = (R) =>
- [
- a = Text.From(R{1}),
- Year = Number.From(Text.Range(a, 6, 4)),
- Mon = Number.From(Text.Range(a, 10, 2)),
- Day = Number.From(Text.Range(a, 12, 2)),
- Old = Number.Round(
- Duration.TotalDays(
- Date.From(DateTime.FixedLocalNow()) - Date.From(#date(Year, Mon, Day))
- )
- / 365,
- 2
- ),
- gx = (S, T) =>
- if T < 30 then
- "0-30"
- else if S > T then
- Text.Format("#{0}-#{1}", {Text.From(T + 1), Text.From(T + 10)})
- else
- @gx(S, T - 10),
- O = gx(Old, 60),
- Sex = if Number.IsEven(Number.From(Text.Range(a, 16, 1))) then "女" else "男",
- H = {R{0}, O, Sex, R{2}}
- ][H],
- Lst = List.Transform(Table.ToRows(Tab1), each fx(_)),
- Tab2 = Table.FromRows(Lst),
- Group = Table.Group(
- Tab2,
- "Column1",
- {
- "c",
- (z) =>
- Table.Group(
- z,
- "Column2",
- {
- "d",
- each [
- Mal = List.Count(List.Select([Column3], (x) => x = "男")),
- Fam = List.Count([Column3]) - Mal,
- MM = List.Sum(
- Table.SelectRows(_, (y) => y[Column3] = "男")[
- Column4
- ]
- ),
- FM = List.Sum([Column4]) - MM,
- k = Table.FromRows(
- {{Mal, Fam, MM, FM}},
- {"男数", "女数", "男收", "女收"}
- )
- ][k]
- }
- )
- }
- ),
- Expand = Table.TransformColumns(
- Group,
- {
- "c",
- each [
- a = Table.Sort(
- Table.ExpandTableColumn(_, "d", {"男数", "女数", "男收", "女收"}),
- {(x) => Text.BeforeDelimiter(x[Column2], "-"), 0}
- ),
- b = Table.Group(
- a,
- {},
- List.Transform(
- Table.ColumnNames(a),
- (y) =>
- {y, (z) => try List.Sum(Table.Column(a, y)) otherwise "小计"}
- )
- ),
- c = a & b
- ][c]
- }
- ),
- Res = [
- a = Table.ExpandTableColumn(Expand, "c", {"Column2", "男数", "女数", "男收", "女收"}),
- b = Table.SelectRows(a, each [Column2] = "小计"),
- c = Table.Group(
- b,
- {},
- List.Transform(
- List.Skip(Table.ColumnNames(b)),
- each {_, (x) => try List.Sum(Table.Column(b, _)) otherwise "总计"}
- )
- ),
- d = a & c
- ][d]
- in
- Res
复制代码
|