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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
parameswaran
Regular Visitor

Calculate sum of column based on primary key & distinct records from another table

 

Table 1: (ID Primary Key, Value)                                                         

ID (Primary Key)

Value

1

100,000

2

50,000

3

100,000

4

50,000

 

Table 2: (ID)

ID

1

1

1

2

2

 

To calculate sum of the Column: Table 1 [Value] based on the distinct values from Table 2.

Logic:
1. Create a measure on Table 1 which can return the sum of Table 1 [Value]

  • Internally within the measure, create a table by joining these two tables which looks like:

ID (DISTINCT from Table 2)

Value (Table 1)

1

100,000

2

50,000

  • Finally the measure will return the sum of the Column: Table 1 [Value] (like Measure on Table 1 = 150,000)

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Hi @parameswaran ,

Can you create a relationship between two tables?  If you can, I created a sample that maybe help you.

1.PNG

Measure = CALCULATE(SUM('Table 1'[Value]),FILTER('Table 1','Table 1'[ID (Primary Key)] in VALUES('Table 2'[ID])))

2.PNG

Best Regards,

Xue Ding

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

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
amitchandak
Super User
Super User

Hi @amitchandak : thanks for your input. GROUPBY() is not appropriate here as it returns a new table not as a measure and CURRENTGROUP() can operate only on aggregate functions like SUMX.


To give some idea, here is the look of the measure I'm working but this is wrong..
Final Sum =
CALCULATE(

ADDCOLUMNS(
DISTINCT (Table 2[ID]),
"Result",
var T1_ = SELECTCOLUMNS (Table 1, "Total Value", [Value])
var T2_ = DISTINCT('Table 2'[ID])
RETURN
SUM(INTERSECT (T1_, T2_))
),
[Result]
)

NOTE: This is simplified form of the problem of the complex scenario, I'm working on. If the expected output is not in the form of a measure, it's not really helpful for me. Thanks for understanding.

Hi @parameswaran ,

Can you create a relationship between two tables?  If you can, I created a sample that maybe help you.

1.PNG

Measure = CALCULATE(SUM('Table 1'[Value]),FILTER('Table 1','Table 1'[ID (Primary Key)] in VALUES('Table 2'[ID])))

2.PNG

Best Regards,

Xue Ding

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

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks @v-xuding-msft : your input helps me.

Measure = CALCULATE(SUM(Table 1[Value]), Table 1[ID] IN VALUES(Table 2[ID]))
@v-xuding-msft : this also worked for me.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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