Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
1 Svar
2649 Visninger

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.

Avatar
Kassér
Bedste svar

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

Avatar
Kassér
Forfatter

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 Besvarelser Visninger Aktivitet
1
jun. 25
1874
3
jul. 25
3481
1
maj 25
1605
1
maj 25
1850
4
maj 25
2964