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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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