power-query-expert
Power Query Expert
Connect Microsoft Power Query (Power BI, Excel) to Frappe apps with M code for report data access.
Quick Start
Simple REST API Connection
For direct document access and small lists (< 1000 records):
let
BaseUrl = "https://your-site.frappe.cloud",
DocType = "Item",
Fields = "[""item_code"", ""item_name"", ""standard_rate""]",
ApiUrl = BaseUrl & "/api/resource/" & DocType & "?fields=" & Uri.EscapeDataString(Fields),
Response = Json.Document(Web.Contents(ApiUrl)),
Table = Table.FromRecords(Response[data])
in
Table
Use for: Direct document reads, small queries, real-time data
See references/rest-api-power-query-example.md for complete REST API examples with filters, sorting, and pagination.
Simple Frappe Report Connection
For fast reports that won't timeout:
let
BaseUrl = "https://your-site.frappe.cloud",
ReportName = "Simple Report",
ApiUrl = BaseUrl & "/api/method/frappe.desk.query_report.run?report_name=" & Uri.EscapeDataString(ReportName),
Response = Json.Document(Web.Contents(ApiUrl)),
Data = Response[message][result],
Table = Table.FromRecords(Data)
in
Table
Use for: Small, fast reports (< 1000 rows, < 30 seconds execution)
Long-Running Reports with report_long_polling
For heavy reports, use the report_long_polling API to prevent timeouts. This API executes reports asynchronously in Frappe's background worker queue.
Three-step workflow:
- Start job - Create Prepared Report job
- Poll status - Wait for completion
- Get result - Retrieve data with column metadata
See references/long-polling-api-reference.md for API endpoint details.
Power BI/Excel Integration
1. Get Data → Blank Query
2. Advanced Editor - Paste M code from references/long-polling-power-query-example.md
3. Configure:
BaseUrl = "https://your-site.frappe.cloud"
ReportName = "Item Prices"
Filters = [] // Add filters as needed
4. Close & Apply
5. Configure Authentication when prompted:
- Excel/Power BI will prompt for credentials on first connection
- Option 1 - Username/Password: Enter your Frappe username and password
- Option 2 - API Key/Token: Enter API Key as username, API Secret as password
- Authentication is handled automatically via HTTP Basic Auth
- No need to add Authorization headers in M code
Authentication
Excel and Power BI handle authentication automatically through their built-in authentication forms. You have two options:
Option 1: Username/Password
- Use your Frappe account username and password
- Excel/Power BI uses HTTP Basic Authentication
- Header format:
Authorization: Basic base64(username:password)
Option 2: API Key/Token (Recommended for Production)
- Create API Keys in Frappe: User → API Access → Generate Keys
- Copy API Key and Secret
- In Excel/Power BI authentication prompt:
- Username: Enter your API Key
- Password: Enter your API Secret
- Excel/Power BI uses HTTP Basic Authentication
- Header format:
Authorization: Basic base64(api_key:api_secret)
Note: Excel/Power BI uses HTTP Basic Authentication, not Frappe's token format. Frappe's API accepts both formats, so authentication works seamlessly.
Security Best Practices
- Use API keys instead of passwords for automated refreshes
- Grant minimal permissions to the API user
- Rotate keys regularly
- Never include credentials in M code
Column Transformation
The report_long_polling API returns column metadata enabling automatic transformation:
{"fieldname": "item_code", "label": "Item Code", "fieldtype": "Link"}
Automatic type mapping:
- Int/Long Int → Int64.Type
- Currency → Currency.Type
- Date → Date.Type
- Datetime → DateTime.Type
- Check → Logical.Type
- Text types → type text
Manual override:
ManualTypeMap = [
Rate = Currency.Type, // Force currency
#"Stock Qty" = Int64.Type // Force integer
]
See references/long-polling-api-reference.md for full type mapping table.
Cache-Busting
Power Query caches aggressively. Add timestamps to force fresh requests:
Timestamp = Text.From(Number.Round(
Duration.TotalSeconds(DateTime.LocalNow() - #datetime(1970, 1, 1, 0, 0, 0)) * 1000
)),
UrlWithCacheBuster = BaseUrl & "&_ts=" & Timestamp
Common Use Cases
- Power BI Dashboards: Live sales, inventory, financial reports
- Excel Workbooks: Monthly analysis with pivot tables
- Scheduled Refresh: Daily/hourly dataset updates
- Cross-System Reporting: Combine Frappe data with external sources
Troubleshooting
See references/troubleshooting.md for detailed solutions.
Quick fixes:
- Timeout errors: Increase maxAttempts, verify workers running
- Connection errors: Check BaseUrl, authentication, permissions
- Empty results: Verify filters, check Prepared Report status
- Type errors: Use manual type overrides, check for nulls
- Stale data: Add cache-busting timestamps, clear cache
Resources
- REST API Examples: references/rest-api-power-query-example.md
- Long-Polling M Code: references/long-polling-power-query-example.md
- API Details: references/long-polling-api-reference.md
- Troubleshooting: references/troubleshooting.md
More from kehwar/frappe_tweaks
frappe-tweaks-power-query-expert
Expert guidance for connecting Power Query (Power BI, Excel) to Frappe apps and reports. Use when building Power Query M code for Frappe data access, integrating Frappe reports with Power BI/Excel, implementing authentication for Power Query connections, handling heavy/long-running reports with report_long_polling API to avoid timeouts, applying column types and transformations, or troubleshooting Power Query caching and connection issues.
6open-observe-api-expert
Expert guidance for OpenObserve API integration in Frappe Tweaks. Use when creating, configuring, or troubleshooting OpenObserve API DocType, implementing send_logs() or search_logs() functionality, integrating with Server Scripts/Business Logic/Client-side code, debugging connection issues, or implementing logging, monitoring, error tracking, performance metrics, or audit trail use cases.
5frappe-ci-expert
Expert guidance for setting up CI/CD tests for Frappe apps. Use when users ask about GitHub Actions workflows, CI test setup, continuous integration for Frappe apps, running tests in CI environments, database setup for CI, bench configuration in CI, or automating tests for Frappe/ERPNext applications.
4workflow-expert
Expert guidance on Frappe Workflow system including workflow structure, states and transitions, workflow actions, email notifications, permission hooks (before_transition, after_transition, filter_workflow_transitions, has_workflow_action_permission), and best practices. Use when creating workflows, implementing workflow logic, understanding state transitions, working with workflow actions, configuring email notifications, or troubleshooting workflow-related issues.
4report-expert
Expert guidance on Frappe reports including report types, structure, creation workflow, and best practices. Use when creating standard script reports, query reports, understanding report structure, working with columns and filters, or troubleshooting report-related issues.
4api-reviewer
Security review and analysis for Frappe API endpoints decorated with @frappe.whitelist(). Use when reviewing API security, checking for permission vulnerabilities, scanning for unprotected endpoints, validating role restrictions, or auditing API endpoints for security best practices. Helps identify missing frappe.only_for(), frappe.has_permission(), or frappe.get_list() usage.
4