源自知识星球星友遇到的一个对比问题,本文使用一个度量值实现元素间两两对比,主要用来和优秀对标。示例是零售店铺之间的差异。
这是一个普通的条形图:
选中任意两个店铺,可以突出对比两个店铺之间的差异百分比:
选中三个或者以上店铺,可以从高到低两两对比:
差异的标签可以自定义,比方换为绝对值:
差异也可以使用不同颜色的条形突出显示:
以下视频是详细的操作演示:
以下是率值差异的度量值,使用说明见注释:
代码语言:javascript复制SVG条形图-自定义比较项目-变化率 =
//替换原则:把本度量值中的店铺资料表换成你的维度表,[店铺名称]换成你的维度,[M.销售业绩]换成你的度量值即可复用。
//替换快捷方式:把本图表完整复制到记事本,查找替换,再复制回Power BI。
//公众号、知乎、B站:wujunmin
VAR Text_Width = 50 //类别标签50像素宽,安装类别文字长度按需设置
VAR Bar_Width = 100 //条形100像素宽
VAR Bar_Height = 12 //条形实际高度12像素
VAR Bar_Height_Space = Bar_Height 6 //上下间距18像素
VAR Line_Width = Bar_Width 20 //直线长度120像素
VAR Circle_R = Bar_Height / 2 //对比数据标签因此的圆圈半径6像素
VAR Text_Size = 6 //标签字号
VAR t =
ADDCOLUMNS ( ALL ( '店铺资料' ), "Index", RANKX ( ALL ( '店铺资料' ), [M.销售业绩] ) ) //完整的表
VAR ItemCount =
COUNTROWS ( t )
VAR MaxValue =
MAXX ( t, [M.销售业绩] )
VAR BarTable =
ADDCOLUMNS (
t,
"Store",
//画占比数据标签 公众号、知乎、B站:wujunmin
"<text x='" & Text_Width - 1 & "' y='" & ( [Index] - 1 ) * Bar_Height_Space Bar_Height / 2 & "' text-anchor='end' dominant-baseline='middle' font-size='" & Text_Size & "' id='wujunmin'>" & [店铺名称] & "</text>",
"Rect",
//画条形
"<rect x='" & Text_Width & "' y='" & ( [Index] - 1 ) * Bar_Height_Space & "' width='" & Bar_Width * [M.销售业绩] / MaxValue & "' height='" & Bar_Height & "' fill='DarkCyan'/>",
"Text",
//画数据标签
"<text x='" & Text_Width 1 & "' y='" & ( [Index] - 1 ) * Bar_Height_Space Bar_Height / 2 & "' text-anchor='start' dominant-baseline='middle' font-size='" & Text_Size & "' >"
& ROUND ( [M.销售业绩] / 1000, 0 ) & "</text>"
)
VAR t_join =
NATURALINNERJOIN ( '店铺资料', t ) //交集的表
VAR t_comp =
ADDCOLUMNS ( t_join, "Comp_Index", RANKX ( t_join, [Index],, ASC ) ) //加新索引
VAR Bar_Table_Comp =
ADDCOLUMNS (
t_comp,
"Line",
//画框
"<line x1='" & Text_Width & "' y1='" & ( [Index] - 1 ) * Bar_Height_Space Bar_Height / 2 & "' x2='" & Line_Width Text_Width & "' y2='" & ( [Index] - 1 ) * Bar_Height_Space Bar_Height / 2 & "' stroke='Tomato' stroke-width='0.5' id='wujunmin'/>"
& IF (
[Index] = MINX ( t_comp, [Index] ),
"<line x1='" & Line_Width Text_Width & "' y1='" & ( [Index] - 1 ) * Bar_Height_Space Bar_Height / 2 & "' x2='" & Line_Width Text_Width & "' y2='"
& ( [Index] - 1 ) * Bar_Height_Space Bar_Height / 2
Bar_Height_Space
* ( MAXX ( t_comp, [Index] ) - [Index] ) & "' stroke='Tomato' stroke-width='0.5'/>",
BLANK ()
),
"Comp",
//画对比标签 公众号、知乎、B站:wujunmin
IF (
MAXX ( t_comp, [Index] ) = [Index],
BLANK (),
"<circle cx='" & Line_Width Text_Width & "' cy='"
& ( [Index] - 1 ) * Bar_Height_Space Bar_Height / 2
(
MAXX ( FILTER ( t_comp, EARLIER ( [Comp_Index] ) = [Comp_Index] - 1 ), [Index] ) - [Index]
) * Bar_Height_Space / 2 & "' r='" & Circle_R & "' fill='white' fill-opacity='1'/>
<text x='"& Line_Width Text_Width & "' y='"
& ( [Index] - 1 ) * Bar_Height_Space Bar_Height / 2
(
MAXX ( FILTER ( t_comp, EARLIER ( [Comp_Index] ) = [Comp_Index] - 1 ), [Index] ) - [Index]
) * Bar_Height_Space / 2 & "' text-anchor='middle' dominant-baseline='middle' font-size='" & Text_Size & "' >"
& FORMAT (
DIVIDE (
[M.销售业绩]
- SUMX (
FILTER ( t_comp, EARLIER ( [Comp_Index] ) = [Comp_Index] - 1 ),
[M.销售业绩]
),
SUMX (
FILTER ( t_comp, EARLIER ( [Comp_Index] ) = [Comp_Index] - 1 ),
[M.销售业绩]
)
),
"0%"
) & "</text>"
)
)
VAR Bar =
IF (
COUNTROWS ( t_comp ) > 1,
CONCATENATEX ( Bar_Table_Comp, [Line] & [Comp] ),
BLANK ()
)
& CONCATENATEX ( BarTable, [Store] & [Rect] & [Text] )
VAR SVG = "data:image/svg xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='" & Text_Width Line_Width Circle_R 3 & "' height='" & Bar_Height_Space * ItemCount & "'>" & Bar & "</svg>"
//预留3个像素的宽度,作为可能的数据标签溢出空间
RETURN
SVG