|
本帖最后由 ThxAlot 于 2024-4-6 19:09 编辑
你这个文不对题哦,标题是“后进先出”,要求里又写的是“先进先出”;期待的结果虽然是“后进先出”,但看样子是没把“[清单申报日期] <= TODAY()”说明白。
PQ的可读性肯定是不好的,我是更愿意用DAX搞定,放在这里供参考:
- REQ =
- CALCULATE(
- SUM( Table2[数量] ),
- TREATAS(
- SUMMARIZE( Table1, Table1[企业原始料号], Table1[备案序号], Table1[原产国] ),
- Table2[企业原始料号],
- Table2[备案序号],
- Table2[原产国]
- )
- )
复制代码- LIFO =
- VAR __req = [REQ]
- VAR __rev_rt =
- IF(
- MAX( Table1[清单申报日期] ) <= TODAY(),
- CALCULATE(
- SUM( Table1[Subtotal] ),
- Table1[清单申报日期] <= TODAY(),
- WINDOW(
- 1, REL,
- -1, ABS,
- ALLSELECTED( Table1[企业原始料号], Table1[备案序号], Table1[原产国], Table1[清单申报日期] ),
- ORDERBY( Table1[清单申报日期] ),
- PARTITIONBY( Table1[企业原始料号], Table1[备案序号], Table1[原产国] )
- ),
- REMOVEFILTERS()
- )
- )
- VAR __current = SUM( Table1[Subtotal] )
- RETURN
- IF(
- MAX( Table1[清单申报日期] ) <= TODAY() && __rev_rt < __req,
- IF( __rev_rt + __current < __req, __current, __req - __rev_rt )
- )
复制代码
- let
- Source = let today = Date.From(DateTime.LocalNow()) in Table.NestedJoin(Table2, {"企业原始料号","备案序号","原产国"}, Table.Sort(Table.SelectRows(Table1, each [清单申报日期] <= today), {{"清单申报日期", Order.Descending}}), {"企业原始料号","备案序号","原产国"}, "joined", JoinKind.Inner),
- #"Transformed joined" = Table.ReplaceValue(
- Source,
- each [数量],
- null,
- (x,y,z) =>
- let
- rows = Table.ToRecords(x),
- tb = Table.FromRecords(List.Accumulate(rows, {}, (s,c) => let prev = List.Sum(Table.FromRecords(s)[Subtotal]? ?? {0}) in if prev < y then if prev + c[Subtotal] >= y then s & {Record.TransformFields(c, {"Subtotal", each y - prev})} else s & {c} else s))
- in tb,
- {"joined"}
- ),
- #"Expanded joined" = Table.ExpandTableColumn(#"Transformed joined", "joined", {"清单编号", "清单申报日期", "Subtotal"}, {"清单编号", "清单申报日期", "Subtotal"})
- in
- #"Expanded joined"
复制代码
|
|