帕累托分析(Pareto Analysis),也被称为80/20法则、关键少数法则,是一种常用的管理工具,用于识别和处理影响业务的主要因素。
看到李伟坚老师在Excel使用Vega实现了花式帕累托(参考:Excel 零公式实现高级帕累托图表),我决定在Power BI也试试。只需要将维度和指标拖入Deneb(免费,支持vega)视觉对象:
无需手动计算累计占比,即得到:
原理是vega-lite,维度和指标放入Deneb后,画布上点击...-编辑-空白vega-lite,粘贴以下代码,即可复现:
{
"data": {"name": "dataset"},
"transform": [
{
"window": [{"op": "sum", "field": "KPI", "as": "total"}],
"frame": [null, null]
},
{
"window": [
{"op": "sum", "field": "KPI", "as": "cumulative_sum"},
{"op": "rank", "as": "rank"}
],
"sort": [{"field": "KPI", "order": "descending"}],
"frame": [null, 0]
},
{
"calculate": "datum.cumulative_sum/datum.total",
"as": "cumulative_percent"
}
],
"layer": [
{
"mark": {
"type": "bar",
"opacity": 0.3,
"tooltip": true,
"cornerRadius": 100,
"cornerRadiusEnd": 100,
"color": "lightgray",
"size": 12
},
"encoding": {
"y": {
"field": "KPI",
"type": "quantitative",
"axis": null
}
}
},
{
"mark": {
"type": "bar",
"tooltip": true,
"cornerRadius": 100,
"cornerRadiusEnd": 100,
"color": "deepskyblue",
"size": 25
},
"encoding": {
"y": {
"field": "KPI__highlight",
"type": "quantitative",
"axis": null
},
"opacity": {
"condition": {
"test": {"field": "__selected__", "equal": "off"},
"value": 0
},
"value": 1
}
}
},
{
"mark": {
"type": "line",
"tooltip": true,
"color": "gray",
"strokeWidth": 1.5,
"interpolate": "monotone"
},
"encoding": {
"y": {
"field": "cumulative_percent",
"type": "quantitative",
"axis": null,
"scale": {"domain": [0, 1]}
}
}
},
{
"mark": {
"type": "point",
"tooltip": true,
"color": "tomato",
"size": 60,
"filled": true,
"opacity": 1
},
"encoding": {
"y": {
"field": "cumulative_percent",
"type": "quantitative",
"scale": {"domain": [0, 1]}
}
}
},
{
"mark": {
"type": "text",
"align": "center",
"baseline": "bottom",
"dy": -5,
"color": "black",
"fontSize": 14
},
"encoding": {
"y": {
"field": "KPI__highlight",
"type": "quantitative",
"axis": null
},
"text": {
"field": "KPI__highlight",
"format": ".0f"
},
"opacity": {
"condition": {
"test": {"field": "__selected__", "equal": "off"},
"value": 0
},
"value": 1
}
}
},
{
"mark": {
"type": "text",
"align": "center",
"baseline": "middle",
"dy": -10,
"dx": 0,
"color": "gray",
"fontSize": 12,
"fontWeight": "bold"
},
"encoding": {
"y": {
"field": "cumulative_percent",
"type": "quantitative"
},
"text": {
"field": "cumulative_percent",
"format": ".1%"
}
}
}
],
"encoding": {
"x": {
"field": "城市",
"type": "nominal",
"axis": {
"title": null,
"labelFontSize": 14,
"labelAngle": 0,
"labelPadding": 10
},
"sort": {
"field": "KPI",
"order": "descending"
}
}
},
"resolve": {
"scale": {
"y": "independent"
}
},
"config": {
"axis": {
"grid": false,
"domain": false,
"labels": true,
"ticks": false,
"title": null
},
"axisY": {
"labels": false,
"title": null
},
"view": {
"stroke": "transparent"
}
}
}
格式在此基础上可以调整,比如突出前80%:
变为商务一些:
或者调整为纵向:
以上所有效果在下方知识星球分享。知识星球还有对应的帕累托专题,参考:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有