【问题标题】:dcast / melt / reshape a table which has calculated columns in power BI using DAXdcast / 融化 / 重塑一个表格,该表格在 Power BI 中使用 DAX 计算列
【发布时间】:2019-09-03 15:01:35
【问题描述】:

我有一个有 5 列的表格

Store ID | Year | Sales Group 1 | Sales Group 2 | Sales Group 3

所有带有Sales Group 的字段都是使用 DAX 计算的。我想创建一个包含Store IDYearSales GroupSales Value 的新表。 所以基本上我会为每个商店 ID 和年份提供 3 行数据,每行包含不同销售组的销售值

我希望将 DAX 查询从表 1 转换为表 2

表 1:

表 2:

【问题讨论】:

  • 那你的问题是什么?
  • @AmiraBedhiafi 如何使用 DAX 创建该格式的新表,我知道它可以使用 pwoer 查询完成,但我的字段是计算字段。

标签: powerbi dax data-analysis


【解决方案1】:

在 Power Query M 中执行此操作可能会更好,它具有内置的 unpivot 转换,但您可以使用 DAX:

UnpivotedTable =
// GROUPBY gives you unique combinations of the columns referenced
VAR StoreYears =
  GROUPBY (
    'Table',
    'Table'[Store ID],
    'Table'[Year]
  )
RETURN
// UNION does what it says on the label, unions multiple tables
UNION (
  // ADDCOLUMNS is also self-descriptive - takes a table, adds columns to it
  ADDCOLUMNS (
    StoreYears,
    // After the table arg, ADDCOLUMNS takes pairs of quoted column name and
    // DAX expression to evaluate for the value in that column. We create
    // two columns, the group and the sum of the source column for that group.
    "Sales Group", 1,
    "Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 1] ) )
  ),
  // repeat the pattern above per group
  ADDCOLUMNS (
    StoreYears,
    "Sales Group", 2,
    "Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 2] ) )
  ),
  ADDCOLUMNS (
    StoreYears,
    "Sales Group", 3,
    "Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 3] ) )
  )
)

【讨论】:

    【解决方案2】:

    执行此操作的最佳方法是在查询编辑器(性能)中。加载您的数据,转到查询编辑器并选择表格。

    选择销售组的3列,转到Transform -> unpivot

    重命名列标题

    最终结果:

    【讨论】:

    • 我需要对使用 DAX 创建的列执行该函数,它们本质上是动态的,并且是通过对数据模型中的多个表执行的操作创建的
    • 我会考虑在 M 查询中也做这部分,当然我不知道你的数据集有多大,所以如果你对 DAX 解决方案感到满意,那很好。
    • 我同意 Aldert 的观点,即在 M 中执行此操作的更好位置。DAX 计算的列不是动态的。它们在模型刷新时计算一次,然后是静态的,直到下一次刷新。如果您的意思是刷新模型时值会发生变化,那么任何此类计算都可以在 M 中轻松完成。
    猜你喜欢
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多