8. Adding Columns: Power Query Add Column Tab and DAX Calculated Columns
8.4 Invoke Custom Function
Use it to run the same logic in many queries (for example Blinkit and Amazon Now), or to call a function for each row (pagination, Module 5.3).
Steps in Power BI
- Home › New Source › Blank Query › Home › Advanced Editor › paste the function below › Done. Rename the query
fnDeliveryBand. - Go to the Orders query › Add Column › Invoke Custom Function.
- New column name:
Delivery Band; Function query:fnDeliveryBand; mins: choose Column Name ›Delivery Time Mins› OK. - Set the type to Text. Repeat in the AmazonNow query so both use identical rules.
// fnDeliveryBand
(mins as nullable number) as text =>
if mins = null then "Not delivered"
else if mins <= 10 then "0-10 min"
else if mins <= 15 then "11-15 min"
else if mins <= 30 then "16-30 min"
else "30+ min"
Tip
If the business changes the band limits, you edit one function instead of every query.
Practice task
Write fnPaymentGroup(mode as text) as text that returns "Digital" for UPI/Card/Wallet and "Cash" otherwise. Invoke it on Orders.
Ravindra Bagale's Tip
Friends, many students invoke a custom function on a table with thousands of rows that each call a web API, and refresh takes forever. Test the function on a few rows first, keep the function simple, and remember that invoking custom functions usually stops query folding. Don't worry – after doing it two or three times, it becomes a habit.
Ravindra Bagale's Tip – मराठी
मित्रांनो, बरेच students हजारो rows असलेल्या table वर custom function invoke करतात, जिथे प्रत्येक row web API ला call करते, आणि refresh ला अनंत वेळ लागतो. आधी काही rows वर function test करा, function सोपं ठेवा, आणि लक्षात ठेवा की custom functions invoke केल्याने साधारणपणे query folding थांबतं. घाबरू नका, दोन-तीन वेळा केलं की सवय होते.
Ravindra Bagale's Tip – हिंदी
दोस्तों, बहुत से students हज़ारों rows वाली table पर custom function invoke करते हैं जहाँ हर row web API को call करती है, और refresh में बहुत ज़्यादा समय लगता है. पहले कुछ rows पर function test करो, function आसान रखो, और याद रखो कि custom functions invoke करने से आमतौर पर query folding रुक जाता है. घबराओ मत, दो-तीन बार करने पर आदत हो जाती है.