2016-04-29 106 views
1

此其他Power Query question and answer提供了一種解決方案,可根據列字符計數寬度將字符分隔文本文件拆分爲列。Power Query - 按可變字段長度拆分列 - 佔空值

但它沒有考慮到空值。遇到空值時,會在右邊的某一列出現錯誤。我不能完全說出發生了什麼事。該錯誤是

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位和電源查詢版本:2.29.4217.1861

如何修改此以計及空值?

回答

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)