简体   繁体   English

在Power BI中合并表格

[英]Combine tables in Power BI

I'm trying to combine two tables in Power BI. 我正在尝试在Power BI中合并两个表。

I have the following two tables: 我有以下两个表:

 <table border="1"> <tr><th>Id</th><th>Category</th></tr> <tr><td>1</td><td>A</td></tr> <tr><td>1</td><td>B</td></tr> <tr><td>2</td><td>A</td></tr> <tr><td>3</td><td>B</td></tr> <tr><td>3</td><td>C</td></tr> <tr><td>4</td><td>A</td></tr> </table> <br> <br> <table border="1"> <tr><th>Id</th><th>Value</th></tr> <tr><td>1</td><td>10</td></tr> <tr><td>1</td><td>20</td></tr> <tr><td>3</td><td>10</td></tr> <tr><td>3</td><td>30</td></tr> <tr><td>4</td><td>20</td></tr> <tr><td>4</td><td>30</td></tr> <tr><td>5</td><td>10</td></tr> </table> 

I need to combine them as below: 我需要将它们合并如下:

 <table border="1"> <tr><th>Id</th><th>Category</th><th>Value</th></tr> <tr><td>1</td><td>A</td><th>10</th></tr> <tr><td>1</td><td>A</td><th>20</th></tr> <tr><td>1</td><td>B</td><th>10</th></tr> <tr><td>1</td><td>B</td><th>20</th></tr> <tr><td>2</td><td>A</td><th>BLANK</th></tr> <tr><td>3</td><td>B</td><th>10</th></tr> <tr><td>3</td><td>B</td><th>30</th></tr> <tr><td>3</td><td>C</td><th>10</th></tr> <tr><td>3</td><td>C</td><th>30</th></tr> <tr><td>4</td><td>A</td><th>20</th></tr> <tr><td>4</td><td>A</td><th>30</th></tr> <tr><td>5</td><td>BLANK</td><th>10</th></tr> </table> 

How can this be achieved using the DAX in Power BI ? 如何在Power BI中使用DAX来实现?

First, you will need a distinct list of all IDs between your two tables. 首先,您将需要两个表之间所有ID的唯一列表。 To get this in PowerBI, click on 'Modeling' -> 'New Table' and enter this formula. 要在PowerBI中获得此功能,请单击“建模”->“新建表”,然后输入此公式。

IDs = DISTINCT(UNION(
    SELECTCOLUMNS(Categories, "ID", Categories[Id]), 
    SELECTCOLUMNS('Values', "ID", 'Values'[Id]))
)

身份证表

This table will help create a many-to-many relationship between your category table and value table. 该表将帮助您在类别表和值表之间建立多对多关系。

关系

With that relationship in place, you can create another new table with this formula to get you results. 有了这种关系,您就可以使用此公式创建另一个新表来获得结果。

Results = SELECTCOLUMNS(NATURALLEFTOUTERJOIN(NATURALLEFTOUTERJOIN(IDs, Categories), 'Values'),
    "ID", IDs[ID], 
    "Category", Categories[Category], 
    "Value", 'Values'[Value]
)

结果

(a Power Query, not DAX solution) (电源查询,不是DAX解决方案)


Add a blank query: 添加一个空白查询:

= Table.NestedJoin(Table1,{"Id"},Table2,{"Id"},"Table2",JoinKind.FullOuter)

Add a transformation step: 添加一个转换步骤:

= Table.ExpandTableColumn(Source, "Table2", {"Id", "Value"}, {"Table2.Id", "Value"})

Add step: 添加步骤:

= Table.AddColumn(#"Expanded Table2", "NewId", each (if [Id] = null then [Table2.Id] else [Id]))

Delete columns Id and Table2.Id . 删除IdTable2.Id列。


Alternatively, a GUI approach via PowerBI Desktop: 或者,通过PowerBI Desktop的GUI方法:

  • Open query editor -> Home tab 打开查询编辑器->主页选项卡
  • Merge queries -> As New 合并查询->如新
  • Select both tables, highlight Id column, Full outer join 选择两个表,突出显示“ Id”列,“完全外部联接”
  • Expand the last column. 展开最后一列。
  • Rename columns appropriately. 适当地重命名列。
  • Coalesce Id columns using the method of your choice. 使用您选择的方法合并ID列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM