Power BI Embedded 問與答Q&A in Power BI Embedded

Power BI Embedded 提供您一種將問與答併入應用程式的方式,讓您的使用者使用自然語言提問,並立即收到類似圖表或圖形視覺效果的表單回應。Power BI Embedded offers you a way to incorporate Q&A into an application and allow your users to ask questions using natural language and receive immediate answers in the form of visuals like charts or graphs.

內嵌框架中的問與答互動式問題

內嵌在應用程式中的問與答有兩種模式:互動式只產生結果There are two modes for embedding Q&A within your application: interactive and result only. 互動式模式讓您輸入問題,並在視覺效果中顯示。Interactive mode allows you to type in questions and have them displayed within the visual. 如想要顯示事先準備好的問題,您可以在內嵌組態中擴展問題,使用只產生結果模式。If you have a saved question, or a set question you want to display, you can use the result only mode by populating the question in your embed config.

JavaScript 程式碼看起來會像下面這樣。Here is a look at what the JavaScript code will look like.

// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed within the JavaScript API.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
var config= {
    type: 'qna',
    tokenType:   models.TokenType.Embed | models.TokenType.Aad,
    accessToken: access token value,
    embedUrl:    https://app.powerbi.com/qnaEmbed (groupId to be appended as query parameter if required),
    datasetIds:  array of requested data set ids (at the moment we support only one dataset),
    viewMode:    models.QnAMode.Interactive | models.QnAMode.ResultOnly,
    question:    optional parameter for Explore mode (QnAMode.Interactive) and mandatory for Render Result mode (QnAMode.ResultOnly)
};

// Get a reference to the embedded QNA HTML element
var qnaContainer = $('#qnaContainer')[0];

// Embed the QNA and display it within the div container.
var qna = powerbi.embed(qnaContainer, config);

事先準備好的問題Set question

如果對事先準備好的問題使用結果模式,您可以在框架中插入其他問題,讓它們立即獲得解答,取代原先的結果。If you used result mode with a set question, you can inject additional questions into the frame and have them immediately answered replacing the previous result. 符合新問題的新視覺效果隨即呈現。A new visual is rendered matching the new question.

常見問題集清單即是這種使用方式的一例。One example of this usage would be a frequently asked question list. 使用者可以瀏覽問題,讓它們在相同的內嵌組件內獲得解答。The user could go through the questions and have them answered within the same embedded part.

JS SDK 使用方式的程式碼片段:Code snippet for JS SDK usage:

// Get a reference to the embedded Q&A HTML element
var qnaContainer = $('#qnaContainer')[0];

// Get a reference to the embedded Q&A.
qna = powerbi.get(qnaContainer);

qna.setQuestion("This year sales")
    .then(function (result) {
        …….
    })
    .catch(function (errors) {
        …….
    });

視覺化呈現的事件Visual rendered event

若為互動式模式,每次呈現的視覺效果以正在鍵入的更新輸入查詢為目標變更時,應用程式就會收到資料變更事件通知。For interactive mode, the application can be notified with a data changed event each time the rendered visual changes to target the updated input query as it is being typed.

接聽 visualRendered 事件可讓您儲存問題以供之後使用。Listening to the visualRendered event allows you to save questions for use later.

JS SDK 使用方式的程式碼片段:Code snippet for JS SDK usage:

// Get a reference to the embedded Q&A HTML element
var qnaContainer = $('#qnaContainer')[0];

// Get a reference to the embedded Q&A.
qna = powerbi.get(qnaContainer);

// qna.off removes a given event listener if it exists.
qna.off("visualRendered");

// qna.on will add an event listener.
qna.on("visualRendered", function(event) {
     …….
});

內嵌的權杖Embed token

建立移出資料集的內嵌權杖,以便開始問與答的一部分。Create an embed token off of a dataset to start a Q&A part. 如需詳細資訊,請參閱 Generate token for Q&A (產生問與答的權杖)。For more information, see Generate token for Q&A.

後續步驟Next steps

若要嘗試問與答內嵌,請參閱 JavaScript 內嵌範例To give Q&A embedding a try, check out the JavaScript embed sample.

有其他問題嗎?More questions? 嘗試在 Power BI 社群提問Try asking the Power BI Community