# Filter data for dashboard

**URL:** <https://discourse.nodered.org/t/filter-data-for-dashboard/89432>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [11 July 2024 17:18 UTC](https://discourse.nodered.org/t/filter-data-for-dashboard/89432 "2024-07-11T17:18:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomtom](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@Tomtom](https://discourse.nodered.org/u/Tomtom)\
**Post date:** [11 July 2024 17:18 UTC](https://discourse.nodered.org/t/filter-data-for-dashboard/89432/1 "2024-07-11T17:18:02Z")

</div>

Hi everyone,

I am having a node that generates for each day of the year some power consumption data:

> ["01-06-2024",9,0,"test"]  
> ["02-06-2024",10,7,"test"]  
> ["03-06-2024",12,0,"test"]  
> ["04-06-2024",5,1,"test"]  
> ["05-06-2024",4,1,"test"]  
> ["06-06-2024",2,0,"test"]  
> ["07-06-2024",1,0,"test"]  
> ... until  
> ["31-12-2024",1,0,"test"]

I display these data in a dashboard.

I would like to add a dropdown with the months selection, so that only the data of the selected month will be displayed but I am struggling on creating a function to select only the lines relevant for the selected month.

Could ou help me on that function?  
Many thanks,  
Thomas

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [11 July 2024 19:00 UTC](https://discourse.nodered.org/t/filter-data-for-dashboard/89432/2 "2024-07-11T19:00:02Z")

</div>

Where does the data come from?

It's a bit unusual to see DD-MM-YYYY date format in input data. YYYY-MM-DD might be easier to work with.

For example this function returns 7

```auto
const datestring = "2024-07-12"
const monthnum = new Date(datestring).getMonth() + 1
msg.payload = monthnum
return msg;

```

---

<div class="post-metadata">

**Author:** ![Tomtom](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@Tomtom](https://discourse.nodered.org/u/Tomtom)\
**Post date:** [12 July 2024 07:52 UTC](https://discourse.nodered.org/t/filter-data-for-dashboard/89432/3 "2024-07-12T07:52:49Z")

</div>

Yes, I’m able to capture this from the date as well, but how to build a function able to delete (or extract) all strings that are not from the relevant month ?

The data are coming from an external Energy provider database which I store in a local file.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [12 July 2024 08:57 UTC](https://discourse.nodered.org/t/filter-data-for-dashboard/89432/4 "2024-07-12T08:57:24Z")

</div>

Your input data looks like an array of arrays.

Use a split node to create one message per day eg` ["01-03-2024",9,0,"test"]`  
Then a function node can test the value of the month part of the date, something like this

```auto
const chosenmonth = flow.get("chosenmonth") || ""
if (msg.payload[0].split("-")[1] == chosenmonth) {
    return msg;
}

```

Then either pass the filtered messages on individually or use a join node to recreate the array of arrays structure.

---

<div class="post-metadata">

**Author:** ![Tomtom](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@Tomtom](https://discourse.nodered.org/u/Tomtom)\
**Post date:** [16 July 2024 10:57 UTC](https://discourse.nodered.org/t/filter-data-for-dashboard/89432/5 "2024-07-16T10:57:41Z")

</div>

This is working fine, many thanks Jbudd 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 October 2024 10:58 UTC](https://discourse.nodered.org/t/filter-data-for-dashboard/89432/6 "2024-10-14T10:58:16Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
