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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
shaunwilks
Helper V
Helper V

M Script Help - IF statement on Append Query

 

I am wanting to use the "Advanced Editor" M- Script to optionally append one or another table based on a parameter.

 

So I have 3 tables PRIMARY, T1 and T2.  There is a parameter - P1. 

 

The PRIMARY table is the only table that will be available in the Report.

If P1 = "Table1" then the table T1 is to be appended to PRIMARY

If P2 = "Table2" then the table T2 is to be appended to PRIMARY

 

I have tried numerous times but cannot get the code to work - no matter what the contents of the parameter Table2 is always the one that gets added to the PRIMARY, not table 1.

 

Here is an example of one attempt.....

#"Appended Query1" = if ("#P1" = "Table1") then Table.Combine({Source, "T1 }) else Table.Combine({Source, "T2"})

 

Here is another try....

#"Appended Query1" = Table.Combine({Source, if ("#P1" = "Table1") then "T1" else "T2"})

 

Can someone please advise what it is I am doing wrong or provide some script that would work.

 

 

1 ACCEPTED SOLUTION

let

AppendTable = if #"P 1" = "One" then #"T 2" else #"T 1",
Source = if #"P 1" = "One" then #"T 1" else #"T 2",
#"Appended Query" = Table.Combine({Source, AppendTable})
in
#"Appended Query"



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

4 REPLIES 4
parry2k
Super User
Super User

let

    AppendTable = if P1 = 1 then T2 else T1,        
    Source = if P1 = 1 then T1 else T2,
    #"Appended Query" = Table.Combine({Source, AppendTable})
in
    #"Appended Query"

start with blank query, name it primary and paste above code in it. 

 

you can disable t1 and t2 in report loading, so you will always see primary table and will append will happen based on parameter value.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

I see the logic and its looks like it will work. The problem I am having is syntax based.

 

My parameter is not P1, its another term with a space in it.

The Value of the parameter is text and also has a space and so does the table name.

 

I made the adjustments to your code below and I get errors.

Expression.Error: We cannot convert the value "T 1" to type Table.
Details:  Value=Measures-AgeGroupsBeforePaid  Type=Type

 

let

AppendTable = if #"P 1"= "1" then "T 2" else "T 1",

Source = if #"P 1" = "1" then "T 1" else "T 2",

#"Appended Query" = Table.Combine({Source, AppendTable})

in

#"Appended Query"

let

AppendTable = if #"P 1" = "One" then #"T 2" else #"T 1",
Source = if #"P 1" = "One" then #"T 1" else #"T 2",
#"Appended Query" = Table.Combine({Source, AppendTable})
in
#"Appended Query"



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

 

Thanks very much

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 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.