'How to filter on aggregated data in Foundry Functions?
I would like to know how can I filter on grouped data in Foundry Functions. I already managed to group and aggregate on my data, see below:
@Function()
public async grouping(lowerBound : Integer ): Promise<TwoDimensionalAggregation<string>> {
let grouping = Objects.search()
.HospitalLosAnalysis()
.groupBy(val => val['primaryHospitalName'].topValues())
.count()
//FILTER SHOULD BE HERE
return grouping
}
Now, I would like to filter only on rows, where the count is bigger than parameter lowerBound
. The problem is I am not able to filter anymore as the grouping returns a TwoDimensionalAggregation
, on which I am not able to filter anymore.
Some context: I would like to create a chart in Workshop where the user would be able to look only at hospitals with a significant count. He would input the lowerBound
parameter in a textbox, and the function would remove all of the rows that are smaller than lowerBound
.
Solution 1:[1]
Assuming what you're looking for here is to filter on the result of the aggregation - otherwise, just filter the incoming data first before aggregating.
Once you've done your TwoDimensionalAggregation, you'll have a typescript data structure of that is a list of "buckets" where each bucket has a key
and a value
property. The key
can be complex (i.e. if you groupBy a numeric, date, or timestamp property and have a range for a key) or simple, as in your case, where the key would be the primaryHospitalName
.
So you'll have something like:
`grouping.buckets[0].key === "hospitalName1"`
`grouping.buckets[0].value === N`
You can easily filter this list of buckets any number of ways: something trivial like:
const filteredGrouping = {
buckets = grouping.buckets.filter(e => e.value >= lowerBound)
}
Then return filteredGrouping
instead of grouping
. There's potentially relevant documentation about how to Create a custom aggregation here: https://www.palantir.com/docs/foundry/functions/create-custom-aggregation/
Solution 2:[2]
You should be able to loop through the grouping
variable. You can always check what is inside with an old console.log(grouping)
and then preview.
It should look something like:
let newBuckets = [];
grouping.buckets.forEach(bucket => {
if (bucket.value > lowerBound) {
newBuckets = bucket
}
});
grouping.buckets = newBuckets;
don't forget to add an await
on your let grouping = await Objects.search..etcetc..count()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | fmsf |
Solution 2 | fmsf |