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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

How to get closest Warehouse for each Sales Order?

Hello Friends, 
I am not sure this is right place to ask, but I need an idea or help on how to do this. I am looking to create an Inventory report which gives me the closest warehouse to my sales order. Sales orders are coming around the US, but I have only 3 warehouses now. 
 
All I need to show is which warehouse is closest to sales order zip code. 
 
I have 2 main tables.  I can add another table with zip codes for all the state in US, but I am not sure how to achive it. Any  idea will be really appricated. (If I can make it dynamic, that will be ideal. When we add new warehouse it automatically adjust)
1.PNG

 Thank you so much

 

 
 
1 ACCEPTED SOLUTION

hi @Anonymous - You would need to bring in another table which has distance between possible sales order locations and each warehouse - so in your case each potential location of sales will have 3 records in this "distance" table

You can then rank the distance for each sales order location and then display warehouse for sales order where rank = 1  

Rank-Distance =

VAR _Rnk =
RANKX (
FILTER (
'Warehouse-Dist',
'Warehouse-Dist'[Sales Loc] = EARLIER ( 'Warehouse-Dist'[Sales Loc] )
&& 'Warehouse-Dist'[Distance] <> BLANK ()
),
'Warehouse-Dist'[Distance],
,
ASC,
DENSE
)
RETURN
IF ( 'Warehouse-Dist'[Distance] = BLANK (), BLANK (), _Rnk )

 

Sumanth_23_0-1601101817961.png

Please mark the post as a solution and provide a 👍 if my comment helped with solving your issue. Thanks!

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!

Proud to be a Super User!



View solution in original post

14 REPLIES 14
v-diye-msft
Community Support
Community Support

Hi @Anonymous 

 

If the above posts help, please kindly mark it as a answer to help others find it more quickly. thanks!

If not, please kindly elaborate more.

 

 

Community Support Team _ Dina Ye
If this post helps, then please consider Accept it as the solution to help the other members find it more
quickly.
Ashish_Mathur
Super User
Super User

Hi,

I am confused.  What exact result are you expecting?  How does one determine the "closest warehouse to a sales order"?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @Ashish_Mathur 

 

Thank you, the only thing I have in my mind is distance calculated by zip code  for each warehouse and sales order zip code, then get the lowest or manually create 3 groups of zip codes that are closed to each warehouse. 
 
Again, I am not sure the above may not be possible. I just want some expert idea how to do this. I never face this kinda problem. So kinda brainstorming ideas, how to do this easiest possible way. Please let me know if you know any other easy way. 
 
Note -  I have added another table with expected results. 
 
Thank you so much

hi @Anonymous - You would need to bring in another table which has distance between possible sales order locations and each warehouse - so in your case each potential location of sales will have 3 records in this "distance" table

You can then rank the distance for each sales order location and then display warehouse for sales order where rank = 1  

Rank-Distance =

VAR _Rnk =
RANKX (
FILTER (
'Warehouse-Dist',
'Warehouse-Dist'[Sales Loc] = EARLIER ( 'Warehouse-Dist'[Sales Loc] )
&& 'Warehouse-Dist'[Distance] <> BLANK ()
),
'Warehouse-Dist'[Distance],
,
ASC,
DENSE
)
RETURN
IF ( 'Warehouse-Dist'[Distance] = BLANK (), BLANK (), _Rnk )

 

Sumanth_23_0-1601101817961.png

Please mark the post as a solution and provide a 👍 if my comment helped with solving your issue. Thanks!

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!

Proud to be a Super User!



Anonymous
Not applicable

Hi @Sumanth_23  ,

 

I am getting an error. I know it something on my side. I am not familer with funtion Power BI. Can you please tell me what I am doing wrong here? 

 

Rank_Distance = 
    VAR_Rnk =
        RANKX(
        FILTER(
            Main, 
            Main[Ship_City] = EARLIER(Main[Ship_City]) 
            && Main[Distance] <> BLANK()
            ),
        Main[Distance],
        ASC, 
        DENSE)
    RETURN
        IF(Main[Distance] = BLANK(), BLANK(),_Rnk)

 

Error - The syntax for 'RETURN' is incorrect. (DAX( VAR_Rnk = RANKX( FILTER( Main, Main[Ship_City] = EARLIER(Main[Ship_City]) && Main[Distance] <> BLANK() ), Main[Distance], ASC, DENSE) RETURN IF(Main[Distance] = BLANK(), BLANK(),_Rnk))).

 

Sample data of my table

Code = Warehouse
Shipcity = Sales Loc

Capture.PNG

Thank you so much for your help and time! 

hi @Anonymous - There were a couple of syntax errors but on fixing them the column is calculating as expected 

1. There is SPACE between VAR & _Rnk on line 2

2. There is an additional "," before "ASC,"

Rank_Distance =
VAR _Rnk =
RANKX(
FILTER(
'Warehouse-Distance',
'Warehouse-Distance'[Ship_City]= EARLIER('Warehouse-Distance'[Ship_City])
&& 'Warehouse-Distance'[Distance] <> BLANK()
),
'Warehouse-Distance'[Distance],
,
ASC,
DENSE)
RETURN
IF('Warehouse-Distance'[Distance] = BLANK(), BLANK(), _Rnk)
Item Rank2.png
Please mark the post as a solution and provide a 👍 if my comment helped with solving your issue. Thanks!
 
Did I answer your question? Mark my post as a solution! Appreciate your Kudos!

Proud to be a Super User!



Anonymous
Not applicable

Hi @Sumanth_23 

 

Thank you I was able to fix the formula, but I think it is not grouping correctly on my one. I see on your picture rank is 1,2,3 for each code (Warehouse) by sales location distance. My one does not show like that.  

 

I am really sorry to bug you, but I think I am really close. Do you know why it that? Please see below, I have selected a few sales orders for your reference. 

Capture.PNG

 

I even try to swich Ship cit with Code (Warehouse), did not work. 

 

Rank_Distance = 
    VAR _Rnk =
        RANKX(
        FILTER(
            Main, 
            Main[Ship_City] = EARLIER(Main[Ship_City]) 
            && Main[Distance] <> BLANK()
            ),
        Main[Distance],,
        ASC, 
        DENSE)
    RETURN
        IF(Main[Distance] = BLANK(), BLANK(),_Rnk)

 

Thank you so much

hi @Anonymous - That is strange - can you check the data types of all columns in your data model. Also I have uploaded the pbix file to the below path - you can take a look and see what is different when compared to your model

https://drive.google.com/file/d/1DiwHnohFxA-dbALMZR0Glkqg_55ifpP_/view?usp=sharing

 

Please mark the post as a solution and provide a 👍 if my comment helped with solving your issue. Thanks!

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!

Proud to be a Super User!



Anonymous
Not applicable

Hi @Sumanth_23 

 

I am matching data format as it like on your file. I have no idea why it doing like that since I have samething like you. I have uploaded my sample pibix file. I would appreciate you could take look. Thank you so much for the help.  There should be something I am missing. Again, million thanks for your help. 

 

https://drive.google.com/file/d/1zQajuEdbPM-RLQYn5Nd8KR-YewFFvHFx/view?usp=sharing

hi @Anonymous - It appears you have data for other values for "Code" which you are filtering out at visual level - since rank is being calculated within your table it is based on the data available. 

Please see below if all codes are included then the rank is being evaluated as expected  

Sumanth_23_0-1601491852538.png

Please mark the post as a solution and provide a 👍 if my comment helped with solving your issue. Thanks!

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!

Proud to be a Super User!



Anonymous
Not applicable

@Sumanth_23  Thank you so much for your hlpe and time. You are awesome. !! This works for me. I am going to filter it out on my table level. It should work on ranking then. Again, thank you 

Anonymous
Not applicable

@Sumanth_23  Thank you so much, I will try this. 

 

hi @Anonymous - Happy to help! 🙂

 

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!

Proud to be a Super User!



Hi,

I do not understand.  May be someone else will help you.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors