How To Create a Dashboard Filter Through the API

Knowledge Drop

Last tested: Mar 30, 2020
 

The good news: creating the filter is very easy!

The bad news: applying it to tiles is much more involved

tl;dr

  1. Run create_dashboard_filter endpoint, noting the filter name.
  2. Run get_all_dashboardelements to get an array of all tiles on the dashboard.
  3. Run get_dashboard_element and copy any existing applied dashboard filters in the "listens" array.
  4. Run update_dashboard_element by pasting in the existing filters to the "listens" array, then add your new filter to this array.
  5. Repeat steps 3 and 4 for any other elements returned in step 2 that you would like to have the new filter

Creating the Filter

Use the create_dashboard_filter endpoint: https://docs.looker.com/reference/api-and-integration/api-reference/v3.1/dashboard#create_dashboard_...

For more complex filter syntax, I'd recommend creating an example filter then using the endpoint to get the filter and see what the API returns. An example of a simple call:

Screen Shot 2020-03-30 at 2.10.48 PM.png

Applying the filter

This part is a bit more involved. Note the name of the dashboard filter we just created, as we will be using it in the final API call.

First, we will want to get a list of dashboard elements (tiles) on the dashboard that we created the filter on using this endpoint: https://docs.looker.com/reference/api-and-integration/api-reference/v3.1/dashboard#get_all_dashboard... This will return an array of dashboard element ids to update.

Now, it is important to call get_dashboard_element: https://docs.looker.com/reference/api-and-integration/api-reference/v3.1/dashboard#get_dashboardelem... This will return lots of info, but search for the "listens" array and copy that. This array is the list of filters already applied to this tile, we will need to preserve that when adding this new one. Example output to look for:

Screen Shot 2020-03-30 at 1.36.35 PM.png

If this array is empty, that is fine, it means no filters have been applied to this tile yet.

From here we will call update_dashboard_element: https://docs.looker.com/reference/api-and-integration/api-reference/v3.1/dashboard#update_dashboarde... The body of the call will look like so:

Screen Shot 2020-03-30 at 2.15.36 PM.png

In this example, "created" and "created_2" were already applied, so I pasted them from the get_dashboard_element call. I then added the comma and the next object for the new filter.

Congratulations! You have now added your new dashboard filter to a tile on your dashboard. Repeat for any other tiles in the get_all_dashboardelements call you would like this applied to.

This content is subject to limited support.                

Comments
Gijs1
New Member

Hi, im trying to do what is described above, but im having difficulties finding that ‘listens’ array mentioned above. Also the screenshots are not available, so that does not really help.

I tried with python sdk 3.1 and 4.0.

What am I missing here?

akhilsai
Participant I

Hi Ryan, 

Can you reupload the screenshots. We are not able to see them

Gijs1
New Member
Gijs1
New Member

I also got some Python SDK warnings in my IDE, but it actually does execute the api call with the outcome I want:

4269d7cc-1d53-4289-9446-a7f3582100ac.png
akhilsai
Participant I

@Gijs1 : Thanks for your inputs.

One Question I have is, When we select the filters in the dashboard. Do those filter values capture?

Gijs1
New Member

@Gijs1 : Thanks for your inputs.

When we select the filters in the dashboard. Do those filter values capture?

I dont understand your question (and im not that known with Looker).

Before I call the code to create a DashboardElement (and let that element listen to the filters), I already created the filters needed on this dashboard, with the exact same name I use in the code above:

    response = sdk.create_dashboard_filter(
body=ml.WriteCreateDashboardFilter(
dashboard_id=str(_dashboard_id),
name='source',
title='source',
type="field_filter",
dimension='tags.source',
model='star_schema',
explore='invitations_and_responses',
required=False,
))
akhilsai
Participant I

@Gijs1 :

We have a requirement like below,

  • We need to send bulk data to Google Spreadsheet after invoking an action from looker.

The below are the things we implemented

  • We are able to send the whole data to Google Spreadsheet but not filtered data.

What we want is the below 

  • We are looking for an looker SDK where we can captures filter values. 
Gijs1
New Member

Sorry I cant help with that. This topic is to use the SDK to create a dashboard within Looker, which is different from what you are asking.

akhilsai
Participant I

Sorry I cant help with that. This topic is to use the SDK to create a dashboard within Looker, which is different from what you are asking.

No Issues. Thanks for you help

Version history
Last update:
‎06-14-2021 06:16 PM
Updated by: