Here's an example of my data:
Sample
Method A
Method B
Method C
Method D
Method E
BATCH Nu
Lab Data
Sample 1
1
2
8
TX_0001
LAB1
Sample 1
5
9
TX_0002
LAB2
Sample 2
7
8
8
23
TX_0001
LAB1
Sample 2
41
TX_0001
LAB2
Sample 3
11
55
TX_0394
LAB2
Sample 4
2
9
5
9
TX_0394
LAB1
I need to make a M Language code that unites them, based on duplicated samples. Note that they might be in the same batch and/or in the same lab, but they won't ever be made the same method twice.
So I can't pass the column names, because they keep changing, and I wanted to do it passaing the column names dynamically.
**OBS: I have the possibility to make a linked table of the source to a Microsoft Access and make this with SQL, but I couldn't find a text aggregation function in MS Access library. There it's possible to each column name with no problem. (Just a matter that no one else knows M Language in my company and I can't let this be non-automated)
**
This is the what I have been trying to improve, but I keep have some errors:
1.Both goruped columns have "Errors" in all of the cells
2.Evaluation running out of memory
I can't discover what I'm doing wrong here.
let
Source = ALS,
schema = Table.Schema(Source),
columns = schema[Name],
types = schema[Kind],
Table = Table.FromColumns({columns,types}),
Number_Columns = Table.SelectRows(Table, each ([Column2] = "number")),
Other_Columns = Table.SelectRows(Table, each ([Column2] <> "number")),
numCols = Table.Column(Number_Columns, "Column1"),
textColsSID = List.Select(Table.ColumnNames(Source), each Table.Column(Source, _) <> type number),
textCols = List.RemoveItems(textColsSID, {"Sample ID"}),
groupedNum = Table.Group(Source, {"Sample ID"},List.Transform(numCols, each {_, (nmr) => List.Sum(nmr),type nullable number})),
groupedText = Table.Group(Source,{"Sample ID"},List.Transform(textCols, each {_, (tbl) => Text.Combine(tbl, "_")})),
merged = Table.NestedJoin(groupedNum, {"Sample ID"}, groupedText, {"Sample ID"}, "merged"),
expanded = Table.ExpandTableColumn(merged, "merged", Table.ColumnNames(merged{1}[merged]))
in
expanded
This is what I expected to have:
Sample
Method A
Method B
Method C
Method D
Method E
BATCH Nu
Lab Data
Sample 1
1
2
5
9
8
TX_0001_TX_0002
LAB1_LAB2
Sample 2
7
8
8
23
41
TX_0001_TX_0001
LAB1_LAB1
Sample 3
11
55
TX_0394
LAB2
Sample 4
2
9
5
9
TX_0394
LAB1
Here is a method which assumes only that the first column is a column which will be used to group the different samples.
It makes no assumptions about any column names, or the numbers of columns.
It tests the first 10 rows in each column (after removing any nulls) to determine if the column type can be type number, otherwise it will assume type text.
If there are other possible data types, the type detection code can be expanded.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//dynamically detect data types from first ten rows
//only detecting "text" and "number"
colNames = Table.ColumnNames(Source),
checkRows = 10,
colTestTypes = List.Generate(
()=>[t=
let
Values = List.FirstN(Table.Column(Source,colNames{0}),10),
tryNumber = List.Transform(List.RemoveNulls(Values), each (try Number.From(_))[HasError])
in
tryNumber, idx=0],
each [idx] < List.Count(colNames),
each [t=
let
Values = List.FirstN(Table.Column(Source,colNames{[idx]+1}),10),
tryNumber = List.Transform(List.RemoveNulls(Values), each (try Number.From(_))[HasError])
in
tryNumber, idx=[idx]+1],
each [t]),
colTypes = List.Transform(colTestTypes, each if List.AllTrue(_) then type text else type number),
//Group and Sum or Concatenate columns, keying on the first column
group = Table.Group(Source,{colNames{0}},
{"rw", (t)=>
Record.FromList(
List.Generate(
()=>[rw=if colTypes{1} = type number
then List.Sum(Table.Column(t,colNames{1}))
else Text.Combine(Table.Column(t,colNames{1}),"_"),
idx=1],
each [idx] < List.Count(colNames),
each [rw=if colTypes{[idx]+1} = type number
then List.Sum(Table.Column(t,colNames{[idx]+1}))
else Text.Combine(Table.Column(t,colNames{[idx]+1}),"_"),
idx=[idx]+1],
each [rw]), List.RemoveFirstN(colNames,1)), type record}
),
//expand the record column and set the data types
#"Expanded rw" = Table.ExpandRecordColumn(group, "rw", List.RemoveFirstN(colNames,1)),
#"Set Data Type" = Table.TransformColumnTypes(#"Expanded rw", List.Zip({colNames, colTypes}))
in
#"Set Data Type"
Original Data
Results
One way. You could probably do this all within the group as well
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
names = List.Distinct(List.Select(Table.ColumnNames(Source), each Text.Contains(_,"Method"))),
#"Grouped Rows" = Table.Group(Source, {"Sample"}, {{"data", each _, type table }}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Batch Nu", each Text.Combine(List.Distinct([data][BATCH Nu]),"_")),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Lab Data", each Text.Combine(List.Distinct([data][Lab Data]),"_")),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom", each Table.SelectRows(Table.UnpivotOtherColumns([data], {"Sample"}, "Attribute", "Value"), each List.Contains(names,[Attribute]))),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom.1", each Table.Pivot([Custom], List.Distinct([Custom][Attribute]), "Attribute", "Value", List.Sum)),
#"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom3" , "Custom.1", names,names),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom.1",{"data", "Custom"})
in #"Removed Columns"
I want to perform a group by in Entity Framework core with no repetitive groups.
Lets suppose I have two columns
Column A Column B
1 1
2 1
2 1
4 5
5 4
If a group by is performed for two columns Entity framework core the result is pretty obvious.
Column A Column B
1 1
2 1
4 5
5 4
But i want to perform a group by which works both ways A->B and B->A hence the result would be
Column A Column B
1 1
2 1
5 4
Any idea how to do that in Entity Framework Core?
My original attempt was to use Union
var user = _context.Transactions
.Where(p => !p.IsDeleted && (p.ReceiverUserId == userId) &&
(p.SenderUserId != null))
.Include(p => p.SenderUser)
.GroupBy(p => p.SenderUserId)
.Select(p => new TempModel { Id = p.FirstOrDefault().SenderUser.Id, User = p.FirstOrDefault().SenderUser, CreatedDate = p.FirstOrDefault().CreatedDate });
var user2 = _context.Transactions
.Where(p => !p.IsDeleted && (p.SenderUserId == userId) &&
(p.ReceiverUserId != null))
.Include(p => p.ReceiverUser)
.GroupBy(p => p.ReceiverUserId)
.Select(p => new TempModel { Id = p.FirstOrDefault().ReceiverUser.Id, User = p.FirstOrDefault().ReceiverUser, CreatedDate = p.FirstOrDefault().CreatedDate});
var finalQuery = user.Union(user2);
var finalQuery2 = finalQuery.GroupBy(p => p.Id);
var finalQuery1 = finalQuery2.OrderByDescending(p => p.FirstOrDefault().CreatedDate);
finalQuery.GroupBy(p => p.Id); <- this line gives error
You should sort these columns by descending: 4-5 => 5-4; 5-4 => 5-4;
5-5 => 5-5 and then group by or distinc by them:
var answer = db.Table.Select(x => new
{
ColumnA = x.ColumnA > x.ColumnB ? x.ColumnA : x.ColumnB,
ColumnB = x.ColumnA > x.ColumnB ? x.ColumnB : x.ColumnA
}).Distinct().ToList();
The following script returns the values from the for loop in rows. Is there anyway to return the results into the adjacent columns instead?
function readMessage(msgId){
var url = "https://api.pipedrive.com/v1/mailbox/mailThreads/";
var token = "/mailMessages?api_token=token";
Logger.log("readMessage called with msgId: " + msgId);
if (msgId){ // make sure there is a msgId in the cell
var rows = []
var response = UrlFetchApp.fetch(url+msgId+token);
var dataAll = JSON.parse(response.getContentText());
var dataSet = dataAll;
var rows = [], data;
for (var i = 0; i < dataSet.data.length; i++) {
data = dataSet.data[i];
rows.push([data.body_url]);
}
Logger.log( JSON.stringify(rows,null,2) );
return rows;
}
Current output is-
A B C D
1 =readMessage(msgId)
2 "blah blah"
3 "blah blah"
What I want is-
A B C D
1 =readMessage(msgId) "blah blah" "blah blah"
2
3
Google Apps Script data is always in the form of a 2D array. It's helpful to have visualization.
A B C
1 [[A1, B1, C1],
2 [A2, B2, C2]]
in order to return a row of data the function should return
[[A,B,C]] // 3 columns of data in one row
in order to return a column of data you can use either the full 2D array or a single array
[[1],[2],[3]]
OR
[1,2,3]
The issue is the formatting of the data you are returning. Try this rows.push(data.body_url); instead:
for (var i = 0; i < dataSet.data.length; i++) {
data = dataSet.data[i];
rows.push(data.body_url); //Brackets [] have been removed to insert data as a row.
}
If you return data like this: [[1],[3],[6]] the result will be a column of values:
1
3
6
If you data has this format: [2,4,8] you get a row of values:
2 | 4 | 6
Source: Custom Function
I need to sum all the even values from my array, so here is an example of it:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 4
[5] => 6
[6] => 6
)
looking a way to sum all from same value:
Array
(
[1] => 4
[4] => 1
[6] => 2
)
Any ideas?
var buckets:Object = {};
var data:Array = [1, 1, 1, 1, 4, 6, 6];
for(var i=0; i<data.length; ++i) {
if(!buckets[data[i]]) {
buckets[data[i]] = 1;
} else {
buckets[data[i]]++;
}
}
trace(buckets);
Try this:
var sum:uint = 0; //Setting the sum value to 0;
for(var i:uint = 0; i < nameOfArray.length; i++){ //Loops trough the array
if(nameOfArray[i] % 2 == 0 ){ //If the number is an even number
sum+=nameOfArray[i]; //Add that even number to the sum variable
}
}
trace(sum) // Prints out the sum
You could create a map & loop through your array, adding the values into you're map. If you don't know what a map is, it is basically a collection type. It is created using a pair, using a unique key & a value associated with it. In your case, the unique key will be the number value in the array and the value will be the number of times it appears (frequency).
Her is an image showing a the table i have, b the grid i need to display.
opps cant post image. ill try to explain. My table have four colums.
Project Number(String)
ItemNumber(String)
Location(String)
Qty.(Real).
My grid need to look like this.
ProjectNumber
ItemNumber
QtyMain.
QtyOther.
I Need to write a linq query Grouping evry line so i will se 1 Line pr Project/ItemNumber combination summing qty into 2 differetn colums 1 showing to qty where location is main and 1 showing the qty where location is not (!=) main. Can linq do this for me, or how can thsi be done?
public class Foo
{
public Int32 ProjectNumber;
public String ItemNumber;
public String InventLocation;
public Int32 Qty;
}
void Main()
{
List<Foo> foos = new List<Foo>(new[]{
new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Main", Qty = 3 },
new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Main", Qty = 3 },
new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Sub", Qty = 2 },
new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Sub", Qty = 1 },
new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Sub2", Qty = 5 }
});
var foo = from f in foos
group f by new { f.ProjectNumber, f.ItemNumber } into fGroup
select new {
ProjectNumber = fGroup.Key.ProjectNumber,
ItemNumber = fGroup.Key.ItemNumber,
QtyMain = fGroup.Where (g => g.InventLocation == "Main").Sum (g => g.Qty),
Rest = fGroup.Where (g => g.InventLocation != "Main").Sum (g => g.Qty)
};
foo.Dump();
}
Resulting in:
IEnumerable<> (1 item)
ProjectNumber ItemNumber QtyMain Rest
1 a 6 8