diff --git a/superset/viz.py b/superset/viz.py index 4aa19aed693e2..18b030971863f 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -1762,8 +1762,12 @@ def get_data(self, df: pd.DataFrame) -> VizData: df = df.copy() df[filled_cols] = df[filled_cols].fillna(value=NULL_STRING) - row = df.groupby(self.groupby).sum()[metrics[0]].copy() - row.sort_values(ascending=False, inplace=True) + sortby = utils.get_metric_name( + self.form_data.get("timeseries_limit_metric") or metrics[0] + ) + row = df.groupby(self.groupby).sum()[sortby].copy() + is_asc = not self.form_data.get("order_desc") + row.sort_values(ascending=is_asc, inplace=True) pt = df.pivot_table(index=self.groupby, columns=columns, values=metrics) if fd.get("contribution"): pt = pt.T diff --git a/tests/core_tests.py b/tests/core_tests.py index 9ad5806e34acb..82dca0d2ab75d 100644 --- a/tests/core_tests.py +++ b/tests/core_tests.py @@ -976,6 +976,7 @@ def test_explore_json_dist_bar_order(self): "optionName": "metric_80g1qb9b6o7_ci5vquydcbe", }, ], + "order_desc": True, "adhoc_filters": [], "groupby": ["name"], "columns": [], @@ -999,7 +1000,8 @@ def test_explore_json_dist_bar_order(self): SELECT count(name) AS count_name, count(ds) AS count_ds FROM birth_names WHERE ds >= '1921-01-22 00:00:00.000000' AND ds < '2021-01-22 00:00:00.000000' - GROUP BY name ORDER BY count_name DESC, count_ds DESC + GROUP BY name + ORDER BY count_name DESC LIMIT 10; """, client_id="client_id_1", diff --git a/tests/viz_tests.py b/tests/viz_tests.py index 1e001c98e99be..fd2572dbc5a0b 100644 --- a/tests/viz_tests.py +++ b/tests/viz_tests.py @@ -462,6 +462,7 @@ def test_groupby_nulls(self): "adhoc_filters": [], "groupby": ["toppings"], "columns": [], + "order_desc": True, } datasource = self.get_datasource_mock() df = pd.DataFrame( @@ -487,6 +488,7 @@ def test_groupby_nans(self): "adhoc_filters": [], "groupby": ["beds"], "columns": [], + "order_desc": True, } datasource = self.get_datasource_mock() df = pd.DataFrame({"beds": [0, 1, nan, 2], "count": [30, 42, 3, 29]}) @@ -508,6 +510,7 @@ def test_column_nulls(self): "adhoc_filters": [], "groupby": ["toppings"], "columns": ["role"], + "order_desc": True, } datasource = self.get_datasource_mock() df = pd.DataFrame( @@ -537,6 +540,7 @@ def test_column_metrics_in_order(self): "adhoc_filters": [], "groupby": ["toppings"], "columns": [], + "order_desc": True, } datasource = self.get_datasource_mock() df = pd.DataFrame( @@ -574,6 +578,7 @@ def test_column_metrics_in_order_with_breakdowns(self): "adhoc_filters": [], "groupby": ["toppings"], "columns": ["role"], + "order_desc": True, } datasource = self.get_datasource_mock() df = pd.DataFrame(