"Format String" Transformation error when NULL values are present

  • What Grafana version and what operating system are you using?
    v11.4.1

  • What are you trying to achieve?
    Apply a Format String transformation to a field that contains some null values.

  • How are you trying to achieve it?
    I have a Stat panel pulled from SQL data that displays Distinct Counts of values in each field. I use the Transformations tab to apply a Format String → Trim transformation to one of the fields.

  • What happened?
    “Error transforming data: Cannot read properties of null (reading ‘trim’)”
    There are some null values in this field, which seem to be causing the transformation to not work. I do not want to filter out those rows, though, as that will throw off the counts of the other fields in the Stat panel.

  • What did you expect to happen?
    The transformation to Trim any non-null values in the field (remove leading and trailing spaces). Some values are being counted as distinct where they should not be because of these spaces.

Is there some way to make the transformation ignore the null values? Or apply some other transformation beforehand that will convert the null values to “something else”?

You are getting this error in Grafana:
“Error transforming data: Cannot read properties of null (reading ‘trim’)”
This happens because your data has null values, and Grafana cannot use the Trim function on null.
try this in query
COALESCE(TRIM(department), 'Unknown') AS cleaned_department
Note: TRIM(department) removes spaces & COALESCE(…, ‘Unknown’) replaces null with the word “Unknown”.
You get clean data.

Thank you for the quick response. Is there a way to do this purely through Transformations? This panel is using another panel in the dashboard as the source rather than its own query, and there are other panels doing the same that are filtering on null. I’d like to avoid having to change the Transformations in the other panels if possible.

Alternatively, is there somewhere I can request that the Format String transformation ignore Null values so it will work in this scenario? Sorry if that’s a newb question, but I’m new here. :slight_smile:


Use this convert field type it will conver you null to string and after that use Format string transformation now it will not give any error.

1 Like

I would recommend you use coalesce or isnull type of function to give it a non null value like empty string unless that approach breaks your aggregation etc

Please post some sample data for which you are having issues with

I should have known! :joy: Thank you, this works perfectly

1 Like

Thanks @cayse for your feedback :blush:.

1 Like