Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Pivot table download should respect currency formatting (#75969)

Nathan Voxland58819b5ea2d7
diff --git a/src/metabase/formatter/impl.clj b/src/metabase/formatter/impl.clj
index 06b879f52c5..36559d54dd0 100644
--- a/src/metabase/formatter/impl.clj
+++ b/src/metabase/formatter/impl.clj
@@ -323,13 +323,23 @@
(fn [column]
(create-formatter timezone column settings format-rows?))))
+(defn- pivot-inline-currency-col
+ "Pivot exports render the currency symbol in the cell (matching the in-app pivot table), since pivot measures have no
+ column header to carry it. Express that by forcing `currency-in-header` off for currency columns -- the
+ override goes on the column's `:settings`, which wins [[number-formatter]]'s settings merge. Scoped to currency
+ columns so non-currency columns are untouched."
+ [col settings]
+ (cond-> col
+ (streaming.common/currency-settings? (streaming.common/viz-settings-for-col col settings))
+ (assoc-in [:settings :currency-in-header] false)))
+
(defn- create-formatters
"Value formatters for the columns at `indexes`; each formats a raw value via
[[metabase.query-processor.streaming.common/format-value]] then the column's formatter."
[columns indexes timezone settings format-rows?]
(let [formatter-fn (get-formatter timezone settings format-rows?)]
(mapv (fn [idx]
- (let [formatter (formatter-fn (nth columns idx))]
+ (let [formatter (formatter-fn (pivot-inline-currency-col (nth columns idx) settings))]
(fn [value]
(formatter (streaming.common/format-value value)))))
indexes)))
diff --git a/src/metabase/query_processor/streaming/xlsx.clj b/src/metabase/query_processor/streaming/xlsx.clj
index 9b7fa6e6a7a..10ab1fbc3eb 100644
--- a/src/metabase/query_processor/streaming/xlsx.clj
+++ b/src/metabase/query_processor/streaming/xlsx.clj
@@ -281,10 +281,17 @@
(.setDataFormat (. data-format getFormat ^String format-string))))
(defn- compute-column-cell-styles
- "Compute a sequence of cell styles for each column"
- [^Workbook workbook ^DataFormat data-format viz-settings cols format-rows?]
+ "Compute a sequence of cell styles for each column. When `inline-currency?` is true, currency columns render the
+ symbol in the cell (used for pivot exports, whose measures have no column header to carry it) -- expressed by
+ forcing `currency-in-header` off for the column."
+ [^Workbook workbook ^DataFormat data-format viz-settings cols format-rows? inline-currency?]
(for [col cols]
- (let [settings (streaming.common/viz-settings-for-col col viz-settings)
+ (let [base-settings (streaming.common/viz-settings-for-col col viz-settings)
+ ;; Scope the override to currency columns only -- injecting `currency-in-header` into a non-currency
+ ;; column's settings would perturb the number-formatting heuristics that inspect the setting keys.
+ settings (cond-> base-settings
+ (and inline-currency? (streaming.common/currency-settings? base-settings))
+ (assoc ::mb.viz/currency-in-header false))
format-strings (format-settings->format-strings settings col format-rows?)]
(when (seq format-strings)
(mapv
@@ -656,9 +663,10 @@
:val-formatters (create-formatters cell-styles cols val-indexes timezone settings format-rows?)})
(defn- generate-styles
- [workbook viz-settings non-pivot-cols format-rows?]
+ [workbook viz-settings non-pivot-cols format-rows? & {:keys [inline-currency?]}]
(let [data-format (. ^SXSSFWorkbook workbook createDataFormat)]
- {:cell-styles (compute-column-cell-styles workbook data-format viz-settings non-pivot-cols format-rows?)
+ {:cell-styles (compute-column-cell-styles workbook data-format viz-settings non-pivot-cols format-rows?
+ inline-currency?)
:typed-cell-styles (compute-typed-cell-styles workbook data-format format-rows?)}))
(defmethod qp.si/streaming-results-writer :xlsx
@@ -726,7 +734,7 @@
{:keys [pivot-rows pivot-cols pivot-measures]} pivot-export-options
{:keys [cell-styles typed-cell-styles]}
- (generate-styles workbook settings non-pivot-cols format-rows?)
+ (generate-styles workbook settings non-pivot-cols format-rows? :inline-currency? true)
formatters (make-formatters cell-styles
non-pivot-cols
diff --git a/test/metabase/api/downloads_exports_test.clj b/test/metabase/api/downloads_exports_test.clj
index 47c57d4c3b3..9bb17c228e7 100644
--- a/test/metabase/api/downloads_exports_test.clj
+++ b/test/metabase/api/downloads_exports_test.clj
@@ -403,6 +403,55 @@
((fn [m] (update-vals m #(into #{} (mapv first %)))))
(apply concat)))))))))))
+(deftest pivot-export-currency-in-header-default-test
+ (testing "Currency symbols are shown inline in pivot exports even with the default `currency_in_header` (UXW-4499).
+ Pivot measures have no column header to carry the symbol, matching the in-app pivot table."
+ (mt/dataset test-data
+ (mt/with-temp [:model/Card card
+ {:display :pivot
+ :visualization_settings {:pivot_table.column_split
+ {:rows ["CATEGORY"]
+ :columns ["CREATED_AT"]
+ :values ["sum"]}
+ ;; note: no `:currency_in_header false` here -- the default leaves the
+ ;; currency symbol in the header for regular tables, but pivot exports
+ ;; should still show it inline.
+ :column_settings
+ {"[\"name\",\"sum\"]" {:number_style "currency"}}
+ :pivot.condense_duplicate_totals true}
+ :dataset_query (mt/mbql-query products
+ {:aggregation [[:sum $price]]
+ :breakout [$category
+ !year.created_at]})}]
+ (testing "csv"
+ (is (= [["Category" "2016" "2017" "2018" "2019" "Row totals"]
+ ["Doohickey" "$632.14" "$854.19" "$496.43" "$203.13" "$2,185.89"]
+ ["Gadget" "$679.83" "$1,059.11" "$844.51" "$435.75" "$3,019.20"]
+ ["Gizmo" "$529.70" "$1,080.18" "$997.94" "$227.06" "$2,834.88"]
+ ["Widget" "$987.39" "$1,014.68" "$912.20" "$195.04" "$3,109.31"]
+ ["Grand totals" "$2,829.06" "$4,008.16" "$3,251.08" "$1,060.98" "$11,149.28"]]
+ (card-download card {:export-format :csv :format-rows true :pivot true}))))
+ (testing "xlsx"
+ ;; The pivot measure cells get a currency data format (`[$$]#,##0.00`), which Excel renders with the
+ ;; symbol. (POI's test DataFormatter renders the `[$$]` marker literally, so we assert on the format string.)
+ (let [result (mt/user-http-request :crowberto :post 200
+ (format "card/%d/query/xlsx" (:id card))
+ {:format_rows true :pivot_results true})
+ measure-formats (with-open [in (io/input-stream result)]
+ (let [sheet (->> (spreadsheet/load-workbook in)
+ (spreadsheet/select-sheet "Query result"))]
+ (->> (spreadsheet/row-seq sheet)
+ (drop 1) ;; skip header row
+ (mapcat (fn [r] (->> (spreadsheet/cell-seq r)
+ (drop 1) ;; skip the row-dimension cell
+ (keep (fn [c]
+ (some-> (.getCellStyle ^org.apache.poi.ss.usermodel.Cell c)
+ (.getDataFormatString)))))))
+ distinct
+ vec)))]
+ (is (every? #(str/includes? % "[$$]") measure-formats))
+ (is (seq measure-formats))))))))
+
(deftest ^:mb/driver-tests simple-pivot-with-sum-and-average-export-test
(testing "Pivot table exports look pivoted and can have multiple measures aggregated properly."
(mt/test-drivers #{:h2 :postgres}
diff --git a/test/metabase/formatter/impl_test.clj b/test/metabase/formatter/impl_test.clj
index b7eda5c86fe..e4b916a0ee1 100644
--- a/test/metabase/formatter/impl_test.clj
+++ b/test/metabase/formatter/impl_test.clj
@@ -284,3 +284,20 @@
(testing "Should use column-specific GBP currency, not global AUD currency"
(is (str/starts-with? result "£")
(str "Expected GBP symbol (£) but got: " result))))))
+
+(deftest pivot-make-formatters-inlines-currency-test
+ (testing "Pivot value formatters render the currency symbol inline even when currency-in-header is the default (UXW-4499).
+ In-app pivot tables always show the currency symbol in the cell because measures have no column header to carry it."
+ (let [col {:name "sum" :base_type :type/Float :effective_type :type/Float :field_ref [:aggregation 0]}]
+ (doseq [[label viz] [["currency-in-header unset (default)"
+ {::mb.viz/column-settings
+ {{::mb.viz/column-name "sum"} {::mb.viz/number-style "currency"
+ ::mb.viz/currency "USD"}}}]
+ ["currency-in-header explicitly true"
+ {::mb.viz/column-settings
+ {{::mb.viz/column-name "sum"} {::mb.viz/number-style "currency"
+ ::mb.viz/currency "USD"
+ ::mb.viz/currency-in-header true}}}]]]
+ (testing label
+ (let [val-fmt (first (:val-formatters (formatter/make-formatters [col] [] [] [0] viz nil true)))]
+ (is (= "$632.14" (str (val-fmt 632.14))))))))))