The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
08-22-2025 02:47 AM - last edited 08-22-2025 02:49 AM
Overview
In this workflow, industrial equipment is fitted with IoT sensors streaming operational metrics (temperature, vibration, pressure, etc.) into a real-time event stream. An event processor (Activator) continuously evaluates incoming readings against expected patterns. If a defect or anomaly is detected, the system:
Sends an instant notification to the maintenance worker’s mobile device.
Creates a new defect tracking record in the operational SQL database with initial details.
After performing the repair, the maintenance worker accesses a Power BI maintenance dashboard. Here, they can:
Review the defect record (with sensor data snapshots, alerts, and location).
Document the defect resolution, root cause, and parts replaced.
Submit this information directly back to the SQL defect tracking table via a User Data Function (UDF).
The UDF securely updates the operational database in real time, eliminating the need for separate maintenance forms or manual syncing. This ensures that historical defect data stays complete, accurate, and available for both operational and analytical purposes.
Here is the code used for this scenario to update the defect after resolving it through workforce:
import fabric.functions as fn
udf = fn.UserDataFunctions()
# ===============================
# UDF: update_defect_resolution
# ===============================
# Take a defect id, resolver, resoltuion notes and parts replaced (Status will be set automatically to "closed" as this is the intended action) as input parameters
# Write the data back to the SQL database
# Users will provide these parameters in the PowerBI report
@udf.connection(argName="sqlDB",alias="<Your DB alias from connections>")
@udf.function()
def update_defect_resolution(sqlDB: fn.FabricSqlConnection,
defectid: int,
resolvedby: str,
resolutionnotes: str,
partsreplaced: str) -> str:
"""
Updates defect tracking record in the operational SQL database.
Status is default set to "Closed" if a resolution is entered.
Parameters:
-----------
defectid : int
Unique identifier of the defect record.
resolvedby : str
Name or ID of the maintenance worker.
resolutionnotes : str
Description of how the defect was resolved.
partsreplaced : str
Comma-separated list of parts replaced.
Returns:
--------
str
"Defect resolution updated successfully."
"""
try:
# Establish a connection to the SQL database
connection = sqlDB.connect()
cursor = connection.cursor()
# Update query
query = """
UPDATE [maintenance_demo].[defect_tracking]
SET resolved_by = ?,
resolution_notes = ?,
parts_replaced = ?,
status = 'Closed',
resolution_timestamp = GETUTCDATE()
WHERE defect_id = ?
"""
# Execute update
cursor.execute(query, (resolvedby, resolutionnotes, partsreplaced, defectid))
# Commit the transaction
connection.commit()
cursor.close()
connection.close()
return "Defect resolution updated successfully."
except Exception as e:
return {
"defect_id": defectid,
"status": "Error",
"message": str(e)
}
Feel free to use this code as inspiration for your own scenarios!