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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dadorno
Regular Visitor

DAX Calculations

Before I begin, any help is greatly appreciated, so thank you in advance.  

 

I would like to calculate the average cost and total amount, by ID, where ID's from File 1, match to File 2, but where the Companies are not alike.  So in the below example, ID 1 would calculate an average cost of 12.5 (10+15) and total of 16,000.  ID 2 would have 25 and 20,000.  (Company A excluded from the math in File 2).  

 

I know I need a relationship between the tables by ID.  Beyond that, not sure how to get the DAX to work.  The end goal would be to have this automated, so that I can replace File 1 with any single company information.  


Thanks again 

 

 

File 1
CompanyID
A1
A2
A3
A4
A5

 

File 2
CompanyIDCostAmount
A1104000
B1106000
C11510000
A2202000
B22520000
1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @dadorno ,

 

Establish a one to many relatioship between two tables based on [ID] column.

1.PNG

 

Create measures as follows.

Total Cost =
CALCULATE (
    SUM ( File2[Cost] ),
    FILTER (
        File2,
        File2[Company] <> LOOKUPVALUE ( File1[Company], File1[ID], File2[ID] )
    )
)

Average cost =
[Total Cost]
    / CALCULATE (
        COUNT ( File2[ID] ),
        FILTER (
            File2,
            File2[Company] <> LOOKUPVALUE ( File1[Company], File1[ID], File2[ID] )
        )
    )

Total Amount =
CALCULATE (
    SUM ( File2[Amount] ),
    FILTER (
        File2,
        File2[Company] <> LOOKUPVALUE ( File1[Company], File1[ID], File2[ID] )
    )
)

2.PNG

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
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

1 REPLY 1
v-yulgu-msft
Employee
Employee

Hi @dadorno ,

 

Establish a one to many relatioship between two tables based on [ID] column.

1.PNG

 

Create measures as follows.

Total Cost =
CALCULATE (
    SUM ( File2[Cost] ),
    FILTER (
        File2,
        File2[Company] <> LOOKUPVALUE ( File1[Company], File1[ID], File2[ID] )
    )
)

Average cost =
[Total Cost]
    / CALCULATE (
        COUNT ( File2[ID] ),
        FILTER (
            File2,
            File2[Company] <> LOOKUPVALUE ( File1[Company], File1[ID], File2[ID] )
        )
    )

Total Amount =
CALCULATE (
    SUM ( File2[Amount] ),
    FILTER (
        File2,
        File2[Company] <> LOOKUPVALUE ( File1[Company], File1[ID], File2[ID] )
    )
)

2.PNG

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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