Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Cookie_Dookie
Frequent Visitor

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 need the delta beween top and bottom, so [top] - [bot], the issue is that most of the points are not along the same x data point, so I cant simply do [top] - [bot].

 

Cookie_Dookie_1-1747430757526.png

My thought to get this to work is this to connect each point, and artifically create points (so interpolate), it could be both bot and top or just one of them. With this logic I should be able to do [top] - [bot]. 

 

Cookie_Dookie_2-1747431136289.png

I was thinking of a y = mx approach, for the [bot] line and then I can calculate the intercept between a [top] point and the line [bot]

 

Cookie_Dookie_5-1747431532974.png

I literally have millions of rows, and not sure if this should be a DAX measure (likely Power Query?) and more importantly if it can actually be done?


Data structure looks like:

 

x-axiselevationPoint Type
1082066.225142.5top
1082067.689142top
1082068.641141.95top
1082071.206142.15top
1082071.645141.95top
1082071.868142top
1082073.186142.35top
1082077.261104.35bot
1082078.316226.8top
1082084.402332.8top
1082089.316334top
1082091.509297.1top
1082101.344188.75top
1082105.961134.15bot
1082111.167210.1top
1082111.271134.8bot
1082115.266125.55bot
1082118.76688.5bot
1082120.923177.65top
1082122.42895.75bot
1082130.44782.05bot
1082130.749184.35top
1082134.674184top
1082140.66983.35bot
1082144.411188.65top
1082150.26976.05bot
1082154.233192.25top
1082160.71559.15bot
1082163.982182.2top
1082171.22447.55bot
1082173.503183.95top
1082181.5846.55bot
1082183.262179.8top
1082191.59154.45bot
1082193.012175.5top
1082201.26642.75bot
1082202.758174.35top
1082211.81954.05bot

 

 

 

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @Cookie_Dookie ,

 

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,

View solution in original post

5 REPLIES 5
v-mdharahman
Community Support
Community Support

Hi @Cookie_Dookie,

Thanks for reaching out to the Microsoft fabric community forum.

It looks like you are facing difficulties with calculating the difference between two points. As @DataNinja777 have already responded to your query, kindly go through his response and check if it solves your issue.

 

I would also take a moment to thank @DataNinja777, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.

 

If I misunderstand your needs or you still have problems on it, please feel free to let us know.  

Best Regards,
Hammad.
Community Support Team

 

If this post helps then please mark it as a solution, so that other members find it more quickly.

Thank you.

Hi @Cookie_Dookie,

As we haven’t heard back from you, so just following up to our previous message. I'd like to confirm if you've successfully resolved this issue or if you need further help.

If yes, you are welcome to share your workaround and mark it as a solution so that other users can benefit as well. If you find a reply particularly helpful to you, you can also mark it as a solution.


If you still have any questions or need more support, please feel free to let us know. We are more than happy to continue to help you.
Thank you for your patience and look forward to hearing from you.

Hi @Cookie_Dookie,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution so that other community members can find it easily.


Thank you.

Yes I did!  I have millions of rows so I ran it via Power Query, and it definately worked. Slightly different variant for the dataset I have.

Cookie_Dookie_0-1748622937917.png

The code I used:

let
    // 1. Load your data
    Source = #"Elevation Data",

    // 2. Separate top (top) and bot (bot) points
    topPoints = Table.SelectRows(Source, each [Elevation Type] = "top"),
    botPoints = Table.SelectRows(Source, each [Elevation Type] = "bot"),

    // 3. Sort bot points by X_AXIS_M (ascending)
    botPointsSorted = Table.Sort(botPoints, {{"X_AXIS_M", Order.Ascending}}),

    // 4. Function to interpolate bot elevation at a given X_AXIS_M
    Interpolatebot = (x as number) as nullable number =>
        let
            // Find all bot points with X_AXIS_M less than or equal to x
            LowerRows = Table.SelectRows(botPointsSorted, each [X_AXIS_M] <= x),
            // Find all bot points with X_AXIS_M greater than x
            UpperRows = Table.SelectRows(botPointsSorted, each [X_AXIS_M] > x),
            // Get the last lower point
            Lower = if Table.RowCount(LowerRows) > 0 then LowerRows{Table.RowCount(LowerRows)-1} else null,
            // Get the first upper point
            Upper = if Table.RowCount(UpperRows) > 0 then UpperRows{0} else null,
            // If both points exist, interpolate
            Interpolated = if Lower <> null and Upper <> null and Lower[X_AXIS_M] <> Upper[X_AXIS_M]
                then
                    Lower[elevation] + (x - Lower[X_AXIS_M]) * (Upper[elevation] - Lower[elevation]) / (Upper[X_AXIS_M] - Lower[X_AXIS_M])
                else null
        in
            Interpolated,

    // 5. Add interpolated bot elevation to each top point
    topWithbot = Table.AddColumn(
        topPoints,
        "Interpolated bot",
        each Interpolatebot([X_AXIS_M]),
        type number
    ),

    // 6. Calculate Delta (top - Interpolated bot)
    Result = Table.AddColumn(
        topWithbot,
        "Delta",
        each [elevation] - [Interpolated bot],
        type number
    ),
    #"Filtered Rows" = Table.SelectRows(Result, each [Interpolated bot] <> null and [Interpolated bot] <> "")
in
    #"Filtered Rows"



DataNinja777
Super User
Super User

Hi @Cookie_Dookie ,

 

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,

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.