Skip to Content
Menú
This question has been flagged

How to write a formula to get the expense accounts and analytic account code like '2%' 


Avatar
Descartar
Best Answer

You cannot use analytic_account_id.code in a domain when working with analytic distribution in Odoo because analytic_distribution is a JSON field, not a relational field.


If you want to filter records based on analytic account, you can use a domain like this:

Here, 9 is the ID of the analytic account. The 'in' operator checks if the specified analytic account ID exists in the analytic_distribution JSON field.

Avatar
Descartar
Best Answer

Hello Mahmoud,

Based on your screenshot, you're trying to create a financial report that filters expenses by analytic account codes starting with "2". Here's how to properly set this up:

The Domain Filter You Need

For your formula field in the P&L report, you'll want to combine two conditions:

  1. Filter for expense accounts
  2. Filter for analytic distribution codes starting with "2"

The correct domain syntax would be:

["&", ("account_id.account_type", "=", "expense"), ("analytic_distribution.code", "=like", "2%")]
Step-by-Step Implementation
  1. Navigate to Accounting → Reporting → Financial Reports
  2. Edit your "Profit and Loss (Challenger)" report
  3. For the expense line you want to filter:
    • Click on the line (e.g., "Operating Costs")
    • Under "Expressions", select "balance"
    • In the "Formula" field, enter:
    ["&", ("account_id.account_type", "=", "expense"), ("analytic_distribution.code", "=like", "2%")]
  4. Set the Computation Engine to "Formula"
  5. Set Subformula to "sum" (to aggregate the results)
Key Notes About This Setup
  1. The =like operator is used for pattern matching where % is a wildcard
  2. The & (AND) operator combines both conditions
  3. Account types in Odoo 15+ use these main categories:
    • income
    • expense
    • asset
    • liability
    • equity
Alternative Approach (If Above Doesn't Work)

If you have issues with the analytic distribution filter, try this alternative:

["&", ("account_id.account_type", "=", "expense"), ("analytic_account_id.code", "=like", "2%")]
Testing Your Report

After saving:

  1. Generate the report
  2. Verify only expenses with analytic accounts starting with "2" are included
  3. Check your analytic account codes actually follow this pattern (you can view them in Accounting → Configuration → Analytic Accounts)

🚀 Did This Solve Your Problem?

If this answer helped you save time, money, or frustration, consider:

✅ Upvoting (👍) to help others find it faster

✅ Marking as "Best Answer" if it resolved your issue

Your feedback keeps the Odoo community strong! 💪

(Need further customization? Drop a comment—I’m happy to refine the solution!)

Avatar
Descartar
Related Posts Respostes Vistes Activitat
0
d’ag. 25
3
0
d’ag. 25
73
0
de juny 25
448
4
de maig 25
2685
2
de maig 25
6116