cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

Helper III

## Not able to sum by category

Hi all,

I am trying to make in one column the lower of the following:

([column A] / [column B] * .6) – [column A] or 0

So if the result of the calculation is negative I want that amount. If the result is positive the result should show zero.

Currently, I have the following:

``````balance:=sum(tablename [column A])

LTV:=sum(tablename [column B])

Measure 1:=sumx(tablename,([balance]/[LTV])*.6)-sum(tablename [column A])

Measure 2:=min(sumx(tablename,[Measure 1]),0)``````

Measure 2 works on a line by line i.e. per product. But when I group this by region or colour each region/colour shows 0. For each region I want to add all the outcomes per line where the outcome is lower than 0.

Why is this my DAX code not working as intended, please?

Thanks,

Nav

1 ACCEPTED SOLUTION
Helper I

From my point of view, the measure is not working as intended as first of all all columns for price are added, same for discount and then your logic is applied. You want it the other way around. First apply your logic to all single rows, then sum.

I just recreated your data by manually entering them:

Next, because I am a huge fan of user interaction and the values of your parameter might change, create a table containing all values of your parameter, your case 0.2, 0.3, 0.4:

Next, write a measure to store the selection of the value, also asserting a standard value:

Parameter_value = SELECTEDVALUE(Parameter[value], 0.2) (in case nothing is choosen, 0.2 is returned, assuring a calculation)

Next, write a measure to calculate your logic, first calculating your formula on every row, summing afterwards, SUMX was made for this:

Logic = SUMX(Data, IF(Data[price]/Data[discount]*(1-Parameter[Parameter_value])-1.6*Data[price] < 0, -(Data[price]/Data[discount]*(1-Parameter[Parameter_value])-1.6*Data[price]) , 0))

Next, visualisation:

Use matrix value, putting a color-product hierarchy in the rows, using Logic as a value, putting column value of Parameter in the filters to switch the parameter, done! 3 screenshots for a all 3 values of your parameter:

0.2:

0.3:

0.4:

Itmis easy to add new values for the parameter to the table.

6 REPLIES 6
Helper I

From my point of view, the measure is not working as intended as first of all all columns for price are added, same for discount and then your logic is applied. You want it the other way around. First apply your logic to all single rows, then sum.

I just recreated your data by manually entering them:

Next, because I am a huge fan of user interaction and the values of your parameter might change, create a table containing all values of your parameter, your case 0.2, 0.3, 0.4:

Next, write a measure to store the selection of the value, also asserting a standard value:

Parameter_value = SELECTEDVALUE(Parameter[value], 0.2) (in case nothing is choosen, 0.2 is returned, assuring a calculation)

Next, write a measure to calculate your logic, first calculating your formula on every row, summing afterwards, SUMX was made for this:

Logic = SUMX(Data, IF(Data[price]/Data[discount]*(1-Parameter[Parameter_value])-1.6*Data[price] < 0, -(Data[price]/Data[discount]*(1-Parameter[Parameter_value])-1.6*Data[price]) , 0))

Next, visualisation:

Use matrix value, putting a color-product hierarchy in the rows, using Logic as a value, putting column value of Parameter in the filters to switch the parameter, done! 3 screenshots for a all 3 values of your parameter:

0.2:

0.3:

0.4:

Itmis easy to add new values for the parameter to the table.

Helper III

@Schmidtmayer Thanks for your time and efforts

The solution works well in Power BI Desktop. However, I am working in Excel and found that SELECTEDVALUE is not working in Excel. Is there a way I can change the formula so that it works in Excel?

Thanks,

Naveen

Helper III

Hi All,

Thanks so much for your efforts. I really appreciate it. However, it does not get me the desired result. I thought I make it a bit more clear with demo data.

Let's start with he demo dataset is on the left and while the number do not make much sense I am of course after the DAX code 😊.

Demodata

See the results:

Results01

In the first block (upper left) we calculate the result of the following in Excel:

=((\$C2/\$D2)*(1-H\$1)-\$C2*1.6)

in words

((Price / discount) times (1 minus variable)) minus (Price times 1.6)

In the second block (upper right) I calculate

=MIN(H2,0)

the lowest of zero and the result in the first block

In the pivot tables under each block the results are displayed.

In one DAX Column I want achieve what can be seen in the third table i.e. per colour only when the results is below zero

 20% 30% 40% red - -1.22 - 16.52 Blue - - - 11.68 Green - - 8.57 - 24.26

or as in the pivot table below

DesiredResult

The results I getting from the two suggestions above are in the image below. Please note that I am only taking  the last outcome i.e. what should be Sum of 40%.

SuggestedSolutions

Thanks,

Nav

Anonymous
Not applicable

If I got it right now, you have to create 3 Measures.

One for 20%, one for 30% and one for 40%

20 % =
var calc = CALCULATE(((SUM('Sample'[Price]) / SUM('Sample'[Discount])) * (1 - 0.2)) - (SUM('Sample'[Price]) * 1.6))
return
IF(calc > 0, "-", calc)

30 % =
var calc = CALCULATE(((SUM('Sample'[Price]) / SUM('Sample'[Discount])) * (1 - 0.3)) - (SUM('Sample'[Price]) * 1.6))
return
IF(calc > 0, "-", calc)

40 % =
var calc = CALCULATE(((SUM('Sample'[Price]) / SUM('Sample'[Discount])) * (1 - 0.4)) - (SUM('Sample'[Price]) * 1.6))
return
IF(calc > 0, "-", calc)

Then insert all the measures in the table.
With this measures, I get the same table as above:

Regards
MVP

Try this code to add a new column and use that to cover your request:

New Column =
Var _balance=sum(tablename [column A])
Var _LTV=sum(tablename [column B])
Var _Calc = _balance/_LTV*0.6-_balance
Return
IF(_Calc>=0,0,_Calc)

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Try this:

Measure 1 = CALCULATE( ([balance] / [LTV] * 0.6 - [balance]))

Measure 2 = IF( [Measure 1] > 0, 0, [Measure 1])

Regards

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.