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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
LunaD
Regular Visitor

Subtract values in rows based on categories in another table

I have table 1

DateIDValue
23/02/2024x234fs21
23/02/2024d4635d2
23/02/2024a324ss54
23/02/2024d35s223

 

table 2

CategoryIDLocation
TypeAx234fsLake A
TypeBd4635dLake A
TypeAa324ssGarden
TypeBd35s22Garden

 

I want to subtract value of typeA to typeB in table 1 if IDs are in the same location and same month. Category typeA and typeB are fixed but there are many locations.
Expected new table like this:

DateIDValue
23/02/2024x234fs19
23/02/2024d4635d2
23/02/2024a324ss51
23/02/2024d35s223

 

I have been trying to create new column in the table 1 like below but does not work. I appreciate all the help and suggestion. Thank you!!

 

 

 

``` 

SubtractionResult =
VAR LocationsWithAB =
FILTER(
VALUES(table_2),
CALCULATE(COUNTROWS(table_2), table_2[Category] = "Type A") > 0 &&
CALCULATE(COUNTROWS(table_2), table_2[Category] = "Type B") > 0
)
VAR Result =
SUMX(
LocationsWithAB,
VAR CurrentLocation = table_2[Location]
VAR AccountA = CALCULATE(VALUES(table_2[ID]), table_2[Location] = CurrentLocation, table_2[Category] = "Type A")
VAR AccountB = CALCULATE(VALUES(table_2[ID]), table_2[Category] = CurrentLocation, table_2[Category] = "Type B")
VAR ConsumptionA =
CALCULATE(
SUM(table_1[Value]),
table_1[ID] = AccountA &&
table_1[Date] IN VALUES(table_1[Date])

)
VAR ConsumptionB =
CALCULATE(
SUM(table_1[consumption]),
table_1[ID] = AccountB &&
table_1[Date] IN VALUES(table_1[Date])
)
RETURN
IF(
NOT(ISBLANK(ConsumptionA)) && NOT(ISBLANK(ConsumptionB)),
ConsumptionA - ConsumptionB,
ConsumptionB
)

)
RETURN
Result
```

1 ACCEPTED SOLUTION
LunaD
Regular Visitor

Thanks for your suggestion @amitchandak !!
It's very close to what I need, just that it won't work if there are different dates in the table.
A little tweak fixed it:

Measure = if(max(Table2[Category]) ="TypeA", sum(Table1[Value])- CALCULATE(SUM(Table1[Value]),ALLEXCEPT(Table1, Table2[Location], Table1[Date]), Table2[Category]="TypeB"), SUM(Table1[Value]))
 
Cheers.

View solution in original post

2 REPLIES 2
LunaD
Regular Visitor

Thanks for your suggestion @amitchandak !!
It's very close to what I need, just that it won't work if there are different dates in the table.
A little tweak fixed it:

Measure = if(max(Table2[Category]) ="TypeA", sum(Table1[Value])- CALCULATE(SUM(Table1[Value]),ALLEXCEPT(Table1, Table2[Location], Table1[Date]), Table2[Category]="TypeB"), SUM(Table1[Value]))
 
Cheers.
amitchandak
Super User
Super User

@LunaD , Hope two tables are joined. File is attached for reference 

 

Measure = if(max(Table2[Category]) ="TypeA", sum(Table1[Value])- CALCULATE(SUM(Table1[Value]),ALLEXCEPT(Table1, Table2[Location]), Table2[Category]="TypeB"), SUM(Table1[Value]))
 
 
Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.