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
extend
operator to modify theName
column to group all the names containingTransfer
and then use thesummarize
operator to compute the sum ofvalue1
and the average ofvalue2
for each unique value ofName
. Below is the code:Code:
This code uses the
extend
operator to modify theName
column using theiif
function. Theiif
function checks if theName
column contains the word "Transfer", then replaces the value of theName
column with the string "Transfer". Otherwise, it leaves the value of theName
column unchanged. Thesummarize
operator is then used to compute the sum ofvalue1
and the average ofvalue2
for each unique value ofName
.Output:
demo