Skip to content

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

Handle nil subtotal nodes (#74605)

metamben94757c098c9f
diff --git a/src/metabase/pivot/core.cljc b/src/metabase/pivot/core.cljc
index a928ab36428..c44f65be6a3 100644
--- a/src/metabase/pivot/core.cljc
+++ b/src/metabase/pivot/core.cljc
@@ -366,8 +366,12 @@
(defn- create-subtotal-node
"Creates a subtotal node for the given row item."
[row-item]
- (let [subtotal-val (or (get-in row-item [:value :xlsx-formatted-value])
- (:value row-item))]
+ (let [v (:value row-item)
+ ;; In XLSX exports, :value is the formatter map {:col, :value, :xlsx-formatted-value, :styles}.
+ ;; Elsewhere (CSV, FE) it's the formatted value directly. (#71220)
+ subtotal-val (if (and (map? v) (contains? v :xlsx-formatted-value))
+ (or (:xlsx-formatted-value v) (:value v) (i18n/tru "(empty)"))
+ (if (some? v) v (i18n/tru "(empty)")))]
{:value (i18n/tru "Totals for {0}" subtotal-val)
:rawValue (:rawValue row-item)
:span 1
diff --git a/test/metabase/pivot/core_test.cljc b/test/metabase/pivot/core_test.cljc
index 860ecbe6bad..fdca74f9250 100644
--- a/test/metabase/pivot/core_test.cljc
+++ b/test/metabase/pivot/core_test.cljc
@@ -221,6 +221,27 @@
{:value "200%" :isSubtotal true :custom "attr"}]
result)))))
+(deftest create-subtotal-node-test
+ (testing "labels a plain formatted value (CSV / FE path)"
+ (is (= "Totals for BA"
+ (:value (#'pivot/create-subtotal-node {:value "BA" :rawValue "BA"})))))
+ (testing "labels an XLSX formatter-map value using its formatted text"
+ (is (= "Totals for engineer"
+ (:value (#'pivot/create-subtotal-node
+ {:value {:col {:name "bio"}
+ :value "engineer"
+ :xlsx-formatted-value "engineer"
+ :styles nil}
+ :rawValue "engineer"})))))
+ (testing "renders nil breakout values as (empty) instead of dumping the formatter map (#71220)"
+ (is (= "Totals for (empty)"
+ (:value (#'pivot/create-subtotal-node
+ {:value {:col {:name "bio"}
+ :value nil
+ :xlsx-formatted-value nil
+ :styles nil}
+ :rawValue nil}))))))
+
(deftest build-pivot-trees-test
(testing "build-pivot-trees correctly builds basic row and column tree structures"
(let [rows [[1 "A" "Y" 0 10]