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! Request now

Reply
Anonymous
Not applicable

Conditional Merge of 2 queries / if then statement?

Hi All,

Hoping someone can help. I have 2 queries which I want to conditionally merge. Both queries have the columns COST CENTRE, SHIFT, WEEKDAY, etc). At this point it can be done by a straight merge.

 

The issue is that 1 query has the field "occupancy", and the other query has 2 fields "occupancy low" and "occupancy high".

 

What I want to do is merge all fields that have exact match columns (green), then find the "Occupancy Low" and "Occupancy High" line that matches "occupancy" in query 1 (blue columns). Finally I want to also pull in an additional column from Query 2 (orange). I have made a dummy of the data below that is colour coded as per above. Hoping someone can assist out there.Query example.JPG

 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

This is the query for Table1. This refers to Table2. See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1U9JRcvQFEgZAbGiqFKsTrQQWNwfyA+ASRmAJsLgFkO8XDBIEYqCGWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Cost Centre" = _t, Shift = _t, Weekday = _t, Occupancy = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cost Centre", Int64.Type}, {"Shift", type text}, {"Weekday", Int64.Type}, {"Occupancy", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Cost Centre", "Shift", "Weekday"}, Table2, {"Cost Centre", "Shift", "Weekday"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Occupancy Low", "Occupancy High", "Required"}, {"Occupancy Low", "Occupancy High", "Required"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table2", "Custom", each [Occupancy]>=[Occupancy Low] and [Occupancy]<=[Occupancy High]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"})
in
    #"Removed Columns"

 If you need the code for sample Table 2 as well

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1U9JRcvQFEgZAbGgCJIxALBOlWJ1oJbACcyA3AKYCpNzQGEgYgxWA5S2AXL9gkARUkSmYDZKHWxGAaYWhOaoSuCss4MqUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Cost Centre" = _t, Shift = _t, Weekday = _t, #"Occupancy Low" = _t, #"Occupancy High" = _t, Required = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cost Centre", Int64.Type}, {"Shift", type text}, {"Weekday", Int64.Type}, {"Occupancy Low", Int64.Type}, {"Occupancy High", Int64.Type}, {"Required", Int64.Type}})
in
    #"Changed Type"

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Thankyou resident rockstar. This worked a treat and the report as of today is now in production. Kudos to you.

 

Vijay_A_Verma
Super User
Super User

This is the query for Table1. This refers to Table2. See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1U9JRcvQFEgZAbGiqFKsTrQQWNwfyA+ASRmAJsLgFkO8XDBIEYqCGWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Cost Centre" = _t, Shift = _t, Weekday = _t, Occupancy = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cost Centre", Int64.Type}, {"Shift", type text}, {"Weekday", Int64.Type}, {"Occupancy", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Cost Centre", "Shift", "Weekday"}, Table2, {"Cost Centre", "Shift", "Weekday"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Occupancy Low", "Occupancy High", "Required"}, {"Occupancy Low", "Occupancy High", "Required"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table2", "Custom", each [Occupancy]>=[Occupancy Low] and [Occupancy]<=[Occupancy High]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"})
in
    #"Removed Columns"

 If you need the code for sample Table 2 as well

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1U9JRcvQFEgZAbGgCJIxALBOlWJ1oJbACcyA3AKYCpNzQGEgYgxWA5S2AXL9gkARUkSmYDZKHWxGAaYWhOaoSuCss4MqUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Cost Centre" = _t, Shift = _t, Weekday = _t, #"Occupancy Low" = _t, #"Occupancy High" = _t, Required = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cost Centre", Int64.Type}, {"Shift", type text}, {"Weekday", Int64.Type}, {"Occupancy Low", Int64.Type}, {"Occupancy High", Int64.Type}, {"Required", Int64.Type}})
in
    #"Changed Type"

 

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 Kudoed Authors