I have the following datatable:
let DataSource = datatable(Name:string, value1:real, value2:real)
[
'Section 1', 2211.47, 3564.05,
'Transfer 1', 3075.44, 3717.51,
'Transfer 2', 3460.66, 4194.16,
'Transfer 3', 4819.36, 482.02,
'Section 2', 1742.02, 11971.05
];
I need to add to this datatable a new row which with the following values:
Name: Transfer value1: sum of value1 for all Names containing Transfer value2: average of value2 for all Names containing Transfer
The expected outcome is:
let DataSource = datatable(Name:string, value1:real, value2:real)
[
'Section 1', 2211.47, 3564.05,
'Transfer', 11355.46, 2797.9,
'Section 2', 1742.02, 11971.05
];
I don't know if there is a method similar to extend which can create a new row to achieve this result. I tried this way:
let DataSource = datatable(Name:string, value1:real, value2:real)
[
'Section 1', 2211.47, 3564.05,
'Transfer 1', 3075.44, 3717.51,
'Transfer 2', 3460.66, 4194.16,
'Transfer 3', 4819.36, 482.02,
'Section 2', 1742.02, 11971.05
];
DataSource
| as t1
| union (t1 | summarize Name = "Transfer", value1 = sum(value1), value2 = sum(value2))
| where Name !in ("Transfer 1", "Transfer 2", "Transfer 3")
It works but I think it will take a lot of cpu memory if we are working in huge dataset.
Is there a better approach ?
You can use the
extendoperator to modify theNamecolumn to group all the names containingTransferand then use thesummarizeoperator to compute the sum ofvalue1and the average ofvalue2for each unique value ofName. Below is the code:Code:
This code uses the
extendoperator to modify theNamecolumn using theiiffunction. Theiiffunction checks if theNamecolumn contains the word "Transfer", then replaces the value of theNamecolumn with the string "Transfer". Otherwise, it leaves the value of theNamecolumn unchanged. Thesummarizeoperator is then used to compute the sum ofvalue1and the average ofvalue2for each unique value ofName.Output:
demo