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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

How to combine these two pieces of M code

I have this bit of code:

let
Source = FlattenHierarchy(paramAccount_title, paramAccount_id, paramAccount_parentId, paramAccount_display)
in
Source

And this gets called by a function called TestFunction.

 

Then after this I have another bit of code that:

let
    Source = datamapping,
    #"Filtered Rows1" = Table.SelectRows(Source, each ([module] = "interimreporting")),
    #"Filtered Rows2" = Table.SelectRows(#"Filtered Rows1", each [table] = "dimAccount"),
    #"Invoked Custom Function" = Table.AddColumn(#"Filtered Rows2", "TestFunction", each accountHierarchy_Function([hierarchyName], [id], [parentId], [displayField])),
    accountHierarchy = #"Invoked Custom Function"{0}[TestFunction]
in
    accountHierarchy

Is there any way I can combine the first bit of code into the last one, and by-pass the intemdiary step. Note that what the last bit of code is doing, in the "Invoke Custom Function" step, is that its getting the input for TestFunction from the columns in the tables from the previous step. In doing so it's doing an Each to iterate through all rows in the table.

 

However I can safely assume that the table only has 1 row, and therefore instead of doing the Each I just want to feed these same column values into:

FlattenHierarchy(paramAccount_title, paramAccount_id, paramAccount_parentId, paramAccount_display)

 

 

1 ACCEPTED SOLUTION
d_gosbell
Super User
Super User

I think you should be able to simplify the M code down to the following, just using #"Filtered Rows2"{0} to reference the first row from the second Table.SelectRows() call and then just call the FlattenHierarchy function directly. I would not expect too much change in performance, but it will reduce the number of queries in your model.

 

let
    Source = datamapping,
    #"Filtered Rows1" = Table.SelectRows(Source, each ([module] = "interimreporting")),
    #"Filtered Rows2" = Table.SelectRows(#"Filtered Rows1", each [table] = "dimAccount"),
    accountHierarchy = FlattenHierarchy(#"Filtered Rows2"{0}[hierarchyName], #"Filtered Rows2"{0}[id], #"Filtered Rows2"{0}[parentId], #"Filtered Rows2"{0}[displayField])
in
    accountHierarchy

 

View solution in original post

2 REPLIES 2
d_gosbell
Super User
Super User

I think you should be able to simplify the M code down to the following, just using #"Filtered Rows2"{0} to reference the first row from the second Table.SelectRows() call and then just call the FlattenHierarchy function directly. I would not expect too much change in performance, but it will reduce the number of queries in your model.

 

let
    Source = datamapping,
    #"Filtered Rows1" = Table.SelectRows(Source, each ([module] = "interimreporting")),
    #"Filtered Rows2" = Table.SelectRows(#"Filtered Rows1", each [table] = "dimAccount"),
    accountHierarchy = FlattenHierarchy(#"Filtered Rows2"{0}[hierarchyName], #"Filtered Rows2"{0}[id], #"Filtered Rows2"{0}[parentId], #"Filtered Rows2"{0}[displayField])
in
    accountHierarchy

 

Anonymous
Not applicable

Thanks - this greatly simplified my code!

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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