【问题标题】:Power Query - split column by variable field lengths - account for null valuesPower Query - 按可变字段长度拆分列 - 考虑空值
【发布时间】:2020-01-07 23:32:07
【问题描述】:

另一个 Power Query question and answer 提供了一种解决方案,可根据列字符计数宽度将字符分隔的文本文件拆分为列。

但它不考虑空值。当遇到空值时,它会在右侧的一列中给出错误。我不能确切地说到底发生了什么。错误是

An error occurred in the ‘SplitText’ query. Expression.Error: The 'count' argument is out of range.

分割函数的代码是:

let
    SplitText = (text, lengths) => 
    let
        LengthsCount = List.Count(lengths),
        // Keep track of the index in the lengths list and the position in the text to take the next characters from. Use this information to get the next segment and put it into a list.
        Split = List.Generate(() => {0, 0}, each _{0} < LengthsCount, each {_{0} + 1, _{1} + lengths{_{0}}}, each Text.Range(text, _{1}, lengths{_{0}}))
    in
        Split,
    // Convert the list to a record to 
    ListToRecord = (text, lengths) => 
    let
        List = SplitText(text, lengths),
        Record = Record.FromList(List, List.Transform({1 .. List.Count(List)}, each Number.ToText(_)))
    in
        Record
in
    ListToRecord

然后,在您的表格中,添加一个使用此公式的自定义列:

each SplitText([Column1], {4, 2, 5, 3})

我使用的是 Excel 2010 64 位和 Power Query 版本:2.29.4217.1861

如何修改它以解决空值问题?

【问题讨论】:

    标签: excel-2010 powerquery


    【解决方案1】:
    Split = List.Generate(() => {0, 0}, each _{0} < LengthsCount, each {_{0} + 1, _{1} + lengths{_{0}}}, each try Text.Range(text, _{1}, lengths{_{0}}) otherwise null)
    

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 2012-06-01
      相关资源
      最近更新 更多