Se rendre au contenu
Menu
Cette question a été signalée
1 Répondre
2622 Vues

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
Ignorer
Meilleure réponse

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
Ignorer
Auteur

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.

Publications associées Réponses Vues Activité
1
juin 25
1763
3
juil. 25
3367
1
mai 25
1521
1
mai 25
1768
4
mai 25
2897