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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Change payment every month

hello i have 2 tables

attendance:

IdNameDateClock inClock Out
1Lebron06/01/22  07:0015:00
1Lebron07/01/22  07:0015:00
1Lebron08/01/22  07:0015:00
2Ari06/01/22  07:0015:00
2Ari07/01/22  07:0015:00
2Ari08/01/22  07:00

15:00

 

 

and employee table:

 

IdNameDatePayment per hour
1Lebron06/01/22  1
1Lebron07/01/22  1.25
1Lebron08/01/22  1.75
2Ari06/01/22  2
2Ari07/01/22  2.5
2Ari08/01/22

  3

 

i want to calculate pay for each employee bknowing each month the pmt per hour changes:

when i tried, i got an error because relationships are many to many so i cant use the RELATED syntax

 

please help

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Anonymous 
Here is a sample file with the propsed solution https://www.dropbox.com/t/i3qSlzaPNNMgXE9D

4.png1.png3.png

Monthly Salary Column = 
VAR CurrentRate = employee[Payment per hour]
VAR CurrentMonth = MONTH (  employee[Date] )
VAR CurrentYear = YEAR ( employee[Date] )
RETURN
    SUMX (
        FILTER ( 
            RELATEDTABLE ( attendance ), 
            MONTH ( attendance[Date] ) = CurrentMonth
                && YEAR ( attendance[Date] ) = CurrentYear
        ),
        VAR Clockin = attendance[Clock in]
        VAR Clockout = attendance[Clock Out]
        VAR NumberOfHours = DATEDIFF ( Clockin, Clockout, HOUR )
        RETURN
            CurrentRate * NumberOfHours
    )
Monthly Salary Measure = 
SUMX (
    employee,
    VAR CurrentRate = employee[Payment per hour]
    VAR CurrentMonth = MONTH (  employee[Date] )
    VAR CurrentYear = YEAR ( employee[Date] )
    RETURN
        SUMX (
            FILTER ( 
                RELATEDTABLE ( attendance ), 
                MONTH ( attendance[Date] ) = CurrentMonth
                    && YEAR ( attendance[Date] ) = CurrentYear
            ),
            VAR Clockin = attendance[Clock in]
            VAR Clockout = attendance[Clock Out]
            VAR NumberOfHours = DATEDIFF ( Clockin, Clockout, HOUR )
            RETURN
                CurrentRate * NumberOfHours
        )
)

View solution in original post

3 REPLIES 3
tamerj1
Super User
Super User

Hi @Anonymous 
Here is a sample file with the propsed solution https://www.dropbox.com/t/i3qSlzaPNNMgXE9D

4.png1.png3.png

Monthly Salary Column = 
VAR CurrentRate = employee[Payment per hour]
VAR CurrentMonth = MONTH (  employee[Date] )
VAR CurrentYear = YEAR ( employee[Date] )
RETURN
    SUMX (
        FILTER ( 
            RELATEDTABLE ( attendance ), 
            MONTH ( attendance[Date] ) = CurrentMonth
                && YEAR ( attendance[Date] ) = CurrentYear
        ),
        VAR Clockin = attendance[Clock in]
        VAR Clockout = attendance[Clock Out]
        VAR NumberOfHours = DATEDIFF ( Clockin, Clockout, HOUR )
        RETURN
            CurrentRate * NumberOfHours
    )
Monthly Salary Measure = 
SUMX (
    employee,
    VAR CurrentRate = employee[Payment per hour]
    VAR CurrentMonth = MONTH (  employee[Date] )
    VAR CurrentYear = YEAR ( employee[Date] )
    RETURN
        SUMX (
            FILTER ( 
                RELATEDTABLE ( attendance ), 
                MONTH ( attendance[Date] ) = CurrentMonth
                    && YEAR ( attendance[Date] ) = CurrentYear
            ),
            VAR Clockin = attendance[Clock in]
            VAR Clockout = attendance[Clock Out]
            VAR NumberOfHours = DATEDIFF ( Clockin, Clockout, HOUR )
            RETURN
                CurrentRate * NumberOfHours
        )
)
ribisht17
Super User
Super User

@Anonymous 

 

Turning the data model to 

ribisht17_0-1658592898001.png

 

so that you can avoid Many-Many relationship (best practice)

 

ribisht17_1-1658592939226.png

 

Regards,

Ritesh

 

 

vapid128
Solution Specialist
Solution Specialist

add an index colnum on both table.

ID&YYMM = [ID]&"_"&FORMAT([Date],"YYMM")

 

And Link those 2 index colnums

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.