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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
gavin007
Helper V
Helper V

Try Otherwise in M doesn't work when multiple rows meet the condition

I am trying to replace the internal sales account number with the external sales accounts number when the ExtInt="I" (indicating an internal sales), I use try Dax to search each row in source query where it meets the following conditions,

1. internal sales  OrderNO_Revised is the same as external sales

2. ExtInt="C" 

 

When the condition is met, the replacement happen. 

e.g. OrderNo_Revised=60S027143 with Customer Code_Revised IT000007 has been replaced with external customer code CL0002633.

 

My problem is when there are two identical OrderNo_Revised in external sale lines (yellow retangle) the Try formula doesn't work (red retangle shows the internal customer code is still IC0002851 as opposed to CL0002844). Can anyone help or suggest alternative to 

 

Capture.PNG

I upload the folder in onedrive, you need to reconnect the source file when you refresh the query. the try script is in Recco query.

https://1drv.ms/u/s!Aig3EWdV94jKgfAJ74rRtH77XZfbkA?e=Jt1dIa

 

2 REPLIES 2
Fowmy
Super User
Super User

@gavin007 

You have used try---otherwise, it hides the error. When you have multiple results, your code breaks. Remove your code and try to add a new column using the following code, if multiple results are returned, I get the MAX of it.

= Table.AddColumn(Source, "Custom", each 
if [ExtInt]="I" then  
 let Order = [OrderNo_Revised] ,
   Cust =  List.Max(Table.SelectRows(Source, each [ExtInt] = "C" and [OrderNo_Revised] = Order )[Customer Code_Revised])
 in if Cust=null then [Customer Code_Revised] else Cust
else
[Customer Code_Revised]
)

 

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Hi Fowmy, it is working in the samples. However, when i put in actual daily sales files with more than 80,000 rows,  it loops through the source every time it evaluate the formula. it takes more than 8 hours and still counting to apply the query.

 

Is there a way to make it more efficiency. like don't evaluate the external customer, only the internal.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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