1. Add a "Request" trigger action which will allow the group by to be called by another Flow:
Add the following to the "Request JSON Body schema":
{ "type": "object", "properties": { "key": { "type": "string" }, "rows": { "type": "array", "items": { "type": "object" } } } }
Note: The key data type is of type string therefore the rows whose column is the matching key also needs to be of type string.
2. Add a "Select" variable called "SelectGroupKeys". The From should contain the "rows" from the "Request" step and the map should be switched to text with the following expression:
item()?[triggerBody()?['key']]
3. Add an "Initialize variable" with the the name "Initialize Distinct Keys". The variable name should be "varDistinctKeys" of type Array and the value should have an expression of:
intersection(body('SelectGroupByKeys'),body('SelectGroupByKeys'))
4. Add another "Initialize variable" called "Initialize Results Array" with the Name "varResults" of type Array, leaving the "Value" empty.
5. Add the "Apply to each" (found under More). In the "Select an output from previous steps", add "varDistinctKeys" from step 3 above. Add a Filter array and in the "From", choose rows from the "HTTP" request.
Set the left "Choose a value" to use an expression of:
item()[triggerBody()?['key']]
The right "Choose a value" is then set to "Current item"
6. Add an "Append to array variable" step under the "Filter array":
length(body('Filter_array'))
The result of the "For each" should look like the following:
7. Finally, add a response to send the result back to the calling Flow:
The "Response Body JSON Schema" contains the following:
{ "type": "object", "properties": { "results": { "type": "array", "items": { "type": "object", "properties": { "count": { "type": "number" }, "Name": { "type": "string" }, "Group": { "type": "string" } }, "required": [ "count", "Name", "Group" ] } } } }
To test the new flow
Create a new Flow using a flow button without any inputs. Then add a compose (Found under "Data Operations") with the following:
{ "key" : "Status", "rows": [ {"Name" : "Flow1", "Status": "Test Succeeded", "Run history" : "17 minutes ago" }, {"Name" : "Flow2", "Status": "Test Succeeded", "Run history" : "59 minutes ago" }, {"Name" : "Flow3", "Status": "Test Failed", "Run history" : "3 weeks ago" }, {"Name" : "Flow4", "Status": "Test Failed", "Run history" : "4 weeks ago" } ] }
Then add an HTTP step to call the Group By Flow using the "Method" of "POST" and the "Body" of the Compose "Output":
Give the flow a name, save and run a Test. You should now check the run history, scroll to the HTTP step and review the output, which now contains the following result:
[ { "count": 2, "key": "Test Succeeded", "Items": [ { "Name": "Flow1", "Status": "Test Succeeded", "Run history": "17 minutes ago" }, { "Name": "Flow2", "Status": "Test Succeeded", "Run history": "59 minutes ago" } ] }, { "count": 2, "key": "Test Failed", "Items": [ { "Name": "Flow3", "Status": "Test Failed", "Run history": "3 weeks ago" }, { "Name": "Flow4", "Status": "Test Failed", "Run history": "4 weeks ago" } ] } ]