前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Power BI 无公式实现帕累托图表

Power BI 无公式实现帕累托图表

作者头像
wujunmin
发布于 2025-04-23 03:28:04
发布于 2025-04-23 03:28:04
7900
代码可运行
举报
文章被收录于专栏:wujunminwujunmin
运行总次数:0
代码可运行

帕累托分析(Pareto Analysis),也被称为80/20法则、关键少数法则,是一种常用的管理工具,用于识别和处理影响业务的主要因素。

看到李伟坚老师在Excel使用Vega实现了花式帕累托(参考:Excel 零公式实现高级帕累托图表),我决定在Power BI也试试。只需要将维度和指标拖入Deneb(免费,支持vega)视觉对象:

无需手动计算累计占比,即得到:

原理是vega-lite,维度和指标放入Deneb后,画布上点击...-编辑-空白vega-lite,粘贴以下代码,即可复现:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
{
  "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%:

变为商务一些:

或者调整为纵向:

以上所有效果在下方知识星球分享。知识星球还有对应的帕累托专题,参考:

Power BI帕累托/ABC分析八招

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-04-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 wujunmin 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档