Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
2606 Vizualizări

Hello! 

How to add percentage column of two other columns in pivot view?

When I tried to add from the SQL query, only 1 row was resolved correctly (the one below), for the rest of the rows it was added with the value of the previous row. 

Thanks in advance.

Imagine profil
Abandonează
Cel mai bun răspuns

Hi,
Refer the code
from odoo import models, fields, api

class YourModel(models.Model):
    _name = 'your.model'
    _description = 'Your Model'

    field1 = fields.Float(string='Field 1')
    field2 = fields.Float(string='Field 2')
    percentage = fields.Float(string='Percentage', compute='_compute_percentage', store=True)

    @api.depends('field1', 'field2')
    def _compute_percentage(self):
        for record in self:
            if record.field2 != 0:
                record.percentage = (record.field1 / record.field2) * 100
            else:
                record.percentage = 0.0

<record id="view_your_model_pivot" model="ir.ui.view">
<field name="name">your.model.pivot</field>
<field name="model">your.model</field>
    <field name="arch" type="xml">
        <pivot>
            <field name="field1" type="measure" string="Field 1"/>
            <field name="field2" type="measure" string="Field 2"/>
            <field name="percentage" type="measure" string="Percentage"/>
        </pivot>
    </field>
</record>


Hope it helps

Imagine profil
Abandonează
Autor

Thank you Cybrosys Techno Solutions Pvt.Ltd, but the complexity lies in the fact that to solve this pivot I have had to create a view, something non-persistent, because I cannot rely on a single model and it turns out that I need to obtain a percentage from two fields of this query.

When I put the formula in my query, everything is fine, but when it is displayed in the pivot it adds the percentages of the grouped rows.

I mean that if one of the groups has a row and the percentage is 100, the result in the pivot is 100. If in the group there are two records whose percentages have a value of 100, the result shown is 200, which is incorrect.

Related Posts Răspunsuri Vizualizări Activitate
1
iun. 25
1718
3
iul. 25
3338
1
mai 25
1481
1
mai 25
1742
4
mai 25
2874