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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
NBEN66
Frequent Visitor

Dynamically import custom functions

Hello Everyone, 

 

At our company we are trying to automate customer orders that are sent in via PDF. We have hundreds of customer with different PDF templates. We decided to use Power query PDF import to solve this task, and edit each PDF to the desired format. Not the best solution but it is working. We are using an excel as front end where employees can select each instance run the query extract the data and than a macro uploads it to SAP. The problem we are facing is that we store these solutions as custom function in power query and based on instance selected the query invokes the matching solution. At the moment we have 50 custom function in the file. For some reason this makes the file basically unusable its like it trying to load each custom function at the same time even though we disabled that setting.

And our question is: is it possible to import a Custom function for example from txt with a custom function ? So in the end in the file itself there are two function one that import the correct solution and one that runs it ?

1 ACCEPTED SOLUTION
jennratten
Super User
Super User

Hello - yes, this can be done.  Here is an example:

let
    //Load M code from text file
    Source = Text.FromBinary(File.Contents("C:\PathToYourFile.txt")),
    //Evaluate the code from the file as an M expression
    EvaluatedExpression = Expression.Evaluate(Source, #shared)    
in
    EvaluatedExpression 

View solution in original post

5 REPLIES 5
jennratten
Super User
Super User

Hello - yes, this can be done.  Here is an example:

let
    //Load M code from text file
    Source = Text.FromBinary(File.Contents("C:\PathToYourFile.txt")),
    //Evaluate the code from the file as an M expression
    EvaluatedExpression = Expression.Evaluate(Source, #shared)    
in
    EvaluatedExpression 

May I know how to save the content of txt file contains custom functions?

Hi @Karthikeyan_S, this way: (you can see 2 functions there excelTrim and fnShiftRows)

 

[
excelTrim = 
		(textToTrim) =>
		let 
			fn = 
				[   v_split = Text.Split(Text.From(textToTrim), " "),
					v_select = List.Select(v_split, each Text.Length(_) > 0),
					v_combine = Text.Combine(v_select, " ")
				][v_combine]
		in
			fn,


fnShiftRows = 
	(tbl as table, col as text, shift as nullable number, optional newColName as text) as table =>
	//v 3. parametri zadaj zaporne cislo ak chces posunut riadky hore, kladne ak dole, 4. je nepovinny (novy nazov stlpca)
	let
		a = Table.Column(tbl, col),
		b = if shift = 0 or shift = null then a else if shift > 0
			then List.Repeat({null}, shift) & List.RemoveLastN(a, shift)
			else List.RemoveFirstN(a, shift * -1) & List.Repeat({null}, shift * -1),    
		c = Table.FromColumns(Table.ToColumns(tbl) & {b}, Table.ColumnNames(tbl) &
			( if newColName <> null then {newColName} else
				if shift = 0 then {col & "_Duplicate"} else
				if shift > 0 then {col & "_PrevValue"} 
				else              {col & "_NextValue"} ))
	in
	c
]

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

You are a life saver, thank you! 

You're welcome!

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

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