Sort Order parameter in Looker ML

Hello all!

So I’m trying to do some logic in Looker ML. I have a visualization of type: marketplace_viz_report_table::report_table-marketplace and here I have some fields that I want to see in my viz. Also I have filters based on these fields. I want to do some logic for sorting side. Based on a filter, let’s say Sort By filter, I want to introduce fields values from my viz (for example, ID, Name and others) and the possibility to select a value from this filter, single select. Then, I would like to have also a filter named Sort Order that contains 2 values, Ascending & Descending. Based on what I have selected in the Sort By filter and Sort Order filter, I want my viz to be sorted like that.

For example:
Sort By → Name
Sort Order → Desc

I want this logic because this type of viz from marketplace does not accept to sort directly from dashboard viz, but only if I enter the visualization from the explore side. I want to do this from dashboard itself.

I made a logic for this, but it’s not working for descending part, it’s doing something default. It’s grouping the value selected, and it’s sorting by asc, by default I think.

I’m gonna let here the logic that I made. Did someone try something like this? Is this logic possible? My guess is that this would be a limitation from Looker.

Thank you very much!

Dashboard Code:

  • title: Title
    name: Title
    explore: explore
    type: marketplace_viz_report_table::report_table-marketplace
    fields: [explore.name, explore.id]
    sorts: [explore.dynamic_sort_order]
    listen:
    Sort By: explore.sort_by

filters:

  • name: Sort By
    title: Sort By
    type: field_filter
    default_value: ‘’
    allow_multiple_values: true
    required: false
    ui_config:
    type: advanced
    display: popover
    explore: explore
    listens_to_filters:
    field: explore.sort_by
  • name: Sort Order
    title: Sort Order
    type: field_filter
    default_value: “”
    allow_multiple_values: false
    required: false
    ui_config:
    type: advanced
    display: button_group
    explore: explore
    listens_to_filters:
    field: explore.sort_order

View code:
parameter: sort_by {
type: string
label: “Sort By”
default_value: “none”
allowed_value: {
label: “None”
value: “none”
}
allowed_value: {
label: “Id”
value: “Id”
}
allowed_value: {
label: “Name”
value: “Name”
}

parameter: sort_order {
type: string
label: “Sort By Order”
allowed_value: {
label: “Ascending”
value: “asc”
}
allowed_value: {
label: “Descending”
value: “desc”
}
default_value: “desc”
}

dimension: dynamic_sort_order {
type: string
label_from_parameter: sort_by
sql:
  CASE
    WHEN {% parameter sort_by %} = 'Id' THEN ${explore.id}
	WHEN {% parameter sort_by %} = 'Name' THEN ${explore.name}
    ELSE NULL
  END ;;

}

In Looker, you can’t dynamically change the direction of a sort.

Instead, you’d have to use two different fields that would sort the opposite direction (like for example, a number field vs the number field times -1) and then dynamically swap between those two fields to simulate changing the sort direction.

Thank you!!!

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