是否可以在 Deneb 中格式化一个值(我正在使用 Vega-Lite),将 1.000 更改为“1k”或 1.000.000“1M”,并添加该值最多有两位小数 ... “文本”: { “字段”:“值”, “类型”:“定量”, “聚合”:“总和”, “格式”:“,~s” }, ...
例如:1.789.979,32323 至 1,79M
请参阅 Deneb 文档,了解如何使用 Power BI 的自定义格式字符串,它比 D3/Vega 语法好得多。
"x": {
"field": "$ Sales",
"type": "quantitative",
"axis": {
"format": "$#0,,.00M",
"formatType": "pbiFormat"
}
}
如果您只需要 K 或 M 而不是两者都需要,那么 D3/Vega 语法还不错。这是 K 和 1 位小数的示例。
"transform": [
{"calculate": "datum.b/1000", "as": "b"}
],
...
"y": {"field": "b", "type": "quantitative", "axis": {"title": "b (in thousands)"}}
...
"text": {"field": "b", "type": "quantitative", "format": ",.1~f"}
您可以在此处阅读有关 D3 格式化语法的更多信息。
如果您需要根据数字选择 K 或 M 的“自动”格式,您可以使用 DAX 将值创建为文本并将其传递给 Deneb。
Format Actual =
VAR _number = SUM(KPIsDATA[Actual])
VAR _digits = 2
RETURN IF( ABS(_number) >= 1000000000, FORMAT(_number, "0,,," & IF(_digits > 0, ".", "") & REPT("0",_digits) & "B"),
IF( ABS(_number) >= 1000000, FORMAT(_number, "0,," & IF(_digits > 0, ".", "") & REPT("0",_digits) & "M"),
IF( ABS(_number) >= 1000, FORMAT(_number, "0," & IF(_digits > 0, ".", "") & REPT("0",_digits) & "K"),
FORMAT(_number, "0" & IF(_digits > 0, ".", "") & REPT("0",_digits) )
)
)
)
有关此内容的更多信息,请参阅我的回答。
要在 Vega 中执行自动格式化,请尝试以下操作:
"transform": [
{"calculate": "if(abs(datum.b)>1000000000, round(datum.b/10000000)/100+'B',if(abs(datum.b)>1000000, round(datum.b/10000)/100+'M',if(abs(datum.b)>1000, round(datum.b/10)/100+'k',datum.b)))", "as": "b-label"}
],