|
| 1 | +--- |
| 2 | +title: Use the Microsoft Flow connector to run Kusto queries and commands automatically as part of a scheduled or triggered task |
| 3 | +description: Learn some common Microsoft Flow connector usage examples. |
| 4 | +author: orspod |
| 5 | +ms.author: orspodek |
| 6 | +ms.reviewer: dorcohen |
| 7 | +ms.service: data-explorer |
| 8 | +ms.topic: example-scenario |
| 9 | +ms.date: 03/15/2020 |
| 10 | +--- |
| 11 | + |
| 12 | +# Microsoft Flow connector (Preview) usage examples |
| 13 | + |
| 14 | +The Microsoft Flow connector enables you to run Kusto queries and commands automatically as part of a scheduled or triggered task, using [Microsoft Flow](https://flow.microsoft.com/). This document contains several common Microsoft Flow connector usage examples. |
| 15 | + |
| 16 | +For more information, see [Microsoft Flow connector (Preview)](flow.md). |
| 17 | + |
| 18 | +## Usage examples |
| 19 | + |
| 20 | +1. [Microsoft Flow connector and SQL](#example-1-microsoft-flow-connector-and-sql) |
| 21 | +1. [Push data to Power BI dataset](#example-2-push-data-to-power-bi-dataset) |
| 22 | +1. [Conditional queries](#example-3-conditional-queries) |
| 23 | +1. [Email multiple Azure Data Explorer Flow charts](#example-4-email-multiple-azure-data-explorer-flow-charts) |
| 24 | +1. [Send a different email to different contacts](#example-5-send-a-different-email-to-different-contacts) |
| 25 | +1. [Create a custom HTML table](#example-6-create-a-custom-html-table) |
| 26 | + |
| 27 | +## Example 1: Microsoft Flow connector and SQL |
| 28 | + |
| 29 | +Use the Microsoft Flow connector to query your data and aggregate it in an SQL database. |
| 30 | + |
| 31 | +> [!Note] |
| 32 | +> SQL insert is done separately for each row. Only use the Microsoft Flow connector for small amounts of output data. |
| 33 | +
|
| 34 | + |
| 35 | + |
| 36 | +> [!IMPORTANT] |
| 37 | +> In the *Cluster Name* field, enter the cluster URL. |
| 38 | +
|
| 39 | +## Example 2: Push data to Power BI dataset |
| 40 | + |
| 41 | +The Microsoft Flow connector can be used together with the Power BI connector to push data from Kusto queries to Power BI streaming datasets. |
| 42 | + |
| 43 | +1. Create a new *Run query and list results* action. |
| 44 | +1. Select **New step**. |
| 45 | +1. Select **Add an action** and search for *Power BI*. |
| 46 | +1. Select **Power BI**. |
| 47 | +1. Select **Add rows to a dataset**. |
| 48 | + |
| 49 | +  |
| 50 | +1. Enter the *Workspace*, *Dataset*, and *Table* to which data will be pushed. |
| 51 | +1. From the dynamic content dialog, add a *Payload* containing your dataset schema and the relevant Kusto query results. |
| 52 | + |
| 53 | +  |
| 54 | + |
| 55 | +Flow automatically applies the Power BI action for each row of the Kusto query result table. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +## Example 3: Conditional queries |
| 60 | + |
| 61 | +The results of Kusto queries can be used as input or conditions for the next flow actions. |
| 62 | + |
| 63 | +In the following example, we query Kusto for incidents that occurred during the last day. For each resolved incident, a slack message is posted and a push notification is created. |
| 64 | +For each incident that is still active, Kusto is queried for more information about similar incidents. It sends that information as an email, and opens a related TFS task. |
| 65 | + |
| 66 | +Follow these instructions to create a similar Flow: |
| 67 | + |
| 68 | +1. Create a new *Run query and list results* action. |
| 69 | +1. Select **New step**. |
| 70 | +1. Select **Condition control**. |
| 71 | +1. From the dynamic content window, select the parameter you want to use as a condition for next actions. |
| 72 | +1. Select the type of *Relationship* and *Value* to set a specific condition on the given parameter. |
| 73 | + |
| 74 | +  |
| 75 | + |
| 76 | + Flow applies this condition on each row of the query result table. |
| 77 | +1. Add actions for when the condition is true and false. |
| 78 | + |
| 79 | +  |
| 80 | + |
| 81 | +You can use the result values from the Kusto query as input for the next actions. Select the result values from the dynamic content window. |
| 82 | +In the example below, a *Slack - Post Message* action and *Visual Studio - Create a new work item* action containing data from the Kusto query were added. |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +In this example, if an incident is still active, query Kusto again to get information on how incidents from the same source were solved in the past. |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +> [!IMPORTANT] |
| 93 | +> In the *Cluster Name* field, enter the cluster URL. |
| 94 | +
|
| 95 | +Visualize this information as a pie chart and email it to the team. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +## Example 4: Email multiple Azure Data Explorer Flow charts |
| 100 | + |
| 101 | +1. Create a new Flow with "Recurrence" trigger, and define the interval of the Flow and the frequency. |
| 102 | +1. Add a new step, with one or more *Kusto - Run query and visualize results* actions. |
| 103 | + |
| 104 | +  |
| 105 | +1. For each *Kusto - Run query and visualize result*, define the following fields: |
| 106 | + * Cluster URL (in the *Cluster Name* field) |
| 107 | + * Database Name |
| 108 | + * Query and Chart Type (HTML Table/ Pie Chart/ Time Chart/ Bar Chart/ Enter Custom Value). |
| 109 | + |
| 110 | +  |
| 111 | + |
| 112 | +> [!IMPORTANT] |
| 113 | +> In the *Cluster Name* fields, enter the cluster URL. |
| 114 | +
|
| 115 | +1. Add a *Send an email* action. |
| 116 | + * In the *Body* field, insert the required *body* so that the visualized result of the query is included in the body of the email. |
| 117 | + * To add an attachment to the email, add *Attachment Name* and *Attachment Content*. |
| 118 | +  |
| 119 | + |
| 120 | +Results: |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +## Example 5: Send a different email to different contacts |
| 127 | + |
| 128 | +You can leverage Microsoft Flow to send different customized emails to different contacts. The email addresses and the email contents are a result of a Kusto query. |
| 129 | + |
| 130 | +Example: |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +> [!IMPORTANT] |
| 135 | +> In the *Cluster Name* field, enter the cluster URL. |
| 136 | +
|
| 137 | + |
| 138 | + |
| 139 | +## Example 6: Create a custom HTML table |
| 140 | + |
| 141 | +You can leverage Microsoft Flow to create and use custom HTML elements, such as a custom HTML table. |
| 142 | + |
| 143 | +The following example demonstrates how to create a custom HTML table. The HTML table will have its rows colored by log level (the same as in Azure Data Explorer). |
| 144 | + |
| 145 | +Follow these instructions to create a similar Flow: |
| 146 | + |
| 147 | +1. Create a new *Kusto - Run query and list results* action. |
| 148 | + |
| 149 | +  |
| 150 | + |
| 151 | +> [!IMPORTANT] |
| 152 | +> In the *Cluster Name* field, enter the cluster URL. |
| 153 | +
|
| 154 | +1. Loop over the query results and create the HTML table body: |
| 155 | + 1. To create a variable to hold the HTML string, select **New step** |
| 156 | + 1. Select **Add an action** and search for *Variables*. |
| 157 | + 1. Select **Variables - Initialize variable**. |
| 158 | + 1. Initialize a string variable as follows: |
| 159 | + |
| 160 | +  |
| 161 | + |
| 162 | +1. Loop over the results: |
| 163 | + 1. Select **New step**. |
| 164 | + 1. Select **Add an action**. |
| 165 | + 1. Search for *Variables*. |
| 166 | + 1. Select **Variables - Append to string variable**. |
| 167 | + 1. Select the variable name that you initialized before, and create the HTML table rows using the query results. |
| 168 | + When selecting the query results, *Apply to each* is automatically added. |
| 169 | + |
| 170 | + In the example below, the `if` expression is used to define the style of each row: |
| 171 | + |
| 172 | + ```if(equals(items('Apply_to_each')?['Level'], 'Warning'), 'Yellow', if(equals(items('Apply_to_each')?['Level'], 'Error'), 'red', 'white'))``` |
| 173 | + |
| 174 | +  |
| 175 | + |
| 176 | +1. Create the full HTML content: |
| 177 | + 1. Add a new action outside *Apply to each*. |
| 178 | + In the following example the action used is *Send an email*. |
| 179 | + 1. Define your HTML table using the variable from the previous steps. |
| 180 | + 1. If you're sending an email, select **Show advanced options** and, under *Is HTML*, select **Yes**. |
| 181 | + |
| 182 | +  |
| 183 | + |
| 184 | +Result: |
| 185 | + |
| 186 | + |
| 187 | + |
0 commit comments