Hi,
I've seen a few similar discussions and have tried the fixes, but can't seem to get them to work for me.
I have a column called amount that is not stored as a number.
I'd like to display the total of that column within the statistic option.
Hi,
I've seen a few similar discussions and have tried the fixes, but can't seem to get them to work for me.
I have a column called amount that is not stored as a number.
I'd like to display the total of that column within the statistic option.
hey @iancole1989 ! here's how I've done it
Hi @DavidD thanks for the quick response.
So when I try that...
{{_.sum(alltransactions.data.map(d=>parseFloat(d.amount)))}}
I get the following error "alltransactions.data.map is not a function"
I'm definitely missing something very obvious, but can't figure it out.
Thanks again
are you able to share a screenshot?
I assume alltracactions is your table?
is "amount" a string or some other data type?
Yes, that's my table name. And yeah, it's currently stored as a string.
Here's a screenshot. Thanks again.
hm....have you tried referencing the table instead of the query?
otherwise, you might need to make sure the data is formatted as an array with
formatDataAsArray(query1.data........)
Yeah, I think it's the fact it's not formatted as an array so will have a play around with that. Thanks for all your help.
Hi @DavidD. I'm working on something similar and this worked for me! However, I want the sum to update when I add filters? Would there be a way around that? At the moment, I can get the sum of all values in my column but this value does not change when filters are added (i.e values in the column change).
Any help would be appreciated.
When you use the table as your dataset for the component, there should be a function that only returns the data being displayed on the table:
yourTable.getDisplayedData()
instead of yourTable.data
should then only be processing the currently displayed sums.
ETA: The function returns an object type and not an array so if your component needs to take array data you'll have to use formatDataAsArray(theData)
Hi @pyrrho - thank you for your reply. I'm not a developer, so still struggling with this!
What changes would I need to make to the following, to make this work?
{{_.sum(table2.data.map(d=>parseFloat(d.time_invested_hours)))}}
I've tried making the changes you suggested but still getting errors.
Thanks again.
I think you just need to use the getDisplayedData() function I described to use as the source for the map instead of data:
{{_.sum(table2.getDisplayedData().map(d=>parseFloat(d.time_invested_hours)))}}
What types of errors are you seeing when using table2.data?
Also, are you trying to use this value in a separate component or part of your table's aggregation settings?
@pyrrho I am not getting any errors with table2.data; this just shows the overall number of hours in my table. I want this value to update when filters are added.
I tried getDisplayedData() previously but got the error 'TypeError: table2.getDisplayedData is not a function'.
I'm trying to show this value in a separate text component.
Thanks
This is a little strange so I'm not sure if this is a bug or working as intended from the Retool side of things. What I am seeing is that there is a difference between the handling of the table function getDisplayedData() when it is used as part of a query/transformer/in-line processing.
What you should do is create a separate JS Query which just returns the data from this function (this works as part of a JS query but not in a transformer or inline on a component) and then in your component you will be able to use the normal formatting options and _.sum() to return the proper result:
The JS Query:
return yourTable.getDisplayedData()
The component:
{{_.sum(formatDataAsObject(yourJsQuery.data).id)}}
(or whatever your data value to sum is instead of id)
And one last thing: You'll need to setup a trigger for JS Query to run when you change the filter on the table. I also added a trigger for the refresh action on the table since you can't have the JS Query automatically run when inputs change.
Hi @pyrrho,
Resolved! Creating two separate JS queries, one for displaying data and the other for calculating the sum of values (in the displayed data) - worked. I can now show the value of this second query in a text component.
Thanks for all your help.