Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

How to calculate the difference between two points that do not overlap

Hey there community, I have no idea how to search for this problem, so if there is a post out there please let me know as I had no luck.  I basically have data that looks like the below, I basically...
  • DataNinja777's avatar
    1 year ago

    Hi Anonymous ,

     

    To calculate the difference between non-overlapping top and bot elevation points, you need to interpolate one or both datasets so that they align on the same x-axis. Since the x-values are not matched across the two elevation types, you can’t simply subtract [top] - [bot]. Instead, in Power Query, start by extracting a master list of unique x-values across both top and bot data using:

    CombinedX = List.Distinct(YourTable[x-axis])
    

    Then split your original table into two separate queries—one for top and one for bot—like this:

    TopTable = Table.SelectRows(YourTable, each [Point Type] = "top")
    BotTable = Table.SelectRows(YourTable, each [Point Type] = "bot")
    

    Now you need to reindex and interpolate each table to fill in missing x-axis values. To do this, perform a left join from the CombinedX list to each of the top and bot tables. This will give you rows with null elevations where values are missing. Then sort the joined table by x, add an index, and for rows with missing elevations, identify the previous and next known values and interpolate using this formula:

    Interpolated = y1 + (x - x1) * ((y2 - y1) / (x2 - x1))
    

    Where x is the missing point, and (x1, y1) and (x2, y2) are the bounding known data points. You can extract these by grouping and adding custom columns with List.FirstN and List.Skip operations or using buffer + index tricks. Once both top and bot tables are interpolated with the same set of x-values, merge them together on x and compute the difference:

    Delta = [Top Elevation] - [Bot Elevation]
    

    This will give you the vertical gap between the two elevation lines at every point, even when the original data did not overlap. If you're dealing with millions of rows, do this logic inside Power Query rather than DAX to avoid performance bottlenecks.

     

    Best regards,