How do I convert InfluxQL "show series cardinality" into Flux?

I want to convert the below query to Flux - can it be done?

show series exact cardinality from "Measurement" where "TagName" != '' group by "TagName"

@pwnell These types of meta-queries aren’t built directly into Flux yet, but they will be in the future. You could get similar results, but Flux requires a time range, so the cardinality count would be bounded by time.

You would filter the data based on non-null values for TagName using the exists operator. Flux’s from() function outputs series as tables by default, so if you use a selector such as last() or first() to reduce each series to a single row, then ungroup the data to consolidate all the rows into a single table, you can then count() the rows in the table. The _value of the output table would represent the cardinality of series that include the TagName tag within the time range.

from(bucket: "db/rp")
  |> range(start: -7d)
  |> filter(fn: (r) => r._measurement == "Measurement")
  |> filter(fn: (r) => exists r.TagName)
  |> last()
  |> group()
  |> count()

Note: This requires Flux v0.33+.