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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
rohit91
New Member

Customer's Nearest Neighbours by Revenue

Hi Experts!!

 

I've one question regarding finding nearest 4 neighbours of Customer by revenue and displaying them in the same text box.

It could be neighbour who have revenue just more than the selected customer or could be less than selected.

Ideal is to have 2 neighbours having higher revenue and other 2 having lower revenue. Sample data is provided below


Example 1
Selection of Cust C value should yield below as Neighbours
Cust A, Cust B, Cust D and Cust E

 

Example 2
Selection of Cust B should yield below as Neighbours
Cust A, Cust C, Cust D and Cust E

 

Example 3
Selection of Cust E should yield below are Neighbours
Cust C, Cust D, Cust F and Cust G

 

Sample Data is as below. Again stating we need to use Revenue to identify the nearest neighbours

 

Customer IDCustomer NameRevenue
1Cust A1000
2Cust B900
3Cust C800
4Cust D700
5Cust E600
6Cust F500
7Cust G400
8Cust H300
9Cust I200

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi, 

I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_1-1725817750101.png

 

 

Jihwan_Kim_0-1725817736192.png

 

WINDOW function (DAX) - DAX | Microsoft Learn

 

expected result select: =
VAR _Slicer =
    MAX ( 'Customer slicer'[Customer Name] )
VAR _T =
    ADDCOLUMNS (
        ALL ( Data[Customer ID], Data[Customer Name] ),
        "@Revenue", [Revenue total:],
        "@SelectedRevenue", CALCULATE ( [Revenue total:], Data[Customer Name] = _Slicer, REMOVEFILTERS () )
    )
VAR _Diff =
    FILTER (
        ADDCOLUMNS ( _T, "@Diff", ABS ( [@Revenue] - [@SelectedRevenue] ) ),
        Data[Customer Name] <> _Slicer
    )
VAR _NearestCustomer =
    SUMMARIZE (
        WINDOW ( 1, ABS, 4, ABS, _Diff, ORDERBY ( [@Diff], ASC ) ),
        Data[Customer Name]
    )
RETURN
    CONCATENATEX (
        FILTER ( Data, Data[Customer Name] IN _NearestCustomer ),
        Data[Customer Name],
        " ,"
    )

 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

View solution in original post

1 REPLY 1
Jihwan_Kim
Super User
Super User

Hi, 

I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_1-1725817750101.png

 

 

Jihwan_Kim_0-1725817736192.png

 

WINDOW function (DAX) - DAX | Microsoft Learn

 

expected result select: =
VAR _Slicer =
    MAX ( 'Customer slicer'[Customer Name] )
VAR _T =
    ADDCOLUMNS (
        ALL ( Data[Customer ID], Data[Customer Name] ),
        "@Revenue", [Revenue total:],
        "@SelectedRevenue", CALCULATE ( [Revenue total:], Data[Customer Name] = _Slicer, REMOVEFILTERS () )
    )
VAR _Diff =
    FILTER (
        ADDCOLUMNS ( _T, "@Diff", ABS ( [@Revenue] - [@SelectedRevenue] ) ),
        Data[Customer Name] <> _Slicer
    )
VAR _NearestCustomer =
    SUMMARIZE (
        WINDOW ( 1, ABS, 4, ABS, _Diff, ORDERBY ( [@Diff], ASC ) ),
        Data[Customer Name]
    )
RETURN
    CONCATENATEX (
        FILTER ( Data, Data[Customer Name] IN _NearestCustomer ),
        Data[Customer Name],
        " ,"
    )

 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.