cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Shelley Member
Member

Need Some Help with M - How to Use Logic

Hi All,

 

I want to lookup one field in another table in the query editor and then grab a field from that table; however, if there's no corresponding record, I want to use a static field in the current table.

 

So, I have the lookup as follows:

 

//Lookup APR Long Name in Master Data View Table
#"Added Custom3" = Table.AddColumn(#"Filtered Rows1", "Distributor APR Long", each (let currentCustomer = [Sold_To_Party_Description] in Table.SelectRows(Master_APRLongDescription, each [PARTNER] = currentCustomer)){0} [APR_Long_Description]),

 

How do I add the logic that if the [APR_Long_Description] field is missing or null, to then use the field [Sold_To_APR] in the current table?

 

Thanks!

2 ACCEPTED SOLUTIONS

Accepted Solutions
Community Support Team
Community Support Team

Re: Need Some Help with M - How to Use Logic

Hi Shelley,

 

Error-handling may help, you can try M code like formula below:

#"Added Custom3" = Table.AddColumn(#"Filtered Rows1", "Distributor APR Long", each (
try
	let 
		currentCustomer = [Sold_To_Party_Description] 
	in 
		Table.SelectRows(Master_APRLongDescription, each [PARTNER] = currentCustomer)){0} [APR_Long_Description] 
otherwise 
	let 
		currentCustomer = [Sold_To_Party_Description] 
	in 
		Table.SelectRows(Master_APRLongDescription, each [PARTNER] = currentCustomer)){0} [Sold_To_APR]
),

Regards,

Jimmy Tao

Gravanita Regular Visitor
Regular Visitor

Re: Need Some Help with M - How to Use Logic

You can do it like this.

maste1r.jpgMasterLookup.JPGLookup

Select the "Master table" and go to Combine -> Merge Queries

merge menu.JPGMerge

Setup the merge to be "Left outer"

set up merge.JPGSetup merge

Expand the Lookup by clicking the top right arrows

expanded table.JPGExpand Lookup

Add a column with the formula to consider the lookup value if it exists other wise consider a field on the master table:

Lookup formula.JPGLookup formula

 

Remove unneeded columns and you end up with this:

remove columns.JPGFinal Result

 

 

9 REPLIES 9
Community Support Team
Community Support Team

Re: Need Some Help with M - How to Use Logic

Hi Shelley,

 

Error-handling may help, you can try M code like formula below:

#"Added Custom3" = Table.AddColumn(#"Filtered Rows1", "Distributor APR Long", each (
try
	let 
		currentCustomer = [Sold_To_Party_Description] 
	in 
		Table.SelectRows(Master_APRLongDescription, each [PARTNER] = currentCustomer)){0} [APR_Long_Description] 
otherwise 
	let 
		currentCustomer = [Sold_To_Party_Description] 
	in 
		Table.SelectRows(Master_APRLongDescription, each [PARTNER] = currentCustomer)){0} [Sold_To_APR]
),

Regards,

Jimmy Tao

Gravanita Regular Visitor
Regular Visitor

Re: Need Some Help with M - How to Use Logic

You can do it like this.

maste1r.jpgMasterLookup.JPGLookup

Select the "Master table" and go to Combine -> Merge Queries

merge menu.JPGMerge

Setup the merge to be "Left outer"

set up merge.JPGSetup merge

Expand the Lookup by clicking the top right arrows

expanded table.JPGExpand Lookup

Add a column with the formula to consider the lookup value if it exists other wise consider a field on the master table:

Lookup formula.JPGLookup formula

 

Remove unneeded columns and you end up with this:

remove columns.JPGFinal Result

 

 

Shelley Member
Member

Re: Need Some Help with M - How to Use Logic

I tried this exactly as written and it did not work; therefore, it should not be marked as a solution.

Gravanita Regular Visitor
Regular Visitor

Re: Need Some Help with M - How to Use Logic

Hi @Shelley,

Can you be more specific when you say it doesn't work?
I tried to put screen shots of all the steps.
If you publish your teste scenario I'll look at it.

Shelley Member
Member

Re: Need Some Help with M - How to Use Logic

@v-yuta-msft Hello Jimmy, This should not be marked as a solution. I tried this exactly as written and it did not work. I had some errors. I don't remember exactly what happened now and do not have time at the moment to work on it again.

 

Shelley Member
Member

Re: Need Some Help with M - How to Use Logic

@Gravanita Hello, Thank you for your input. I have not had time to try it. My reply was meant for Jimmy. Somebody marked that reply as a solution and it didn't work for me. Other priorities have pulled me away from this right now. When I work on it again, I will give this a try. Thanks again for your help!

 

Gravanita Regular Visitor
Regular Visitor

Re: Need Some Help with M - How to Use Logic

Hi @Shelley

If the solution you're talking about is the one I posted, I'm interested in finding out what didn't work for you.

 

It would have been much easier and faster to solve if you had published an example with the situation you wanted to see solved.

You can find a link here to a pbix with my understanding of your request.
 https://www.dropbox.com/s/3nj7t3c5hwc9cvh/Lookup%20example.pbix?dl=0

 

lu.JPG

Please let me know if I'm missing something.

 

Shelley Member
Member

Re: Need Some Help with M - How to Use Logic

@Gravanita I know it would've been easier if I could post a sample, but my file is confidential and it is huge, so to make something small enough and open enough to share would take me a very long time. I wish it was easier because it would be easier for me to find help and

get the work done at the same time. Thanks again for your help. 

Highlighted
Gravanita Regular Visitor
Regular Visitor

Re: Need Some Help with M - How to Use Logic


@Shelley wrote:

@Gravanita I know it would've been easier if I could post a sample, but my file is confidential and it is huge, so to make something small enough and open enough to share would take me a very long time. I wish it was easier because it would be easier for me to find help and

get the work done at the same time. Thanks again for your help. 


Hi @Shelley,

Have you managed to check again the solution I proposed? To save you time, last week I created and posted an example pbix file. 
Your feedback is important.