Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
I have an task for something really challenging. The idea is to execute the logic described in this link.
Given the following tables:
table "points"
table "polygons"
which represents the following plane:
Find out if the point is inside any polygon, and which polygon it would be. I believe the logic would be something like:
So it would be a main iteration (over the point table) with two nested iterations (one for each polygon, and one for each vertex of that polygon). In the iteration of the vertices, the main calculation is performed.
The main calculation is based on the orientation concept for three ordered points.
Basically, four coordinates are used in this main calculation: [X, Y] from the point being iterated (P), [X, Y] from vertex n of the polygon (Vn) and [X, Y] from vertex n+1 ( Vn+1). In the last vertex iteration, the next vertex to be considered will be the first vertex. The fourth coordinate would be the X value of the point being iterated, and the Y with the value of 1000 (we'll call it Q).
The calculation is four repetitions of the following formula (just changing the variables):
((A[Y] - B[Y]) * (C[X] - A[X])) - ((A[X] - B[X]) * (C[Y] - A[Y]))
Each repetition can be called by On (O1, O2, O3 and O4).
The variables of each repetition are:
O1
A = Vn
B = Vn+1
C = P
O2
A = Vn
B = Vn+1
C = Q
O3
A = P
B = Q
C = Vn
O4
A = P
B = Q
C = Vn+1
The condition that will assert that a given point is inside the iterated polygon will be when any of the following is true, at the end of any iteration over any of the vertices:
The verification that returns true for ON_SEGMENT is:
B[x] <= max({A[X], C[X]}) &&
B[x] >= min({A[X], C[X]}) &&
B[Y] <= max({A[Y], C[Y]}) &&
B[Y] >= min({A[Y], C[Y]})
On getting true for the above condition, 1 is added to a counter. At the end of the iteration over all vertices of the polygon, if the counter value is odd then the point is inside the polygon.
I know it's a lot but the concept can be better understood in the link I provided above. The implementation of this calculation within Power Query would be very interesting, as Power BI does not have any native tool that performs the intersection of points and polygons distributed geographically and could open doors to even more robust analysis. Anyone accept the challenge?
Hi @FranciscoHoff ,
Hope the following similar blogs/videoes helps you. List.Generate() might be a good way.
Easy Looping in Power BI/Power Query - YouTube
List.Generate() and Looping in PowerQuery - Exceed
Iteration in DAX - Microsoft Power BI Community
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
100 | |
51 | |
19 | |
12 | |
11 |