Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
JohnLap
Frequent Visitor

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

 

 

 

1 ACCEPTED SOLUTION

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.

View solution in original post

19 REPLIES 19
kan
Helper I
Helper I

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..

Hi,

 

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


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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..

Hi,

 

I am not sure of what that codde is doing but i assume that you want to first group by the 4th column and then add the numbers from the last 2 columns.  If that be the case, then create a Table visua and drag the 4th column in the row labels.  Then write these measures:

 

Total1 = SUM(Data[Total])

Total2 = SUM(Data[Completed])

Total3=[Total2]/[Total1]

 

Does that help?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Super User @Ashish_Mathur,

   Thanks for the reply.Yaa this worked.

 

 

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yuezhe-msft
Employee
Employee

@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

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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!

 

smpa01
Super User
Super User

Capture.PNG

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

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.

 

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

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

This Posting was "deleted" and marked as success.  I reopened it.

I am posting more evidence.  The problem is real.

I had to recreate the problem since I resolved it by making summary views in SQL source.

 

Here is the summary data.  This is the actual view created in PBI and the data is below it.  I used the native Group By Line#

In the data table I have included the PURE CALC to show the correct answer.  The formula provided in Power BI does not work as it should.

DIVIDE sample 1.jpg

Line#Transaction CountPriorYear.Transaction_CountPure Calc: ((B/C)-1)*100PBI CountTest 8 = (DIVIDE('YoYDailySalesbyStateComp'[Transaction_Count],'YoYDailySalesbyStateComp'[PriorYear.Transaction_Count],0)-1)*100
19013740321.7535.04
245294610-1.760.28
37641653017.0122.77
43623367.7451.83
5134212963.553.46
634353632-5.42-5.37
7111610456.7918.90
86526451.091.46

 

Here is the detail lines that make up each of the Line #'s above.

StateTransaction CountPriorYear.Transaction_Count
123701260
130213159
136222984
212891116
214721732
217681762
322421431
325152523
328842576
45923
4156133
4147180
5352353
5509450
5481493
69761020
611731291
612861321
7270164
7391407
7455474
8200203
8248211
8204231

 

Okay this is what I did

let
    Source = Web.Page(Web.Contents("http://community.powerbi.com/t5/Desktop/ON-ADD-COLUMN-DIVIDE-function-with-two-columns-yields-wrong/m-p/330494#M147703")),
    Data2 = Source{2}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Data2, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"State", Int64.Type}, {"Transaction Count", Int64.Type}, {"PriorYear.Transaction_Count", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"State"}, {{"Transaction Count", each List.Sum([Transaction Count]), type number}, {"Prior Year.Transaction Count", each List.Sum([PriorYear.Transaction_Count]), type number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each (([Transaction Count]/[Prior Year.Transaction Count])-1)*100),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", type number}}),
    #"Rounded Off" = Table.TransformColumns(#"Changed Type1",{{"Custom", each Number.Round(_, 2), type number}})
in
    #"Rounded Off"

is 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

I will go apply those steps now and see what I get.

Thank you very much for the support!!!

I will report back after lunch.

Is this what you are looking for ?Is this what you are looking for ?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

No.  The formulas work fine at the lowest level of detail record for the source data.  In other words, if it is not a summary of the source data then the formula works.  If it is summarized, in this example, at the Line # level it does not.

Hi  @JohnLap,

             I am having same issue where division is going wrong as my data is also summarized.Can you please share how did u solve this in SQL view.

 

Thanks..

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.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.