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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
AnonJ
New Member

Not directly related table using in calculation

I have the below table layout and  I created the datamodel based on the columns highlighted in the yellow. Basically in the contract table in the center contractid is primary key But both  invoice and contractsnapshot table both can have many records for same contractid.

 

The result I would like to get is the last green table. Everything ok except the disticntcontractcount field.  I am not able to get to the invoice.invyr in the formula its throwing error.  Please help me how to solve this ?

 

DistinctContractcount= CALCULATE(DISTINCTCOUNT(ContractSnapshot[ContractID]),ContractSnapshot[SnapshotYear]=Related(Invoice[InvYr]))

 

 

AnonJ_2-1729072089452.png

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION
_AAndrade
Super User
Super User

Hi @AnonJ,

Please try this: 

 

VAR _Year = MAX(Invoice[InvYr]
VAR _Result = 
     CALCULATE(
               DISTINCTCOUNT(ContractSnapshot[ContractID])
               FILTER(
                      ALL(ContractSnapshot),
                      ContractSnapshot[SnapshotYear] = _Year
                )
     )

RETURN
  _Result

 





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




View solution in original post

3 REPLIES 3
_AAndrade
Super User
Super User

Hi @AnonJ,

Please try this: 

 

VAR _Year = MAX(Invoice[InvYr]
VAR _Result = 
     CALCULATE(
               DISTINCTCOUNT(ContractSnapshot[ContractID])
               FILTER(
                      ALL(ContractSnapshot),
                      ContractSnapshot[SnapshotYear] = _Year
                )
     )

RETURN
  _Result

 





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




AnonJ
New Member

Thank you , sorry its not working, its gives me 3 contracts for both 2021 and 2022 years where it should be 3 for 2021 and 2 for 2022.  Secondly, I am unable to establish a relationship between invoice and contractsnapshot. The relationship to these tables I set up using the middle table contract

Kedar_Pande
Super User
Super User

Try:

DistinctContractCount = 
CALCULATE(
DISTINCTCOUNT(ContractSnapshot[ContractID]),
ContractSnapshot[SnapshotYear] = MAX(Invoice[InvYr]) // Reference the current invoice year context
)

Verify that relationships among the tables are set appropriately, especially between Invoice and ContractSnapshot.

 

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors