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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors