本帖最后由 shaoqi0 于 2022-2-17 00:12 编辑
公司先前的凭证资料,需要重新整理,总共24万行多一点,通过POWER QUERY初步整理一下,查询结果可以生成预览,但是加载至表时,就非常慢。
查询显示窗口,"已加载*行",差不多每隔10秒才多增加1行,这样算下来完全加载成功需要1个月时间,期间还能关机。
我初识Power query不久,不知如何改进?
代码如下:
let
源 = Excel.Workbook(File.Contents(路径[路径]{0}), null, true),
导航 = 源{[Item="会计分录序时簿",Kind="Sheet"]}[Data],
删除多余列 = Table.RemoveColumns(导航,{"Column1", "Column2", "Column3", "Column10", "Column11", "Column12", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23", "Column26", "Column27", "Column28", "Column29", "Column30", "Column31", "Column32"}),
会计期间 = Table.TransformColumns(删除多余列,{"Column5", each _ & "期", type text}),
提升标题 = Table.PromoteHeaders(会计期间),
更改类型 = Table.TransformColumnTypes(提升标题,{ {"日期", type date}, {"会计期间期", type text}, {"凭证字号", type text}, {"摘要", type text}, {"科目代码", type text}, {"科目名称", type text}, {"借方", type number}, {"贷方", type number}, {"数量", type number}, {"单价", type number}}),
删除底行 = Table.RemoveLastN(更改类型,1),
借方金额 = Table.AddColumn(删除底行, "借方金额", each if [借方] > 0 then [借方] else if [贷方] < 0 then -[贷方] else 0),
贷方金额 = Table.AddColumn(借方金额, "贷方金额", each if [贷方] > 0 then [贷方] else if [借方] < 0 then -[借方] else 0),
本期金额 = Table.AddColumn(贷方金额, "本期金额", each if [贷方金额] > 0 then Number.Round([贷方金额],2,0) else if [借方金额] > 0 then Number.Round([借方金额],2,0) else 0),
删除借贷 = Table.RemoveColumns(本期金额,{"借方", "贷方"}),
向下填充 = Table.FillDown(删除借贷,{"日期", "会计期间期", "凭证字号","摘要"}),
合并的列 = Table.AddColumn(向下填充, "合并", each Text.From([日期])&"◆"&[会计期间期]&"◆"&[凭证字号]),
收入数量 = Table.AddColumn(合并的列, "材料入数", each if [数量]=0 and [单价]=0 then 0 else if [借方金额]>0 then Number.Abs([数量]) else 0, type number),
发出数量 = Table.AddColumn(收入数量, "材料出数", each if [数量]=0 and [单价]=0 then 0 else if [贷方金额]>0 then Number.Abs([数量]) else 0,type number),
合并1 = Table.AddColumn(发出数量,"合并1",each [合并]&"☆"&Text.Start([摘要],5)&"✦"&Text.From([本期金额])),
索引 = Table.AddIndexColumn(合并1,"索引",1,1,Int64.Type),
次数 = Table.AddColumn(索引,"次数",each Table.RowCount(Table.SelectRows(Table.FirstN(索引,_[索引]),(X)=>X[合并1]=[合并1]))),
判 = Table.AddColumn(次数, "判", each try if Number.IsEven(_[次数]) then [索引] else Table.FirstN(Table.SelectRows(Table.Skip(次数,_[索引]),(X)=>X[合并1]=[合并1]),1)[索引]{0} otherwise null),
固化 = Table.Buffer(判)
in
固化
|