Change Data using PostAPI

visualizations
low_priority
done
reply
dashboards
(Prateek Agrawal) #1

I want to change data without changing the url of Iframe. Consider this example-

  1. Currently graph x-axis respesents months
  2. Now User changes months to week
  3. Finally graph x-axis represents weeks

I inspected the website, I found that after applying the filter, iframe url doesn’t change. I want to know how to achieve this? Is it using PostMessage api? If yes, Then How?

0 Likes

(Izzy) #2

This is generally done using javascript events to communicate from parent page to iframe: Check out this page, it’s super instructive.
https://docs.looker.com/reference/embedding/embed-javascript-events#step-by-step_instructions

If you’ve got any questions about the finer points of that, happy to help answer :slight_smile:

0 Likes

(Prateek Agrawal) #3

Thanks but I want to change the measure of graph, not filter. for example, on X- axis:

The first one is measured as days and second graph as months-

How do I change measure?

0 Likes

(Ben Silverstein) #4

Prateek,
This seems like a match made in heaven for parameters and dynamic dimensions. Try looking at this discourse thread Great Use Cases for Parameter Fields to understand a bit better. Essentially, you’ll want to create a parameter for your users (or your API) to select from allowed values, corresponding to the date buckets you require. From there, create a dimension (selected_period) with CASE WHENs in the SQL to select the appropriate date period you’re after, and make sure that’s your x-axis dimension. To make the dimension dynamic, it’s as simple as referencing the parameter’s value with Liquid tags. Hope I answered your question alright!

As an aside - since your UI appears to force the selection of one of your date buckets, this won’t be a problem, but typically I’d populate the ELSE clause of your parameter with whatever default dimension your business users would like to see (i.e. week, etc.) for the situation when the parameter isn’t populated. Alternatively, you could apply a default_value to your parameter.

EDIT: Scrolling further down in the Discourse thread yields more gold. Check out brettg’s post on on using the liquid conditions and _parameter_value instead of CASE WHEN, though I can’t comment on the performance difference.

2 Likes

(Prateek Agrawal) #5

Thanks @bens , I read the article and got an idea how to solve my problem, but there is small issue that I have. I am using dimension_group instead of dimension. This documentation doesn’t tell about Use of parameter with dimension_group.

Could you please give some advice regarding it?

My LookML code for looks like this-

parameter: timelinefilter {
  type: string
  allowed_value: {
    label: "week"
    value: "week"
  }
  allowed_value: {
    label: "month"
    value: "month"
  }
}
dimension_group: timeline {
    type: time
    timeframes: [
      date,
      week,
      month
    ]
    sql: ${TABLE}.usage_date::date ;;
}
0 Likes

(Ben Silverstein) #6

You’re getting there, Prateek.

You’ll need three ingredients:

  1. Your timelinefilter parameter for users to select a date period type
  2. Your source dimensions (in this case, your dimension_group with the timeframes you chose will create three dimensions automatically - timeline_date, timeline_week, timeline_month)
  3. One last dimension referencing those timeline_ dimensions in branches of a CASE (forgive me if my terminology is off - I learned SQL on my own and am a data team of one at our company). This is the dimension you’ll want to add to your visualization - not your actual source dims. The link I shared before actually uses this exact scenario so I’m going to suggest that you have a go at it yourself.

By the way - if ever there’s terminology you need to understand better, docs.looker.com is your friend. When I feel compelled to ask a question, I try to take a step back, rethink what I’m trying to accomplish, and find the discourse threads and docs content that might guide me to a solution. They’re usually there!

1 Like

(Prateek Agrawal) #7

I found the solution and writing solution here so that it may be helpful for someone in the future.

dimension: usage_date {
  label_from_parameter: usage_date_filter
  description: "Usage"
  type: string
  sql:
    CASE
      WHEN {% parameter usage_date_filter %} = 'week' THEN ${timeline_week}
      WHEN {% parameter usage_date_filter %} = 'month' THEN ${timeline_month}
      WHEN {% parameter usage_date_filter %} = 'time' THEN ${timeline_time}
    END ;;
}

dimension_group: timeline {
  type: time
  timeframes: [
    time,
    week,
    month
  ]
  sql: ${TABLE}.usage_date::timestamp;;
}

parameter: usage_date_filter {
  type: string
  default_value: "week"
  allowed_value: {
    label: "Weekly"
    value: "week"
  }
  allowed_value: {
    label: "Monthly"
    value: "month"
  }
  allowed_value: {
    label: "Daily"
    value: "time"
  }
}
1 Like

Use of parameter with dimension_group (Create dynamic dimension_group in looker)