根据正确的模式转置数据

Transpose data based on the proper pattern

这就是我希望日期在一切都完成并转置数据时的样子。

数据

2 Witches Winery and Brewing Company
209 Trade Street
Danville, VA 24541-3545
Phone: (434) 549-2739
Type: Taproom
www.2witcheswinebrew.com
View Map

36 Fifty Brewing
120 N Chestnut St
Marion, VA 24354
Type: Taproom
View Map

6 Bears & A Goat Brewing Company, LLC
1140 International Pkwy
Fredericksburg, VA 22406-1126
Phone: 540-356-9056 Ext. 2
Type: Brewpub
www.6bgbrewingco.com
View Map

每个单元格代表一个啤酒厂。我正在尝试转置这个值并将其放入行中。这是问题……并非所有值都位于正确的位置。每个啤酒厂的前 3 行始终相同。当它到达每个啤酒厂的第 4 排时,这就是它变得棘手的地方。并非所有啤酒厂都有 phone,因此转置数据会使所有数据不在正确的位置。类型通常应位于“5”行,但由于没有数字,因此位于第 4 行。大约20%的数据是这样的。谁有什么建议。

抱歉,编辑忘记添加我已经尝试过的内容,但它没有按预期工作。

// Table2
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Helper_Column", each if Text.Contains([Column1],"Phone:") then "1 @1" else null),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Added Custom", {"Helper_Column"}),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Removed Errors", {{"Helper_Column", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Helper_Column"),
    #"Added Custom1" = Table.AddColumn(#"Split Column by Delimiter", "Helper Column 1", each if [Helper_Column] = "@1" then null else [Column1]),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Helper Column 1"}),
    #"Added Custom2" = Table.AddColumn(#"Removed Other Columns", "Helper Column", each if Text.Contains([Helper Column 1],"View Map") then "1 @1" else null),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom2", {{"Helper Column", null}}),
    #"Split Column by Delimiter1" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Errors", {{"Helper Column", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Helper Column"),
    #"Added Custom3" = Table.AddColumn(#"Split Column by Delimiter1", "Helper", each if [Helper Column] = "@1" then null else [Helper Column 1]),
    #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom3",{"Helper"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Other Columns1", "Index", 0, 1, Int64.Type),
    #"Inserted Modulo" = Table.AddColumn(#"Added Index", "Modulo", each Number.Mod([Index], 8), type number),
    #"Integer-Divided Column" = Table.TransformColumns(#"Inserted Modulo", {{"Index", each Number.IntegerDivide(_, 8), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Integer-Divided Column", {{"Modulo", type text}}, "en-IN"), List.Distinct(Table.TransformColumnTypes(#"Integer-Divided Column", {{"Modulo", type text}}, "en-IN")[Modulo]), "Modulo", "Helper")
in
    #"Pivoted Column"

这取决于您的示例的真实性。但下面的代码可能会有所帮助。它适用于您发布的数据。

但是你需要有明确的规则。

我从你的数据和你写的东西中得出了一些,并在代码注释中注明了它们。当然,如果您的实际数据不遵循这些规则,算法将无法工作。如果是这种情况,您将不得不修改规则。

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),

//assuming each group is contiguous lines
// with a blank line inbetween each group
// the below few lines will create a column on which to group
// then remove the "blank line between"
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "group", each if [Column1] = null then [Index] else null, Int64.Type),
    #"Filled Up" = Table.FillUp(#"Added Custom",{"group"}),
    #"Removed Columns" = Table.RemoveColumns(#"Filled Up",{"Index"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Column1] <> null)),

//Group, then extract the data
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"group"}, {

//Line one is always the brewery name
        {"Brewery Name", each [Column1]{0}, type text},

//Lines 2 and 3 are always the address
        {"Address Part 1", each [Column1]{1}, type text},
        {"Address Part 2", each [Column1]{2}, type text},

//Phone number starts with "Phone:"
        {"Phone", each List.Accumulate([Column1], "", (state, current)=> 
            if Text.StartsWith(current,"Phone:") then state & current else state), type text},

//Type starts with "Type:"
        {"Type", each List.Accumulate([Column1], "", (state, current)=> 
            if Text.StartsWith(current,"Type:") then state & current else state), type text},

//Other 1 starts with "www."
        {"Other 1", each List.Accumulate([Column1], "", (state, current)=> 
            if Text.StartsWith(current,"www.") then state & current else state), type text},

//Other 2 is the last line
        {"Other 2", each List.Last([Column1]), type text}
        }),

//Remove the grouper column
    #"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"group"})
in
    #"Removed Columns1"

数据

结果