cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ECE Frequent Visitor
Frequent Visitor

Query m merge with if condition

Hi,

 

Can anyone tell me how to merge two queries based on value in primary query?

 

My main dataset includes a column called Sales doc and a column called Sales Doc Item. Depending on the value in the third column "Source" these should be joind with either "P02 sales Doc" and "P02 saled doc item" OR "P08 Sales Doc" and "P08 Sales doc item".

If joining only on sales doc, I am think something in terms off:

 

#"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Sales Doc"},Table1, {if "Souce" = "P08" then "P08 Sales Doc" else "P02 Sales Doc"},"Table1",JoinKind.LeftOuter),

 

The editor do in fact accept the formular above, but it does not understand that "Souce" refers to the "Source" column.

 

If this gets working, I would extend the formular to something like:

 

#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Sales Doc", "Item"},Table1, {if "Souce" = "P08" then "P08 Sales Doc" else "P02 Sales Doc", if "Source" = "P08" then "P08 Item" else if "P02 item" = null then "Item" else "P02 item"},"Table1",JoinKind.LeftOuter),

 

The last if, is because in some cases I don't have an item no, so I would need to join only on "Sales Doc"

 

Any ideas

 

4 REPLIES 4
Highlighted

Re: Query m merge with if condition

This is a little hard to follow without an example. Is it posible to add an example, with expected outcomes?

 

From what I understand of the question, you could try adding a custom column. 

 

In the custom column, you could add:

 

 

if [Source] = "P08"

then

Table.NestedJoin(#"Changed Type",{"Sales Doc"},Table1, "P08 Sales Doc","Table1",JoinKind.LeftOuter)

else if

[P02 item] = null

Table.NestedJoin(#"Changed Type",{"Sales Doc"},Table1, "Item","Table1",JoinKind.LeftOuter)

else

Table.NestedJoin(#"Changed Type",{"Sales Doc"},Table1, "P02 Sales Doc","Table1",JoinKind.LeftOuter)

 

 

ECE Frequent Visitor
Frequent Visitor

Re: Query m merge with if condition

Hi Steve,

 

First of all thank you for you replay.

 

I need to return 4 columns from the sub query.

As default I will be joining two columns from the main query with the sub query, However, depending on the value of "Source" in the main query, I will need to join with different columnns in the sub query. I addition to that, if "Source" = "P02" AND "P02 item" in the SUB query is null, then I will only join on one column.

 

I have tried your solution, but with just 4 rows in both the main and sub query, I get 16 rows return after the join. Not sure if it is possible to do anyting about that. I am returning a table, and then expanding the main query from this table.

 

OBS: The first merge was an attempt to check if "P02 item" was blank in the sub query. Maybe this could be done directly, but don't know how to reference the sub query columns directly.

 

    #"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Sales Doc", "Item"},Table1,{"P02 Sales Doc", "P02 item"},"Table1",JoinKind.LeftOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Text"}, {"Text"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table1", "asCustom", each if [Source] = "P08" then 
Table.NestedJoin(#"Expanded Table1",{"Sales Doc", "Item"},Table1, {"P08 Sales Doc", "P08 Item"},"Table1",JoinKind.LeftOuter) 
else if
[Text] = null then
Table.NestedJoin(#"Changed Type",{"Sales Doc"},Table1, {"P02 Sales Doc"},"Table1",JoinKind.LeftOuter)
else
Table.NestedJoin(#"Expanded Table1",{"Sales Doc", "Item"},Table1, {"P02 Sales Doc", "P02 item"},"Table1",JoinKind.LeftOuter)),
    
#"Expanded asCustom" = Table.ExpandTableColumn(#"Added Custom", "asCustom", {"Table1"}, {"asCustom.Table1"}),
    #"Expanded asCustom.Table1" = Table.ExpandTableColumn(#"Expanded asCustom", "asCustom.Table1", {"Text"}, {"new.Text"})

 

As an alternative I could merge the two queries three times. Each time return the 4 columns, and then afterwards, create 4 new custom columns, based on the first 12 columns returned - that just seems very unefficient.

Community Support Team
Community Support Team

Re: Query m merge with if condition

Hi @ECE,

 

I'd like to suggest you take a look at following link to know how to create a custom query with multiple if conditions.

Multi Condition Logic In Power Query

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin Sheng
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.



For learning resources/Release notes, please visit: | |
ECE Frequent Visitor
Frequent Visitor

Re: Query m merge with if condition

Hi Xiaoxin,

 

I have looked at the link you shared, but I am unable to see how this will help joining with another table. In this example the values to return is hard coded within the function.

I have a main query of 500.000+ rows, which needs to return values from a second sub query of 300.000+ rows.