Power-Query和Power BI如何创建度量并将其联合回原始数据集

问题描述 投票:0回答:1

想知道我们如何基于数据集创建度量,然后将其与原始数据集联合起来?

可能会更容易在视觉上查看问题,所以我创建了下面的图片。

enter image description here

附:不管以前的OR相关功能如何,我都必须以这种方式做到这一点

谢谢

谢谢

powerbi powerquery m
1个回答
0
投票

您可以通过在该类别值的当前行日期之前的最后一个日期查找卷来添加自定义列来计算比较量。删除Volume列,将CalcType从“Individual”重命名为“Comparison”,然后将该表附加到原始表中。

这是M代码的样子。

let
    Source = <Insert Source Here>,
    #"Changed Type" = Table.TransformColumnTypes(Source,
        {{"Date", type date}, {"Category", type text},
         {"CalcType", type text}, {"Volume", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Comparison", each
        try Table.Max(Table.SelectRows(#"Changed Type",
            (C) => C[Date] < [Date] and C[Category] = [Category]), "Date")[Volume]
        otherwise null, Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Volume"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Comparison","Volume"}}),
    #"Replaced Value" = Table.ReplaceValue(#"Renamed Columns",
        "Individual","Comparison",Replacer.ReplaceText,{"CalcType"}),
    #"Filtered Rows" = Table.SelectRows(#"Replaced Value", each ([Volume] <> null)),
    #"Appended Query" = Table.Combine({#"Changed Type", #"Filtered Rows"})
in
    #"Appended Query"
© www.soinside.com 2019 - 2024. All rights reserved.