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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.