微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何组合表中的多列

如何解决如何组合表中的多列

我的问题如下:我有一个表,其中有多个包含日期和值但代表不同内容的列。这是我的标题的示例:

客户姓名 服务类型 付款 1 日期 付款 1 金额 付款 2 日期 I 付款 2 金额 I 付款 3 日期 I 付款 3 金额 I 付款 4日期 付款 4 金额

我想要做的是根据多个条件汇总表格。例如:

I 服务类型 I 第 1 个月 I 第 2 个月 I 第 3 个月 I强>第4个月 服务一
服务二
服务三

问题是我不想写 4 个 sumif(在这种情况下,但实际上我有超过 4 组日期:值列)。

我正在考虑创建一个新表,我可以将所有列放在一起(在一个有 4 列的表中 - 客户名称、服务类型、日期和付款),但该表应该是动态创建的,这意味着它应该用原始表中的新条目动态扩展(即如果原始表有 200 个条目,这将使新表具有 4x200=800 个条目,如果原始表多一条记录,那么新表应该有 4x201 =804 条记录)。

我还检查了 PowerQuery 选项,但无法理解它。

因此,我们将非常感谢您对此事的任何帮助。

谢谢。

解决方法

您当然可以使用 Power Query 创建四列表。但是,我怀疑您也可以使用 PQ 生成最终报告,因此您可以根据需要将其添加到此代码中。

它会更新,但需要“刷新”才能进行更新。

“刷新”可能被触发

  • 用户选择数据/刷新选项
  • 工作表上用户必须按下的按钮。
  • 一个 VBA 事件触发宏

无论如何,为了使查询适应不同数量的列,需要比从 UI 生成的更多的 M 代码,以及自定义函数。

下面的算法取决于采用这种格式的数据:

  • 第 1 列和第 2 列将是 Customer | Type of Service
  • 剩余的列将在 Date | Amount 之间交替并标记为:Payment N Date | Payment N Amount,其中 N 是某个数字

如果实际数据不是那种格式,可能需要对代码进行一些更改。

要使用 Power Query:

  • 选择数据表中的某个单元格
  • Data => Get&Transform => from Table/Range
  • 当 PQ 编辑器打开时:Home => Advanced Editor
  • 记下第 2 行中的表名称
  • 将下面的 M 代码粘贴到您看到的位置
  • 将第 2 行中的表名改回最初生成的名称。
  • 阅读评论并探索Applied Steps以了解算法

在 PQ Editord 中进入自定义功能

  • 在查询窗格中右键单击
  • 从空白查询添加新查询
  • 将自定义函数代码粘贴到高级编辑器中
  • 重命名查询 fnPivotAll

M 代码

let

//Change Table name in next line to be the Actual table name in your workbook
    Source = Excel.CurrentWorkbook(){[Name="Table8"]}[Content],/*set datatypes dynamically with
   first two columns as Text 
   and subsequent columns alternating as Date and Currency*/
textType = List.Transform(List.FirstN(Table.ColumnNames(Source),2),each {_,Text.Type}),otherType = List.RemoveFirstN(Table.ColumnNames(Source),dateType = List.Transform(
                List.Alternate(otherType,1,1),Date.Type}),currType = List.Transform(
                List.Alternate(otherType,0),Currency.Type}),colTypes = List.Combine({textType,dateType,currType}),typeIt = Table.TransformColumnTypes(Source,colTypes),//Unpivot all except first two columns
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(typeIt,List.FirstN(Table.ColumnNames(Source),"Attribute","Value"),//Remove "Payment n " from attribute column
    remPmtN = Table.TransformColumns(#"Unpivoted Other Columns",{{"Attribute",each Text.Split(_," "){2},Text.Type}}),//Pivot on the Attribute column without aggregation using Custom Function
    pivotAll = fnPivotAll(remPmtN,typeIt2 = Table.TransformColumnTypes(pivotAll,{{"date",Date.Type},{"amount",Currency.Type}})
in
    typeIt2

自定义函数:fnPivotAll

//credit: Cam Wallace  https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/

(Source as table,ColToPivot as text,ColForValues as text)=> 

let
     PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))),#"Pivoted Column" = Table.Pivot(Source,PivotColNames,ColToPivot,ColForValues,each _),TableFromRecordOfLists = (rec as record,fieldnames as list) =>
    
    let
        PartialRecord = Record.SelectFields(rec,fieldnames),RecordToList = Record.ToList(PartialRecord),Table = Table.FromColumns(RecordToList,fieldnames)
    in
        Table,#"Added Custom" = Table.AddColumn(#"Pivoted Column","Values",each TableFromRecordOfLists(_,PivotColNames)),#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns",PivotColNames)
in
    #"Expanded Values"

示例数据
enter image description here

输出
enter image description here

如果这不能满足您的要求,或者您在进一步处理以生成所需报告时遇到问题,请回帖。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。