【问题标题】:Power Bi - two column datesPower Bi - 两列日期
【发布时间】:2021-10-22 17:01:11
【问题描述】:

我想要关于 power bi 的帮助。我在行中有几个项目,在不同的列中有 12 个月的预算,还有其他 12 个月的实际费用。

我已将实际费用和预算放在两个不同的列中(而不是 24 --> 2 次 12 个月)。问题是我最终得到了两个不同的日期列,一个用于实际销售,另一个用于预算,我被卡住了。如何解决这个问题?有更好的办法吗?

谢谢

原始数据

期望的输出

.................................................. …………

更新:排序的最新问题(枢轴函数后的索引) enter image description here

【问题讨论】:

标签: excel powerbi powerquery data-modeling powerbi-desktop


【解决方案1】:

有了足够的数据,现在可以更轻松地了解您正在处理什么以及您想要什么。

  • 您似乎需要重复“项目”和“经理”列。所以选择它们,然后取消透视其他列
  • 然后将属性列拆分为月份和我所说的“ValueType”列(预算、实际、最新)
  • 在 ValueType 列上没有聚合的数据透视
  • 然后进行一些清理,对列进行排序和重新排列。

探索应用步骤并阅读 M-Code 中的 cmets 以更好地理解算法
编辑: 编辑原始代码以制作列更具动态性,并且还允许更多“类型”的值(例如:预算、实际、最后、其他)

let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],

//Get list of monthcolumns for setting data type and Count
    //if columns will only be Project, Manager, and then various Date columns, you can use the simpler function
    //if not, you'll need a different Selection function
   monthCols = List.Select(Table.ColumnNames(Source), each
        not (_ = "Project" or _="Manager")),
    typer = {{"Project", Int64.Type},{"Manager", Text.Type}} & 
                      List.Transform(monthCols, each {_, Int64.Type}),

    //How many value types and months
    v = List.Transform(monthCols,each Text.Split(_," "){0}),
    months = List.Distinct(v,Comparer.OrdinalIgnoreCase),
    numMonths = List.Count(months),

    vt=List.Transform(monthCols,each Text.Split(_," "){1}),
    valueTypes= List.Distinct(vt,Comparer.OrdinalIgnoreCase),
  
    #"Changed Type" = Table.TransformColumnTypes(Source,typer),

//Unpivot all except Project and Manager columns
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project", "Manager"}, "Attribute", "Value"),

//Split the Attribute (budget date/type) column to separate the Month and the Type
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", 
        Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Month", "ValueType"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Month", type text}, {"ValueType", type text}}),
    trimAndProper= Table.TransformColumns(#"Changed Type1",{
        {"Month", each Text.Trim(Text.Proper(_))},
        {"ValueType", each Text.Trim(Text.Proper(_))}}),

//Add Index of Month Number for sorting later on
 monthNumber = Table.AddColumn(trimAndProper, "monthNumber", 
    each Date.Month(Date.FromText([Month] & " 2021"))),

//Pivot on ValueType
    #"Pivoted Column" = Table.Pivot(monthNumber, List.Distinct(monthNumber[ValueType]), "ValueType", "Value"),

//Add another Index - Integer/Division for sorting
    #"Added Index1" = Table.AddIndexColumn(#"Pivoted Column", "Index.1", 0, 1, Int64.Type),
    #"Inserted Integer-Division" = Table.AddColumn(#"Added Index1", "Integer-Division", 
        each Number.IntegerDivide([Index.1], numMonths), Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index.1"}),

//Sort, reorder and rename
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Integer-Division", Order.Ascending}, {"monthNumber", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"monthNumber", "Integer-Division"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"Project", "Month"} & valueTypes & {"Manager"})
in
    #"Reordered Columns"

原始数据

结果

【讨论】:

  • 谢谢@Ron 我已经尝试过你的方法(第一次没有排序),但我最终得到了两次,比如 9 月和其他月份:问题月份(参见我的另一个答案中的图片,因为此处无法附加)然后我尝试按照您的额外排序步骤执行一些步骤,并且在枢轴之前似乎还可以,然后与索引编号相关的月份似乎不再合适。一开始我发现这个方法有点复杂,所以我在需要的地方手动插入了 numMonths 而不是直接插入值 12。希望你能帮我解决这个问题,谢谢
  • @A_powerBi 从图表输出中可以看出,您的样本数据并不能真正代表您的实际数据或所需的表(在图表之前)输出,所以我对您遇到问题并不感到惊讶。如果没有代表性数据和代表性表格输出,我将无能为力。
  • @A_powerBi 根据您的图表提出的一些问题:您的原始数据是否在列标题中包含年份?如果是这样,它到底是什么样子的?您希望按月/年分组的结果还是将所有月份汇总在一起?我提出的解决方案假设您的数据看起来像您提供的那样,原始列标题中只有月份名称。两者之间有很多区别。
  • 嗨@Ron 感谢您的支持。我已经解决了清理月份格式的第一个问题,所以现在如果我绘制它们,我只会看到那些应该在那里,而不是重复。在数据方面,我的数据集与作为示例共享的数据集之间的唯一区别是,在我的真实数据集中,我有 12 个月的时间,从 2021 年 6 月到 2022 年 5 月。现在剩下的唯一问题是排序,在我看来,索引在枢轴之前运行良好,但在枢轴之后失去与月份的对应关系(我将图片附在我的主帖中)。再次感谢您!
  • Ron 回答您的最后一个问题,我的数据采用以下格式的月-年,从 2021 年 6 月到 2022 年 5 月。就像我们对示例数据所做的那样,可以将它们单独放在月份列中。我只是想让它们从 2021 年 6 月到 2022 年 5 月按渐进顺序排列。谢谢
猜你喜欢
  • 1970-01-01
  • 2023-01-12
  • 2019-06-19
  • 2022-08-10
  • 2022-01-21
  • 1970-01-01
  • 2020-05-25
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多