You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算表内各地点间距离并统计符合条件的数量(支持Power BI/Python)

计算地点间距离并统计超阈值数量的解决方案

原始数据

PlaceLatitudeLongitude
A2.31497.6110288
B3.42598.6925504
C4.123199.774072
D5.096466667100.8555936
E6.001016667101.9371152
F6.905566667103.0186368
G7.810116667104.1001584
H8.714666667105.18168
I9.619216667106.2632016
J10.52376667107.3447232
K11.42831667108.4262448
L12.33286667109.5077664
M13.23741667110.589288
N14.14196667111.6708096
O15.04651667112.7523312
P15.95106667113.8338528

需求说明

计算每个地点与其他所有地点的直线距离(采用Haversine公式,单位为公里),统计距离大于指定阈值(示例为151公里)的地点数量,将统计结果作为新列Bigger Than 151添加到表格中,最终输出如下:

PlaceLatitudeLongitudeBigger Than 151
A2.31497.61102883
B3.42598.69255045
C4.123199.7740721
D5.096466667100.85559363
E6.001016667101.93711522
F6.905566667103.01863681
G7.810116667104.10015845
H8.714666667105.181682
I9.619216667106.26320164
J10.52376667107.34472321
K11.42831667108.42624480
L12.33286667109.50776640
M13.23741667110.5892880
N14.14196667111.67080960
O15.04651667112.75233120
P15.95106667113.83385280

实现方案

方案1:Power Query(M语言)

  1. 把原始数据导入Power Query,先确认Latitude和Longitude列是数值类型,不是的话转成数值。
  2. 添加自定义列,命名为Bigger Than 151,粘贴以下M代码:
let
    CurrentPlace = [Place],
    CurrentLat = [Latitude] * Number.PI/180,
    CurrentLon = [Longitude] * Number.PI/180,
    // 计算所有地点与当前地点的距离
    DistanceTable = Table.AddColumn(Source, "Distance", each 
        let
            Lat2 = [Latitude] * Number.PI/180,
            Lon2 = [Longitude] * Number.PI/180,
            DLat = Lat2 - CurrentLat,
            DLon = Lon2 - CurrentLon,
            A = Number.Sin(DLat/2)^2 + Number.Cos(CurrentLat)*Number.Cos(Lat2)*Number.Sin(DLon/2)^2,
            C = 2*Number.Atan2(Number.Sqrt(A), Number.Sqrt(1-A)),
            Distance = 6371*C // 地球半径取6371公里
        in
            Distance
    ),
    // 排除自身,统计距离超151的数量
    Count = List.Count(Table.SelectRows(DistanceTable, each [Place] <> CurrentPlace and [Distance] > 151)[Place])
in
    Count
  1. 关闭Power Query并上载数据,就能看到带新列的表格。

方案2:DAX计算列

  1. 在Power BI模型中选中原始数据表,新建计算列,命名为Bigger Than 151,输入以下DAX公式:
Bigger Than 151 = 
VAR CurrentLat = 'Table'[Latitude] * PI()/180
VAR CurrentLon = 'Table'[Longitude] * PI()/180
VAR DistanceTable = 
    ADDCOLUMNS(
        FILTER('Table', 'Table'[Place] <> EARLIER('Table'[Place])),
        "Distance", 
        6371 * 2 * ATAN2(
            SQRT(
                SIN(([Latitude] * PI()/180 - CurrentLat)/2)^2 + 
                COS(CurrentLat) * COS([Latitude] * PI()/180) * SIN(([Longitude] * PI()/180 - CurrentLon)/2)^2
            ),
            SQRT(1 - (
                SIN(([Latitude] * PI()/180 - CurrentLat)/2)^2 + 
                COS(CurrentLat) * COS([Latitude] * PI()/180) * SIN(([Longitude] * PI()/180 - CurrentLon)/2)^2
            ))
        )
    )
RETURN
    COUNTROWS(FILTER(DistanceTable, [Distance] > 151))
  1. 公式输入完成后,计算列自动生成,表格更新为目标格式。

方案3:Python(Power BI集成)

  1. 进入Power Query,点击「转换」选项卡中的「运行Python脚本」。
  2. 粘贴以下代码(需提前用pip install haversine安装haversine库):
import pandas as pd
from haversine import haversine

df = dataset

def count_over_threshold(row):
    current_coords = (row['Latitude'], row['Longitude'])
    count = 0
    for _, other_row in df.iterrows():
        if row['Place'] == other_row['Place']:
            continue
        other_coords = (other_row['Latitude'], other_row['Longitude'])
        distance = haversine(current_coords, other_coords, unit='km')
        if distance > 151:
            count +=1
    return count

df['Bigger Than 151'] = df.apply(count_over_threshold, axis=1)

print(df)
  1. 运行脚本后,Power Query会加载处理好的数据,关闭并上载即可。

内容的提问来源于stack exchange,提问作者Haduncz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 16:35:23