【问题标题】:Power BI :: How to join 2 tables with 1 column to multiple columnsPower BI :: 如何将具有 1 列的 2 个表连接到多个列
【发布时间】:2022-12-01 10:20:12
【问题描述】:

目标:我想预测 Azure 预留实例是否是我们的正确选择。

怎么做:

  1. 我已经通过这个Python script 下载了整个 Azure Price REST API。

  2. 我已将该 CSV 导入到 Azure SQL 数据库中

  3. 感谢Azure Cost Management connector in Power BI Desktop,我想将每个预留实例与我们在 Azure 上拥有的资源进行比较

    问题:在一个完美的世界中,我希望看到像这样列出的所有资源:

    unitPrice 1 Year Reservation 3 Years Reservation
    1.2671 6528.3905 12524.2148

    但我们并不生活在一个完美的世界中,数据是这样组织的:

    unitPrice meterId PK productName skuName location serviceName unitOfMeasure type armSkuName reservationTerm
    6528.3905 003e1713-c374-4003-9a73-27b3ccc80c38 Virtual Machines Ev3 Series - E16 v3 - EU West Virtual Machines Ev3 Series E16 v3 EU West Virtual Machines 1 Hour Reservation Standard_E16_v3 1 Year
    1.2671 003e1713-c374-4003-9a73-27b3ccc80c38 Virtual Machines Ev3 Series - E16 v3 - EU West Virtual Machines Ev3 Series E16 v3 EU West Virtual Machines 1 Hour Consumption Standard_E16_v3 NULL
    12524.2148 003e1713-c374-4003-9a73-27b3ccc80c38 Virtual Machines Ev3 Series - E16 v3 - EU West Virtual Machines Ev3 Series E16 v3 EU West Virtual Machines 1 Hour Reservation Standard_E16_v3 3 Years

    所以我根据产品名称、skuName 和位置创建了一个主键。

    我正在与 Microsoft 通电话,他们确认 meterId 不是唯一标识符。

    问题:现在我有了一个唯一标识符,我可以旋转1年3年将所有内容放在同一行。

    tierMinimumUnits PK armRegionName location meterId meterName productId availabilityId productName skuName serviceName serviceId serviceFamily unitOfMeasure isPrimaryMeterRegion armSkuName effectiveEndDate RI_unitPrice RI_DevTestConsumption RI_1Year RI_3Years
    0.0 Virtual Machines Ev3 Series - E16 v3 - EU West westeurope EU West 003e1713-c374-4003-9a73-27b3ccc80c38 E16 v3/E16s v3 DZH318Z0BQ4L NULL Virtual Machines Ev3 Series E16 v3 Virtual Machines DZH313Z7MMC8 Compute 1 Hour True Standard_E16_v3 NULL 1.2671 NULL 0.744739961213781 0.476242102060993

    但我问自己,我是否做错了。

    如果数据在 3 个单独的行上,也许有一种方法可以通过 Power Query 将数据保存在 3 个单独的行上,并写一条规则说

    “从具有唯一标识符的 3 行中选取 1 年和 3 年”

    什么是最好的方法?

    可应要求提供数据集。

【问题讨论】:

  • 可能只有我一个人,但我无法想象之前的数据是什么样的,以及您试图获得的之后的数据。也许你可以发布之前/之后的样本
  • 是的,让我这样做

标签: powerbi dax powerquery powerbi-desktop cost-management


【解决方案1】:

这似乎对我有用。

对于此解决方案,我从 Excel 中的表格开始。您可以更改适合您的来源。我使用您的数据在 Excel 中设置了我的表格,使其看起来像您的表格示例,但随后我还添加了一些额外的虚拟行。

您可能会注意到我移动了 reservationTerms 的出现顺序……而不是为它们重复相同的模式。我这样做是因为我不确定你的模式是否有模式,我稍后使用 reservationTerms 来命名列。通过四处移动它们,我确保我的解决方案不依赖于它们以任何特定顺序排列——它应该适应任何出现顺序。

我将 excel 表作为 Table1 导入 Power Query。然后我按 productName、skuName 和位置分组,选择所有行。之后,我在每个组的行中嵌入的表格中做了一些转换工作。然后我添加了一列以从每行的每个嵌入表中提取带有单价的记录。然后我扩展了记录。最后,我删除了包含通过分组创建的表的列。结果看起来像这样。

这是M代码。

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"unitPrice", type number}, {"meterId", type text}, {"PK", type text}, {"productName", type text}, {"skuName", type text}, {"location", type text}, {"serviceName", type text}, {"unitOfMeasure", type text}, {"type", type text}, {"armSkuName", type text}, {"reservationTerm", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"productName", "skuName", "location"}, {{"AllData", each _, type table [unitPrice=nullable number, meterId=nullable text, PK=nullable text, productName=nullable text, skuName=nullable text, location=nullable text, serviceName=nullable text, unitOfMeasure=nullable text, type=nullable text, armSkuName=nullable text, reservationTerm=nullable text]}}),
    #"Demoted Headers" = Table.TransformColumns(#"Grouped Rows", {"AllData", each Table.DemoteHeaders(_)}),
    Custom1 = Table.TransformColumns(#"Demoted Headers", {"AllData", each Table.Transpose(_)}),
    Custom4 = Table.TransformColumns(Custom1, {"AllData", each Table.RenameColumns(_,List.Zip({Table.ColumnNames(_),
Record.ToList(Table.SelectRows(_, each [Column1] = "reservationTerm"){0})}))}),
    #"Added Custom" = Table.AddColumn(Custom4, "Custom", each [AllData]{[reservationTerm="unitPrice"]}),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"1 Year", "NULL", "3 Years"}, {"1 Year", "NULL", "3 Years"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"AllData"})
in
    #"Removed Columns"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多