Sankey chart: can't control sort order

I have a simple source → target flow with 5 categories on each side:

  • Excellent

  • Good

  • Sufficient

  • Insufficient

  • Poor

I want both sides sorted in this exact order (best at top, worst at bottom). But Metabase sorts source and target nodes independently — seemingly by flow volume — so "Excellent" might be at position 2 on the left but position 4 on the right.

This makes the chart nearly unreadable for any use case where source and target share the same categories (before/after comparisons, expected vs actual, status changes, etc.). See this sample:

Request: Add an option to sort Sankey alphabetically or by a custom order, applied consistently to both sides. Or should this work, and is this a Bug Fix for Postgres?

Could there be better controls for ordering categories in Sankey charts? Sure. But you can achieve what you want with the existing tools and without elaborate workarounds.

From an experiment, the chart layout responds to sorting the input data. The source and target categories loads from the input table in the order they appear.

Take this SQL query that loads a static table (relies on PostgreSQL extensions, may not work for all DB engines, rewrite with SELECT .. UNION ALL .. SELECT if necessary):

SELECT * FROM (VALUES 
('Apples',10,'Almonds'),
('Apples',2,'Brazil'),
('Apples',2,'Cashews'),
('Apples',2,'Dragon'),
('Bananas',10,'Brazil'),
('Carrots',2,'Cashews'),
('Dorians',10,'Dragon')
) t(fruit1, count, fruit2)
;

This generates a sankey chart like this:

If we change the row ordering with a ORDER BY substring(fruit1 from 2) , e.g., sorting by the second letter of the fruit (source category), the chart changes to this:

Notice that the ‘Brazil’ and ‘Dragon’ targets appear first as they are the first two targets expressed in the input sources.

If the sources & targets don’t lend themselves to normal sorting, use an expression to generate a sortable value.

In your example, it should be sufficient to ORDER BY source, target to achieve the desired ordering in the chart.

1 Like

Thanks for looking into this! Unfortunately, I tried your approach — my query already returns rows in the correct A→E order (verified in the table view), including padding all 25 source/target combinations with near-zero values to ensure every category appears from the first row. The Sankey chart still sorts nodes by volume rather than row order on v0.55.11.1.

This appears to be a confirmed limitation — there's an open feature request for it: Sorting in Sankey Charts · Issue #62500 · metabase/metabase · GitHub , and a related bug report noting that sorting works in all other chart types but not Sankey.

Thanks for the issue link, I threw an upvote on it.

I guess my example was too simplistic, that or the behavior has changed between Metabase 55 and 58, or both. I’ll have to develop a more complex input set.

After I posted I thought about cases where not all the target values appear in order. The usual solution for that would be a dimension table joined to the data so all the sources & targets are expressed, but if it is as you say and volume plays a part then it may not be possible to perturb the order. More experiments needed!

I fiddled with this over the weekend and was able to trigger the volume-based rearrangement, so I understand what you were talking about. Not sure I can come up with a workaround for that short of digging into the viz code.

It’s trying to be helpful by rearranging things in a more pleasing way, but that’s obviously not useful when the viz demands a strict ordering.