Forum Discussion
deew95
4 years agoFrequent Visitor
Using filter and sum function inside an IF function
Hi, I have imported the following dataset into the Power BI desktop. The column Cost for each service for each company is currently calculated as count x cost per unit but it should be calcul...
- 4 years ago
Hi,
This calculated column formula works
Column = if(Data[Service]="Device services",Data[Count]*Data[Cost Per Unit],(Data[Count]-CALCULATE(SUM(Data[Count]),FILTER(Data,Data[Company]=EARLIER(Data[Company])&&Data[Service]="Device services")))*Data[Cost Per Unit])Hope this helps.
- 4 years ago
Thank you for providing the sample data. That helps a lot with proposing a potential solution.
Here is my proposed measure
Cost = if(SELECTEDVALUE('Table'[Service])="Device Services",sum('Table'[Count])*sum('Table'[Cost Per Unit]), var c= sum('Table'[Count])-CALCULATE(sum('Table'[Count]),allexcept('Table','Table'[Company]),'Table'[Service]="Device Services") return c*sum('Table'[Cost Per Unit]) )PBIX is attached.
deew95
4 years agoFrequent Visitor
Hi lbendlin,
I have copied and pasted the source data into a table.
| Company | Service | Category | Count | Cost Per Unit |
| ABC | Device Services | Silver | 322 | 750 |
| EFG | Device Services | Silver | 139 | 750 |
| XYZ | Device Services | Silver | 31 | 750 |
| MNO | Device Services | Silver | 41 | 750 |
| PQR | Device Services | Silver | 600 | 750 |
| ABC | Identity Services | Gold | 434 | 1750 |
| EFG | Identity Services | Gold | 334 | 1750 |
| XYZ | Identity Services | Gold | 53 | 1750 |
| MNO | Identity Services | Gold | 61 | 1750 |
| PQR | Identity Services | Gold | 612 | 1750 |
Also here is a screenshot of the expected outcome.
Appreciate it if you could help me with this.
Ashish_Mathur
4 years agoSuper User
Hi,
This calculated column formula works
Column = if(Data[Service]="Device services",Data[Count]*Data[Cost Per Unit],(Data[Count]-CALCULATE(SUM(Data[Count]),FILTER(Data,Data[Company]=EARLIER(Data[Company])&&Data[Service]="Device services")))*Data[Cost Per Unit])
Hope this helps.
- deew954 years agoFrequent Visitor
Ashish_Mathur thanks for this solution. It worked for me.
- Ashish_Mathur4 years agoSuper User
You are welcome.