将一个 table 的列添加到另一个
Add column from one table to another
我有以下表格:
Table答:
___________________
| ID | NUMBER |
|__________|________|
| 10000378 | 1 |
| | |
Table乙:
_________________________________________________
| ... | ID | Column 1 | Column 2 |
|____________|__________|____________|____________|
| ... | 10000378 | 7 | 2 |
| | | | |
Table A
和 Table B
共享相同的 ID
值。
倒不是很重要,Table A
中的ID
是关键,在Table B
中可以找到。
Table A
的 NUMBER
列告诉我要阅读哪个 Table B
列。
例如:
IF
[Table A].[NUMBER] = 1
THEN
put the value of [Table B].[Column 1] into [Table B].[My Column]
... and so on
作为视觉示例(我想要的结果):
_______________________________________________________________
| ... | ID | Column 1 | Column 2 | My Column |
|____________|__________|____________|____________|_____________|
| ... | 10000378 | 7 | 2 | 7 |
| | | | | |
My Column
是我想要的结果(来自 Column 1
- 由 Table A
的 NUMBER
值指定为 10000378
).
到目前为止我尝试了什么
我已经在 Table A
和 Table B
之间建立了 1-to-1
关系(在 ID
列`)。
然后我进入 Power Query Editor
尝试添加一个 Conditional Column
:
但这不允许我以任何方式引用 Table A
。
所以我决定尝试 Append Queries as New
选项:
选择 Two tables
单选按钮,将 Primary Table
设置为 Table B
,将 Table to append to the primary table
设置为 Table B
。
基于创建以下结构的想法,我可以从中获得我想要的结果(但添加了 NUMBER
列,我并不想要但可以接受):
_____________________________________________________________________________
| ... | ID | Column 1 | Column 2 | NUMBER | My Column |
|____________|__________|____________|____________|_____________|_____________|
| ... | 10000378 | 7 | 2 | 1 | 7 |
| | | | | | |
执行追加复制了 Table B
的所有内容并插入了 NUMBER
列,这很好,但是 NUMBER
列中的所有值现在都是 null
.
我不明白为什么会这样。
我怎样才能达到我想要的结果?
为此,首先 合并 Table B
与 Table A
并展开您要添加到 table 的列( Column 1
和 Column 2
).
一旦你有了,然后写你的自定义列,My Column
:
if [NUMBER] = 1 then [Column 1]
else if [NUMBER] = 2 then [Column 2]
else <whatever you want to return if not 1 or 2>
当您有额外的数据行要添加到现有查询时,使用附加查询(即 union/union 全部在 SQL 中)。在 Power BI 中连接表称为 "merging":
您可以在 Combine queries section of Tutorial: Shape and combine data in Power BI Desktop 文章中找到更多详细信息。
请注意,如果您在模型中的表之间定义了正确的关系(也在上面的文章中进行了描述)。
我有以下表格:
Table答:
___________________
| ID | NUMBER |
|__________|________|
| 10000378 | 1 |
| | |
Table乙:
_________________________________________________
| ... | ID | Column 1 | Column 2 |
|____________|__________|____________|____________|
| ... | 10000378 | 7 | 2 |
| | | | |
Table A
和 Table B
共享相同的 ID
值。
倒不是很重要,Table A
中的ID
是关键,在Table B
中可以找到。
Table A
的 NUMBER
列告诉我要阅读哪个 Table B
列。
例如:
IF
[Table A].[NUMBER] = 1
THEN
put the value of [Table B].[Column 1] into [Table B].[My Column]
... and so on
作为视觉示例(我想要的结果):
_______________________________________________________________
| ... | ID | Column 1 | Column 2 | My Column |
|____________|__________|____________|____________|_____________|
| ... | 10000378 | 7 | 2 | 7 |
| | | | | |
My Column
是我想要的结果(来自 Column 1
- 由 Table A
的 NUMBER
值指定为 10000378
).
到目前为止我尝试了什么
我已经在 Table A
和 Table B
之间建立了 1-to-1
关系(在 ID
列`)。
然后我进入 Power Query Editor
尝试添加一个 Conditional Column
:
但这不允许我以任何方式引用 Table A
。
所以我决定尝试 Append Queries as New
选项:
选择 Two tables
单选按钮,将 Primary Table
设置为 Table B
,将 Table to append to the primary table
设置为 Table B
。
基于创建以下结构的想法,我可以从中获得我想要的结果(但添加了 NUMBER
列,我并不想要但可以接受):
_____________________________________________________________________________
| ... | ID | Column 1 | Column 2 | NUMBER | My Column |
|____________|__________|____________|____________|_____________|_____________|
| ... | 10000378 | 7 | 2 | 1 | 7 |
| | | | | | |
执行追加复制了 Table B
的所有内容并插入了 NUMBER
列,这很好,但是 NUMBER
列中的所有值现在都是 null
.
我不明白为什么会这样。
我怎样才能达到我想要的结果?
为此,首先 合并 Table B
与 Table A
并展开您要添加到 table 的列( Column 1
和 Column 2
).
一旦你有了,然后写你的自定义列,My Column
:
if [NUMBER] = 1 then [Column 1]
else if [NUMBER] = 2 then [Column 2]
else <whatever you want to return if not 1 or 2>
当您有额外的数据行要添加到现有查询时,使用附加查询(即 union/union 全部在 SQL 中)。在 Power BI 中连接表称为 "merging":
您可以在 Combine queries section of Tutorial: Shape and combine data in Power BI Desktop 文章中找到更多详细信息。
请注意,如果您在模型中的表之间定义了正确的关系(也在上面的文章中进行了描述)。