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
rodrigolynce
Regular Visitor

Custom Function exponentiates the number of rows "internally"

Imagine this scenario:

 

I have 2 tables, such as one fact and one dimension, like this:

 

Dim_PRC

Anomes | Valor

201601  | PRC

201605  | PRC2

 

Fact_Hora

Hora | Anomes |  PRC | REAL | ORC

10     | 201601  | 1.2    |  1.5   |  2

 

I made a pivot in table Fact_Hora turning the columns "PRC, REAL, ORC" into rows, like this:

 

Fact_Hora

Hora | Anomes | Atributo | Values

10     | 201601  | PRC        | 1.2

10     | 201601  | REAL        | 1.5

10     | 201601  | ORC        | 2

 

I created a function called "getPRC" in a blank query, like this:

 

(codigo as text, tipo as text) =>

if tipo = "PRC" then
prc{[ANOMES=codigo]}[PRC]

else
tipo

 

 

Now, I applied this function on the table Fact_Hora into Atributo column, to bring the value of Dim_PRC table, the function works as well, but when I "apply and close" in the power query, the query stay running and the number of rows are growing, for example the source has 1.000 lines but it turns to more than 1.000.000 lines after the function were applied, however, when the power query closes the number of rows is correct.

The problem is the time that it takes to "process", is very strange.

The pbix file and the sources are in attached.

 

Files to test

https://1drv.ms/u/s!ArHpvmB5RhllkDSr7_kCeKDJ7f1c


Why it happens?

 

 

Thanks in advance.

 

 

 

Rodrigo

 

1 ACCEPTED SOLUTION

Hi @rodrigolynce,

by using your function on the fact table, you're referencing your dim table 600 k times. To make this faster, you have to buffer your dim table: Table.Buffer(YourTable)

 

But an even faster way to achieve your goal is to do a merge instead to retrieve the price for ALL rows and then add a custom column to define the condition like this:

 

    #"Merged Queries" = Table.NestedJoin(#"Valor Substituído1",{"MesId"},prc,{"ANOMES"},"prc",JoinKind.LeftOuter),
    #"Expanded prc" = Table.ExpandTableColumn(#"Merged Queries", "prc", {"PRC"}, {"PRC.1"}),
    #"Added Custom" = Table.AddColumn(#"Expanded prc", "TipoPRC", each if [Atributo] = "PRC" then [PRC.1] else [Atributo])

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

4 REPLIES 4
v-shex-msft
Community Support
Community Support

HI @rodrigolynce,

 

Based on test, I think these additional rows are caused with reference, each row refer to prc, so temporary table will increase to current row count * reference table row count.

 

Unfortunately, I haven't found any effective solution to solve these temporary rows.

 

@ImkeF @MarcelBeug Any idea about this scenario?

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @rodrigolynce,

by using your function on the fact table, you're referencing your dim table 600 k times. To make this faster, you have to buffer your dim table: Table.Buffer(YourTable)

 

But an even faster way to achieve your goal is to do a merge instead to retrieve the price for ALL rows and then add a custom column to define the condition like this:

 

    #"Merged Queries" = Table.NestedJoin(#"Valor Substituído1",{"MesId"},prc,{"ANOMES"},"prc",JoinKind.LeftOuter),
    #"Expanded prc" = Table.ExpandTableColumn(#"Merged Queries", "prc", {"PRC"}, {"PRC.1"}),
    #"Added Custom" = Table.AddColumn(#"Expanded prc", "TipoPRC", each if [Atributo] = "PRC" then [PRC.1] else [Atributo])

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hello ImkeF,

 

I did the merge and it's works perfectly, but, only for learning, can you give me a example of Table.Buffer in my function?

 

I tried this code below, but the problem still continues:

 

(codigo as text, tipo as text)=>

let
Fonte = Table.Buffer(prc)

in

if tipo = "PRC" then
Fonte{[ANOMES=codigo]}[PRC]
else
tipo

Hello again,

 

I solved the problem, I just put the Table.Buffer in the source of Dim_PRC table and it's works well..

 

 

 

Tranks for help.

 

 

Rodrigo

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.