0

我有一个看起来像这样的文件:

AA,DATA,DATA,DATA
BB,DATA,DATA,DATA,DATA
ZZ,DATA,DATA,DATA,DATA,DATA,DATA
ZZ,DATA,DATA,DATA,DATA,DATA
CC,DATA,DATA
ZZ,DATA,DATA,DATA
ZZ,DATA,DATA,DATA
ZZ,DATA,DATA,DATA,DATA

如您所见,它非常不友好。ZZ 起始行表示它们与之前的记录相关;因此在我的示例中,BB 分为 3 行,CC 分为 4 行。我想在 Power Query (Excel 2013) 中实现的是获得一个只包含 AA、BB 和 CC 记录的表。ZZ 将连接到它们各自的 BB 和 CC 记录。ZZ 记录的数量可能会有所不同。
我是 Power Query 的新手,我想我开始处理的问题太多了;)任何关于我应该如何构建表和/或列表来帮助我的具体想法将不胜感激。

谢谢你。

问候,马丁

4

2 回答 2

0

我很肯定你不能在 PowerQuery 中做到这一点。您可以使用 VBA 获取正确的数据,然后将其放入 PowerQuery。这是一些可以做到这一点的代码。

Sub NormalizeData()

    Dim sFile As String, lFile As Long
    Dim vaLines As Variant
    Dim vaData As Variant
    Dim i As Long, j As Long
    Dim dc As Scripting.Dictionary
    Dim lStart As Long

    'Open file and read in all the data
    sFile = "K:\testfile.txt"
    lFile = FreeFile

    Open sFile For Input As lFile
    vaLines = Split(Input$(LOF(lFile), lFile), vbNewLine)
    Close lFile

    For i = LBound(vaLines) To UBound(vaLines)

        vaData = Split(vaLines(i), ",")

        If vaData(0) <> "ZZ" Then
            'Output existing dictionary if it exists
            If Not dc Is Nothing Then Debug.Print Join(dc.Items, ",")

            'Create a new dictionary and start at the first column (0)
            Set dc = New Scripting.Dictionary
            lStart = 0
        Else
            'starts with ZZ so skip the first column
            lStart = 1
        End If

        'Add the data to the dictionary
        For j = lStart To UBound(vaData)
            dc.Add dc.Count + 1, vaData(j)
        Next j

    Next i

    'Output the last dictionary
    If Not dc Is Nothing Then Debug.Print Join(dc.Items, ",")

End Sub
于 2014-08-29T15:04:14.667 回答
0

我知道这是一个老问题,当时我的解决方案可能行不通。但是当我在 PowerBI Desktop 中尝试时,这个 PQFL 效果很好。

let
  RecordKey = (t,n) => Table.Range(t,n,1){0}[Key],

  NeedsMerged = (t,n) =>
    try
      if RecordKey(t,n+1) = "ZZ" then true else false
    otherwise false,

  MergeWithNext = (t,n) =>
    if NeedsMerged(t,n) then
      let
        thisRow = Table.Range(t, n, 1),
        nextRow = Table.Range(t, n+1, 1)
      in  
        {
        true,  
        Table.Range(t, 0, n) &
        Table.TransformColumns( thisRow, {"Data", (_) => _ & nextRow{0}[Data] } ) &
        Table.Range(t, n + 2)
        }
    else {false,t},

  DoNeededMerging = (t,n) =>
    if Table.RowCount(t) < n then t
    else
      let 
        res = MergeWithNext(t,n),
        resp = res{0},
        tbl = res{1}
      in
        if resp then
          @DoNeededMerging(tbl,n)
        else
          @DoNeededMerging(tbl,n+1),

  Filename = "C:\the_data_file.csv",
  DataTable = Table.FromList(Lines.FromText(Text.FromBinary(File.Contents(Filename))), Splitter.SplitTextByDelimiter(","), {"Key","Data"}, null, ExtraValues.List),
  DataTable2 = DoNeededMerging( DataTable , 0)

in
  DataTable2
于 2016-06-11T22:58:19.087 回答