資料表操作函式

Power BI 的 DAX 中級

Maarten Van den Broeck

Content Developer at DataCamp

資料表操作函式總覽

前面用過的函式
DISTINCT(<table> | <table>)

移除資料表中的重複列,或欄位中的重複值

SELECTCOLUMNS(<table>, <name>, <expression>)

將另一個資料表中的選定欄位作為新資料表傳回

新函式
ADDCOLUMNS(<table>, <name>, <expression>)

將輸入資料表附加來自另一個資料表的選定欄位後傳回

SUMMARIZE(<table>,
          <groupBy_columnName>,
          <name>,
          <expression>)

依群組回傳所要求總計的摘要資料表

Power BI 的 DAX 中級

ADDCOLUMNS()

ADDCOLUMNS(<table>, <name>, <expression>) 

將輸入資料表附加來自另一個資料表的選定欄位後傳回

ADDCOLUMNS(Fact_table,
           "Profit",
           Revenue - Costs)
Power BI 的 DAX 中級

ADDCOLUMNS()

ADDCOLUMNS(<table>, <name>, <expression>) 

將輸入資料表附加來自另一個資料表的選定欄位後傳回

ADDCOLUMNS(Fact_table,
           "Profit",
           Revenue - Costs) 
Revenue Costs Profit
100 25 75
150 25 125
Power BI 的 DAX 中級

ADDCOLUMNS()

ADDCOLUMNS(<table>, <name>, <expression>) 

將輸入資料表附加來自另一個資料表的選定欄位後傳回

ADDCOLUMNS(Fact_table,
           "Profit",
           Revenue - Costs) 
Revenue Costs Profit
100 25 75
150 25 125
SELECTCOLUMNS(<table>, <name>, <expression>)

將另一個資料表中的選定欄位作為新資料表傳回

SELECTCOLUMNS(Fact_table,
              "Profit",
              Revenue - Costs) 
Profit
75
125
Power BI 的 DAX 中級

SUMMARIZE()

SUMMARIZE(<table>,
          <groupBy_columnName>,
          <name>,
          <expression>)

依群組回傳所要求總計的摘要資料表

Power BI 的 DAX 中級

SUMMARIZE()

SUMMARIZE(<table>,
          <groupBy_columnName>,
          <name>,
          <expression>)

依群組回傳所要求總計的摘要資料表

SUMMARIZE(Amounts,

Amounts[Year], Amounts[Category],
"Total Amount", SUM(Amounts[Amount]))
Year Category Amount
2019 Tickets 50
2019 Postcards 500
2020 Tickets 200
2020 Tickets 400
Power BI 的 DAX 中級

SUMMARIZE()

SUMMARIZE(<table>,
          <groupBy_columnName>,
          <name>,
          <expression>)

依群組回傳所要求總計的摘要資料表

SUMMARIZE(Amounts,

Amounts[Year], Amounts[Category],
"Total Amount", SUM(Amounts[Amount]))
Year Category Amount
2019 Tickets 50
2019 Postcards 500
2020 Tickets 200
2020 Tickets 400

$$

Year Category Total Amount
2019 Tickets 50
2019 Postcards 500
2020 Tickets 600
Power BI 的 DAX 中級

SUMMARIZE() 最佳實務

  • SUMMARIZE() 中直接建立的欄位,可能因內容脈絡而出現非預期結果

$$

SUMMARIZE(Amounts,
          Amounts[Year],
          Amounts[Category]),
          "Total Amount",
          SUM(Amounts[Amount])
  • 建議做法:在建立新欄位時,以 ADDCOLUMNS() 包住 SUMMARIZE()
ADDCOLUMNS(
    SUMMARIZE(Amounts,
               Amounts[Year],
               Amounts[Category]),
    "Total Amount",
    SUM(Amounts[Amount])
)
Power BI 的 DAX 中級

一起來練習吧!

Power BI 的 DAX 中級

Preparing Video For Download...