[英]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
. 删除Id
和Table2.Id
列。
Alternatively, a GUI approach via PowerBI Desktop: 或者,通过PowerBI Desktop的GUI方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.