我将以下两个表从SQL数据库导入到power bi:
<table border="1">
<tr><th>Id</th><th>Name</th></tr>
<tr><td>1</td><td>One</td></tr>
<tr><td>2</td><td>Two</td></tr>
<tr><td>3</td><td>Three</td></tr>
<tr><td>4</td><td>Four</td></tr>
<tr><td>5</td><td>Five</td></tr>
</table>
<br>
<br>
<table border="1">
<tr><th>Selected Ids</th></tr>
<tr><td>1,2,3</td></tr>
<tr><td>1,3</td></tr>
<tr><td>1</td></tr>
<tr><td>3</td></tr>
<tr><td>1</td></tr>
<tr><td>BLANK</td></tr>
<tr><td>BLANK</td></tr>
</table>
现在,我需要将计算列添加到第二个表中,其中Ids将替换为Name。像,
<table border="1">
<tr><th>Selected Ids</th><th>Names</th></tr>
<tr><td>1,2,3</td><td>One,Two,Three</td></tr>
<tr><td>1,3</td><td>One,Three</td></tr>
<tr><td>1</td><td>One</td></tr>
<tr><td>3</td><td>Three</td></tr>
<tr><td>1</td><td>One</td></tr>
<tr><td>BLANK</td><td>BLANK</td></tr>
<tr><td>BLANK</td><td>BLANK</td></tr>
</table>
如何以有效的方式实现这一目标,避免创建多个列。
先谢谢。
答案 0 :(得分:1)
嗯,可能有更好的方法,但我使用Power Query来实现这个目标:
我从表1开始:
,如表2所示:
然后我在Table2的查询中工作。
这是M代码:
let
Source = Excel.Workbook(File.Contents("C:\Users\mpincince\Desktop\SelectedIds.xlsx"), null, true),
Table2_Table = Source{[Item="Table2",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table2_Table,{{"SelectedIds", type text}}),
//The lines above established Table2. The following lines address your question...
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Added Table1Copy" = Table.AddColumn(#"Added Index", "Table1Copy", each Table1),
#"Expanded Table1Copy" = Table.ExpandTableColumn(#"Added Table1Copy", "Table1Copy", {"Id", "Name"}, {"Id", "Name"}),
#"Added IdComparison" = Table.AddColumn(#"Expanded Table1Copy", "NameOfMatchedId", each if [SelectedIds]<> "" then (if Text.Contains([SelectedIds],[Id]) then [Name] else false) else null),
#"Filtered Rows" = Table.SelectRows(#"Added IdComparison", each ([NameOfMatchedId] <> false)),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Index", "SelectedIds"}, {{"Names", each ([NameOfMatchedId])}}),
#"Extracted Values2" = Table.TransformColumns(#"Grouped Rows", {"Names", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
#"Removed Columns" = Table.RemoveColumns(#"Extracted Values2",{"Index"})
in
#"Removed Columns"
答案 1 :(得分:0)
它不漂亮,但是如果你想在DAX而不是查询编辑器中完成这个,你可以使用这个公式;只需将“Table2”和“Names”替换为表格的名称。
Names = IF(Table2[Selected Ids] = "BLANK", "BLANK",
IF(
SEARCH(",", Table2[Selected Ids], 1, -1) = -1,
LOOKUPVALUE(Names[Name], Names[Id], Table2[Selected Ids]),
CONCATENATE(
CONCATENATE(LOOKUPVALUE(Names[Name], Names[Id],
MID(Table2[Selected Ids], 1, SEARCH(",", Table2[Selected Ids], 1, LEN(Table2[Selected Ids])) - 1)
), ","),
IF(
SEARCH(",", Table2[Selected Ids],
SEARCH(",", Table2[Selected Ids], 1, LEN(Table2[Selected Ids])) + 1,
-1) = -1,
LOOKUPVALUE(Names[Name], Names[Id],
MID(Table2[Selected Ids],
SEARCH(",", Table2[Selected Ids], 1, LEN(Table2[Selected Ids])) + 1,
LEN(Table2[Selected Ids]) - SEARCH(",", Table2[Selected Ids], 1, LEN(Table2[Selected Ids]))
)
),
CONCATENATE(
CONCATENATE(LOOKUPVALUE(Names[Name], Names[Id],
MID(Table2[Selected Ids],
SEARCH(",", Table2[Selected Ids], 1, LEN(Table2[Selected Ids])) + 1,
SEARCH(",", Table2[Selected Ids], SEARCH(",", Table2[Selected Ids], 1, LEN(Table2[Selected Ids])) + 1, LEN(Table2[Selected Ids])) - SEARCH(",", Table2[Selected Ids], 1, 0) - 1
)
), ","),
LOOKUPVALUE(Names[Name], Names[Id],
MID(Table2[Selected Ids],
SEARCH(",", Table2[Selected Ids], SEARCH(",", Table2[Selected Ids], 1, LEN(Table2[Selected Ids])) + 1, LEN(Table2[Selected Ids])) + 1,
LEN(Table2[Selected Ids]) - SEARCH(",", Table2[Selected Ids], SEARCH(",", Table2[Selected Ids], 1, LEN(Table2[Selected Ids])) + 1, LEN(Table2[Selected Ids]))
)
)
)
)
)
)
此公式最多只能处理两个逗号。如果您想扩展它以处理更多内容,请将最后一个LOOKUPVALUE
替换为另一个IF
,扩展每个SEARCH
函数以检查第三个逗号。