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
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
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.