我可以分割多个列,但保留一些单元格合并吗?

huangapple go评论64阅读模式
英文:

Can I split multiple columns but leave some cells merged?

问题

我明白你需要将CSV文件中的数据进行处理,将"Reference 1"和"Delivery"两列拆分为行,同时合并"Miles"和"Price"列中的重复数据。以下是处理后的数据示例:

| Job No. | Date             | Reference 1 | Delivery                                     | Miles | Price |
|---------|------------------|-------------|----------------------------------------------|-------|-------|
| BTM1607 | 03/05/2023 08:00 | GAINSB      | Gainsborough Bathrooms Ltd, Walsall, WS9 8SR | 33    | 95    |
| BTM1611 | 02/05/2023 06:00 | 569189      | Mr Owen, Stockport, SK12 1HH                | 128   | 192   |
| BTM1611 | 02/05/2023 06:00 | 570075      | Mr Michael Hobbs, Bury, BL9 9RE              | 128   | 192   |
| BTM1612 | 02/05/2023 07:00 | 570078      | Mrs Gillian Bunce, Sutton Coldfield, B75 6AG | 35    | 52.5  |
| BTM1612 | 02/05/2023 07:00 | 569890      | Mr Richard Scott, Sutton Coldfield, B74 2AL  | 35    | 52.5  |
| BTM1613 | 02/05/2023 06:00 | 418719      | D Jiggens, Twickenham, TW2 6PU               | 125   | 187.5 |
| BTM1613 | 02/05/2023 06:00 | 570246      | Mrs J Smith, Surbiton, KT6 6QP               | 125   | 187.5 |
| BTM1614 | 02/05/2023 06:00 | 569539      | Mr Charles Docherty, London, SE4 2DX         | 136   | 204   |
| BTM1614 | 02/05/2023 06:00 | 570036      | Mrs Anne Kirby, Dagenham, RM8 2PU            | 136   | 204   |

如果需要进一步的帮助或有其他问题,请告诉我。

英文:

I have a csv file with loads of data. It looks like the below screenshot:
enter image description here.

Basically what I need is to split 2 columns into rows with power query (reference 1 and Delivery).
Reference 1 is always separated by space and Delivery data is always separated by line feed.
So it would look like the below screenshot, so each reference is matched up to its delivery.
However I need the miles and the price NOT to be duplicated as this would give us wrong total numbers, so I need those merged. (There's also a Job no column in case this would help - this would indicate if 2 or 3 deliveries were done together.
This is how I would need it:
enter image description here

Any help would be appreciated

I have tried splitting the columns separately but then it duplicates data and I would still need to manually delete the extra rows.

Job No. Date Reference 1 Delivery Miles Price
BTM1607 03/05/2023 08:00 GAINSB Gainsborough Bathrooms Ltd, Walsall, WS9 8SR 33 95
BTM1611 02/05/2023 06:00 569189 570075 Mr Owen, Stockport, SK12 1HHMr Michael Hobbs, Bury, BL9 9RE 128 192
BTM1612 02/05/2023 07:00 570078 569890 Mrs Gillian Bunce, Sutton Coldfield, B75 6AGMr Richard Scott, Sutton Coldfield, B74 2AL 35 52.5
BTM1613 02/05/2023 06:00 418719 570246 D Jiggens, Twickenham, TW2 6PUMrs J Smith, Surbiton, KT6 6QP 125 187.5
BTM1614 02/05/2023 06:00 569539 570036 Mr Charles Docherty, London, SE4 2DXMrs Anne Kirby, Dagenham, RM8 2PU 136 204
BTM1615 02/05/2023 06:00 569435 570025 Mr Brian Harrison, Stoke-on-Trent, ST1 2ARMrs Shirley Steiert, Ellesmere Port, CH65 6RA 118 177
BTM1616 02/05/2023 06:00 569249 569172 Mrs Jill Gardham, Bath, BA2 6PLMr Roy Foster, Shaftesbury, SP7 0PJ 134 201
BTM1617 02/05/2023 06:00 569903 569287 Mrs Hill, Bristol, BS15 9UJMrs A Devlin, Cardiff, CF14 1EH 132 198

I need it in the below format (but with the duplicated miles&price merged into 1), but I can't find a solution anywhere.

Job No. Date Reference 1 Delivery Miles Price
BTM1607 03/05/2023 08:00 GAINSB Gainsborough Bathrooms Ltd, Walsall, WS9 8SR 33 95
BTM1611 02/05/2023 06:00 569189 Mr Owen, Stockport, SK12 1HH 128 192
BTM1611 02/05/2023 06:00 570075 Mr Michael Hobbs, Bury, BL9 9RE 128 192
BTM1612 02/05/2023 07:00 570078 Mrs Gillian Bunce, Sutton Coldfield, B75 6AG 35 52.5
BTM1612 02/05/2023 07:00 569890 Mr Richard Scott, Sutton Coldfield, B74 2AL 35 52.5
BTM1613 02/05/2023 06:00 418719 D Jiggens, Twickenham, TW2 6PU 125 187.5
BTM1613 02/05/2023 06:00 570246 Mrs J Smith, Surbiton, KT6 6QP 125 187.5
BTM1614 02/05/2023 06:00 569539 Mr Charles Docherty, London, SE4 2DX 136 204
BTM1614 02/05/2023 06:00 570036 Mrs Anne Kirby, Dagenham, RM8 2PU 136 204

答案1

得分: 0

使用 Power Query,无法生成"合并单元格"。下面的代码仅将里程和价格金额写入特定作业编号的条目的第一行。然后,您需要手动合并单元格或使用 VBA 程序进行合并。

还有其他选项,但如果您必须要合并单元格,您需要开发一个 VBA 解决方案。

将以下代码粘贴到高级编辑器中,将第二行更改为反映您工作簿中实际表格名称。

阅读代码注释并探索应用步骤以了解算法。新表格是通过将 Tables.FromColumns 方法应用于每个作业编号的子组来创建的。

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Job No.", type text}, {"Date", type datetime}, {"Reference 1", type text}, 
        {"Delivery", type text}, {"Miles", Int64.Type}, {"Price", type number}}, "en-GB"),

//按作业编号分组
//然后拆分为行
//只在第一行中包含价格和里程
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Job No."}, {
        {"New Table", (t)=>Table.FromColumns(
                {t[#"Job No."]}
                 & {t[Date]}
                 & {Text.Split(t[#"Reference 1"{0}], " ")}
                 & {Text.Split(t[#"Delivery"{0}], "#(lf)")}
                 & {t[Miles]}
                 & {t[Price]},
            Table.ColumnNames(t)),
            type table [#"Job No."=nullable text, Date=nullable datetime, Reference 1=nullable text, Delivery=nullable text, Miles=nullable number, Price=nullable number]   
            }}),

//展开分组的表格,除了作业编号
    #"Expanded New Table" = Table.ExpandTableColumn(#"Grouped Rows", "New Table", 
        {"Date", "Reference 1", "Delivery", "Miles", "Price"}),

//填充日期列
    #"Filled Down" = Table.FillDown(#"Expanded New Table",{"Date"})
in
    #"Filled Down"

您的原始数据
我可以分割多个列,但保留一些单元格合并吗?

上述结果
我可以分割多个列,但保留一些单元格合并吗?

英文:

Using Power Query, it is not possible to produce "Merged Cells". The code below only writes the miles and price amounts into the first row of the entries for a particular Job No. You would then have to merge the cells manually or by using a VBA routine.

There are other options, but if you must have merged cells, you will need to develop a VBA solution for that.

Paste the code below into the Advanced Editor, changing the second line to reflect your actual Table Name in your workbook.

Read the code comments and explore the Applied Steps to understand the algorithm. The new table is created by applying the Tables.FromColumns method to each subgroup of Job No.

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Job No.", type text}, {"Date", type datetime}, {"Reference 1", type text}, 
        {"Delivery", type text}, {"Miles", Int64.Type}, {"Price", type number}},"en-GB"),

//Group by Job number
//then split into rows
//Price and miles only on first row
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Job No."}, {
        {"New Table", (t)=>Table.FromColumns(
                {t[#"Job No."]}
                 & {t[Date]}
                 & {Text.Split(t[Reference 1]{0}," ")}
                 & {Text.Split(t[Delivery]{0},"#(lf)")}
                 & {t[Miles]}
                 & {t[Price]},
            Table.ColumnNames(t)),
            type table [#"Job No."=nullable text, Date=nullable datetime, Reference 1=nullable text, Delivery=nullable text, Miles=nullable number, Price=nullable number]   
            }}),

//Expand the grouped tables exceot for Job No.
    #"Expanded New Table" = Table.ExpandTableColumn(#"Grouped Rows", "New Table", 
        {"Date", "Reference 1", "Delivery", "Miles", "Price"}),

//Fill down the date column
    #"Filled Down" = Table.FillDown(#"Expanded New Table",{"Date"})
in
    #"Filled Down"

Your Original Data<br>
我可以分割多个列,但保留一些单元格合并吗?

Results from Above<br>
我可以分割多个列,但保留一些单元格合并吗?

huangapple
  • 本文由 发表于 2023年6月27日 17:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定