2017-08-03 121 views
0

我從SQL數據庫導入到雙向電力以下兩個表:替換電源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>

現在,我需要計算的列添加到第二個表,其中ID是用名稱替換。像,

<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>

我怎麼能以有效的方式實現這一點,以免造成多列。

在此先感謝。

回答

1

嗯,有可能是一個更好的辦法,但我使用的電源查詢得到這個:

enter image description here

我開始用這個作爲表1:

enter image description here

,並以此爲表2:

enter image description here

然後我在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" 
0

這不是很漂亮,但如果你想在DAX,而不是查詢編輯器來做到這一點,你可以用這個公式;只需將表格名稱替換爲「表格2」和「名稱」即可。

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])) 
        ) 
       ) 
      ) 
     ) 
    ) 
) 

Results

這個公式將最多隻能處理兩個逗號。如果要擴展它以處理更多,請將LOOKUPVALUE替換爲另一個IF,以擴展每個SEARCH函數以檢查第三個逗號。