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
Vinothsusai
Helper III
Helper III

Unable to calculate Total amount based on the formula.

Hi,

I am new into Power BI. Could you please advise.

I am expecting the output like below. Please help me

32.png

Data

LineIDAmountFormulaTotal Amount
1100 ? 
250D1+D2? 
3200D1+D3? 
470D2+D4? 
520D1+D2+D4? 
610D4+D6? 
720D5+D6+D7? 
10110D5+D6+D7? 
11130D10+D11? 

 

When i use my measure,

Measure Formula = VAR maxf =
MAX ( HANGeneralJournals_Remodify[ReportLayout.Formula])

RETURN
IF ( maxf = BLANK () ,SUM(HANGeneralJournals_Remodify[Custom Main Amount]), CALCULATE(SUM(HANGeneralJournals_Remodify[Custom Main Amount]),FILTER(ALL(HANGeneralJournals_Remodify),ISBLANK([ReportLayout.Line ID])=FALSE()&&CONTAINSSTRINGEXACT(maxf,"D"&[ReportLayout.Line ID]))))
 
I got the wrong result for the formula (D10 + D11) in the row Line ID 11, the measure taken D1+D1 instead of D10 + D11. Please see the below screen shot.
 
LineIDAmountFormulaTotal Amount
1100 100 
250D1+D2150 
3200D1+D3300 
470D2+D4120 
520D1+D2+D4220 
610D4+D680 
720D5+D6+D750 
10110D5+D6+D750 
11130D10+D11200 

 

Instead of get 240 (D10+D11), I am getting 200(D1+D1) in lineitemID 11 by using my measure.

Please advise.

 

Thanks

Vinoht S

10 REPLIES 10
Cmcmahan
Resident Rockstar
Resident Rockstar

Huh. This is an interesting issue to think about, though trying to actually implement it seems like a nightmare. I applaud you for getting this far.

 

My quick and easy fix would be to add a delimiter (likely a period or semicolon) after every cell ID in your formula.  So you would have "D1 + D2 " as your formula, and then search for "D"&[LineNo]&" ".  So then it would be looking for "D1 " vs "D10 " instead of finding a match for "D1" in "D10".

 

I'm honestly surprised that Line 11's measure isn't calculating to 340, since the filter should be matching D1, D10, and D11. 

 

Actually, looking closer at the current value, if these are actual numbers you've used to calculate, I have no idea how it's summing the result from D1 twice.  The CONTAINSSTRINGEXACT function should be comparing "D10+D11" against each value.  So it should evaluate to true for each number once.  I'm pretty sure that your formula would fail if you set up a formula to calculate D2+D2, returning just the value of D2 for example.

So I just went ahead and copied the data you provided, and I was right.  Copy/pasting your exact measure, I did get 340 for Line 11, and I got 50 when trying to calculate D2+D2.

 

As far as fixing your measure, it was very easy to add a delimiter to each of the formulas and then test for "D"&[LineID]&delimiter

Hi,

Thank you very much for your reply.

Could you please give me an example. 

For ex. D10 + D11

 

Thanks

Vinoth 

 

Well first you have to update all of the values in the formula column to be "D10;+D11;" or with a delimiter of your choice. 

 

Then you can add the bit in red to fix your issue:

Measure Formula = VAR maxf =
MAX ( HANGeneralJournals_Remodify[ReportLayout.Formula])

RETURN
IF ( maxf = BLANK () ,
SUM(HANGeneralJournals_Remodify[Custom Main Amount]),
CALCULATE( SUM(HANGeneralJournals_Remodify[Custom Main Amount]),
FILTER(ALL(HANGeneralJournals_Remodify),
ISBLANK([ReportLayout.Line ID])=FALSE() &&
CONTAINSSTRINGEXACT(maxf,"D"&[ReportLayout.Line ID]&";"))))
 

Hi @Cmcmahan,@jdbuchanan71 

Thank you very much for your reply.

@Cmcmahan - your solution works great. I have a doubt how can i calculate formula with the same aggreagated column amount. Could you please advise.

 

For example.

i take 25th line item. The formula is  D9|+D12|+D15|+D18|+D21|+D24|

For now its calculating Amount column values by using above measure= 0 + 0 + 0 + 0 + 0 + 0 = 0

But i have to take Total Amount Measure value  ie -69944.87 + 0  -13086.25 -95366.48 + 27602.27-10226.1 = 295150.290000

 

Please see the table below

Line IDGroup AccountGroup Account DescriptionMainAccountReportLayout.FormulaAmountTotal Amount
1R701000Gross sales70100000 148165942.6148165942.6
2R70100TGross sales D1|0148165942.6
3R708001Commissions to retail partner65100000 00
4R708002Regul commission to retail partner  00
5R70800TSales allowances D3|+D4|00
6R_NSNet sales D4|+D5|00
7R601001Fish purchase  00
8R603001Change in fish inventory60310001 -69944.87-69944.87
9R601001TFish & and other sea products D7|+D8|0-69944.87
10R601002Meat purchase  00
11R603002Change in meat inventory  00
12R601002TMeat D10|+D11|00
13R601003Fruits & vegetables purchase  00
14R603003Change in fruits & vegetables inventory60310003 -13086.25-13086.25
15R601003TFruits & vegetables D13|+D14|0-13086.25
16R601004Grocery purchase  00
17R603004Change in grocery products inventory60310004 -95366.48-95366.48
18R601004TGrocery D16|+D17|0-95366.48
19R601005Finished goods purchase60700001 456171.72456171.72
20R603005Change in finished goods inventory60370000 27602.2727602.27
21R601005TFinished goods D19|+D20|0483773.99
22R601006Other products purchase  00
23R603006Change in other product inventory60310005 -10226.1-10226.1
24R601006TOther products D22|+D23|0-10226.1
25R_FOODFood D9|+D12|+D15|+D18|+D21|+D24|00
26R602001Packaging purchases60261000 2204347.662204347.66

 

Thanks

Vinoht S

So for that one specific line, you want it to calculate using the TotalAmount instead of the Amount cell?

 

What indicates that this specific formula should calculate differently from the others?

Hi,

Thank you for your reply.

Its not specific line. Like that we have many lines to calculate the formula. Its a dynamic calculation based on the total amount.

 

Thanks

Vinoth SUSAINATHAN

Ahh, you want to use the value in Total Amount for future calculations. That gets much trickier than using the static Amount column for calculations like your original post indicated.  You would essentially need to recursively calculate all previous amounts to get the proper total. 

 

DAX measures are not good at using previous results to get current values, since it calculates each cell at the time the measure is called. Using a DAX calculated column also seems unlikely to work for you, since they only refresh when you refresh your entire dataset.

 

 

If you're just trying to get grouped subtotals, you can do that with the matrix visualization in PowerBI, without having to mess around with these formulas.

Honestly, it looks like you're trying to attempt some workaround to use PowerBI to do Excel's job.  Why not just use Excel to do Excel's job, and then import results into PowerBI for analysis?

@Vinothsusai  Take a look at this post on cascading subtotals.  I think it may be along the lines of what you are trying to accomplish and may help.

https://powerpivotpro.com/2011/09/profit-lossthe-art-of-the-cascading-subtotal/

jdbuchanan71
Super User
Super User

Hello @Vinothsusai 

I think you will need to change the format of your ID column in order to avoid the problem.  Something like:

LineIDAmountFormulaTotal Amount
ID01100 100
ID0250D1+D2150
ID03200D1+D3300
ID0470D2+D4120
ID0520D1+D2+D4220
ID0610D4+D680
ID0720D5+D6+D750
ID10110D5+D6+D750
ID11130D10+D11200

That way your measure doesn't find '1' twice in '11' 

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.