Forum Discussion

JohnLap's avatar
JohnLap
Frequent Visitor
8 years ago
Solved

ON ADD COLUMN: DIVIDE() function with two columns yields wrong result?

I am new! I am probably missing something obvious!!!  But after 12 hours of trying to figure this out I need help.

Newest version Power BI Desktop.  Just want to Divide two numeric columns.  When I do the MATH is failing.  I have tried this every way I can find, no joy!


The specific math in this example for row 1:
2867 / 3159 = 0.90756568 (using a calculator)

 

As I understand it, this should be a row by row calculation for this view.

CountTest6 = DIVIDE('YoY_Daily_Sales'[Transaction_Count],'YoY_Daily_Sales'[PriorYear.Transaction_Count])
The answer provided by this formula is 13.53.  This is incorrect!

 

LineTransaction_CountPriorYear.Transaction_CountCountTest6
12867315913.53084674
2147217327.749660014
32515252311.04036185
41471800.816666667
54814933.889987179
6117312916.29352048
73914071.893128079

 

 

 

  • Correction:  The real solution was a little more complex

     

    Transaction Var % Measure = (DIVIDE(CALCULATE(SUM('Table'[Count])),CALCULATE(SUM('Table'[PriorYear.Count])),0)-1)*100

     

    This actually solves the problem.

     

    Converting to a measure is required and will not work against a Direct Query.

19 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    JohnLap,

    Your DAX formula is correct. Do you use the latest version of Power BI Desktop(2.53.4954.621)? Which table does the Line field come from? Is it also a field in the YoY_Daily_Sales table?

    Regards,
    Lydia

    • JohnLap's avatar
      JohnLap
      Frequent Visitor

      Thank you for the response.

      I think I may have figured out what is happening, but I am surprised if it is designed this way.

       

      The Line# comes from the same table.  It is actually STATE but I changed it to Line # for simplicity.

       

      While the table view in PowerBI is summarized  at the Line# level (Group By) there are multiple rows behind that.  It appears that Power BI is doing the division on each line of the lowest level of detail and then averaging or summing those.  This is absolutely the wrong way to do it.  I would have assumed that "Row by Row" calculations meant each Row that is in the VIEW.  To solve the problem, I had to create a SQL view in the source table that summarized the data by Line#(State).  So, that becomes the lowest level of detail.  

       

      Either the documentation, or this DIVIDE function needs to be changed.  Users of these reports can easily do the division on the view they see and determine that the math is just wrong.  Great way to lose credibility of a report.  Also, dramatically reduces the "POWER" in PowerBI if I have to create views to aggregate to every group by level I would want in the report.  My expectation was that I could produce the data at the lowest level and have all of the math work out as I grouped as I pleased in Power BI.  NOPE!

       

    • JohnLap's avatar
      JohnLap
      Frequent Visitor

      I see you recreated the problem to test.  THANK YOU for taking the time to do that.However, you only recreated the single line of data for each line in the summarized view.  If you had multiple rows behind each of the Line# rows, the formula provides the incorrect result.  This is the issue.

       
      • smpa01's avatar
        smpa01
        Icon for Community Champion rankCommunity Champion

        JohnLap- can you share the sample data please in excel/PBI - whatever you prefer?

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi All,

            I am new to power bi and am trying to divide two existing columns and get result into calculated column,but the results are incorrect.can someone please help.I tried to use below formula

     

    %Complete = DIVIDE(Node[completed],Node[Total])

     

     

    NodemajormilestonenameTotalcompleted%CompleteCorrect Values
    Preliminary Node Lock7701256137.561055233.25
    Final Node Lock7701186132.9809542 
    ER Approval7743182242.0270005 
    Node Engineering7701176836.9119816 
    Equipment Ordered770159917.5948997 
    Make Ready Start770130014.5904787 
    Construction Permit Request770181222.3153618 
    Make Ready Complete770121912.3539329 
    Construction Permit Received770141614.9592466 
    Construction Start774362017.9762811 
    Power Complete774348211.040731 
    Construction Complete774353812.1115371 
    Inspection Complete770141611.45646925.4
    Integration Complete - Internal77013807.46115686 
    Node On-Air – Customer77012645.80146265 
    Closeout Complete77114359.91702557 

     

     

    Thanks..

    • Ashish_Mathur's avatar
      Ashish_Mathur
      Icon for Super User rankSuper User

      Hi,

       

      There is no mistake in your formula.  Share the link from where i can download your PBI fle.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Thanks for the reply.Following is my source code for dataset.Both Total and Completed are original fields and part of dataset and are grouped by node name.I am struck when trying to calculate completed/total  in power bi.

         

        Source Code:

         

        select ProjectId,region,state,milestonename as Nodemajormilestonename,nodemajormilestonedefaultsid,Total,completed
        from (
        select distinct p.WspNetworkId as projectid,p.regionid as region,p.networkstate as state,milestonename,nodemajormilestonedefaultsid,count(distinct m.projectnodeid) Total,
        sum(case when actualmilestonedate is not null then 1 else 0 end) as completed
        from dbo.ProjectMaster p
        join
        (
        select
        ProjectdesignId
        ,NodeName
        ,max(ProjectNodeId) ProjectNodeId
        from dbo.ProjectNode
        group by
        ProjectDesignId
        ,NodeName
        ) n on (p.ProjectDesignId = n.ProjectDesignId)
        join dbo.ProjectNodeMajorMilestone m on (n.ProjectNodeId = m.ProjectNodeId)
        group by MilestoneName,p.WspNetworkId,p.regionid,p.networkstate,nodemajormilestonedefaultsid)x
        order by projectid,nodemajormilestonedefaultsid

         

        Following is my Source dataset:

         

        ProjectIdregionstateNodemajormilestonenamenodemajormilestonedefaultsidTotalcompleted
        NC-IA-801GRAND-VZWCIAPreliminary Node Lock110
        NC-IA-801GRAND-VZWCIAFinal Node Lock210
        NC-IA-801GRAND-VZWCIAER Approval310
        NC-IA-801GRAND-VZWCIANode Engineering410
        NC-IA-801GRAND-VZWCIAEquipment Ordered510
        NC-IA-801GRAND-VZWCIAMake Ready Start610
        NC-IA-801GRAND-VZWCIAConstruction Permit Request710
        NC-IA-801GRAND-VZWCIAMake Ready Complete810
        NC-IA-801GRAND-VZWCIAConstruction Permit Received910
        NC-IA-801GRAND-VZWCIAConstruction Start1010
        NC-IA-801GRAND-VZWCIAPower Complete1110
        NC-IA-801GRAND-VZWCIAConstruction Complete1210
        NC-IA-801GRAND-VZWCIAInspection Complete1310
        NC-IA-801GRAND-VZWCIAIntegration Complete - Internal1410
        NC-IA-801GRAND-VZWCIANode On-Air – Customer1510
        NC-IA-801GRAND-VZWCIACloseout Complete1610
        NC-IA-CLARKEF1-VZWCIAPreliminary Node Lock150
        NC-IA-CLARKEF1-VZWCIAFinal Node Lock250
        NC-IA-CLARKEF1-VZWCIAER Approval350
        NC-IA-CLARKEF1-VZWCIANode Engineering450
        NC-IA-CLARKEF1-VZWCIAEquipment Ordered550
        NC-IA-CLARKEF1-VZWCIAMake Ready Start650
        NC-IA-CLARKEF1-VZWCIAConstruction Permit Request750
        NC-IA-CLARKEF1-VZWCIAMake Ready Complete850
        NC-IA-CLARKEF1-VZWCIAConstruction Permit Received950
        NC-IA-CLARKEF1-VZWCIAConstruction Start1050
        NC-IA-CLARKEF1-VZWCIAPower Complete1150
        NC-IA-CLARKEF1-VZWCIAConstruction Complete1250
        NC-IA-CLARKEF1-VZWCIAInspection Complete1350
        NC-IA-CLARKEF1-VZWCIAIntegration Complete - Internal1450
        NC-IA-CLARKEF1-VZWCIANode On-Air – Customer1550
        NC-IA-CLARKEF1-VZWCIACloseout Complete165

         

         

        0

         

        Below is data tried to build in visualisation mode from datasets.here i used 

        Complete = DIvide(Query1[completed],Query1[Total]  which is giving wrong results (might be data is summarized) ).For example for Preliminary Node Lock complete=2561/7701 =0.332 but is giving 37.56 which is wrong.

         

        NodemajormilestonenameTotalcompletedComplete
        Preliminary Node Lock7701256137.56106
        Final Node Lock7701186533.98095
        ER Approval7743182242.027
        Node Engineering7701176836.91198
        Equipment Ordered770159917.5949
        Make Ready Start770130014.59048
        Construction Permit Request770181222.31536
        Make Ready Complete770122313.35393
        Construction Permit Received770141715.20925
        Construction Start774362017.97628
        Power Complete774348211.04073
        Construction Complete774353812.11154
        Inspection Complete770141611.45647
        Integration Complete - Internal77013807.461157
        Node On-Air – Customer77012645.801463
        Closeout Complete77114359.917026

         

        Thanks..