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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Kirt1965
Helper III
Helper III

New PowerBI User needs help with a M-M relationship

Hello All,

Hoping someone can help a Newbie with what I hope is a simple solution.  I’ve been using SQL for 30+ years, but just started using Power BI a couple weeks ago.  I have a situation I know how to handle in an SQL query, but I don’t know how to do what I need in Power BI.

 

We have a timecard application.  Every employee charges to a charge code.  Every employee has a contract record.  The contract record contains the employee charge rate, SOC code, and start and end dates for each period of a contract.  For example, you can see below that Mickey Mouse worked from 1/1/2017 through 12/31/2017 at a rate of 35 with SOC P170.  The next year his rate was 45 with SOC code M020.

 

Capture.PNG

The Contract table to Timecharge table relationship is many to many.  How do I get the ONE contract record that goes with a timecharge record?  I need the contract record where an employee’s start and end dates are before and after the charge date, respectively.  For example, Mickey’s charge on 2/12/2017 goes with the contract record with a SOC code of P170 because the charge date is within that contract record’s start and end date.  But his charge on 4/4/2018 goes with the M020 contract record because of a different date.

 

How can I accomplish this?  Hints, etc, very much appreciated.  Please keep it fairly simply since I'm new.

 

Thank you very much,
Kirt

 

 

1 ACCEPTED SOLUTION
jdbuchanan71
Super User
Super User

Hello @Kirt1965 

The behavior of a visual is to only show rows where the measures are not blank.  We can us that along with a measure to compare the ChargeDate to the Start_Date and End_Date like so.

 

Match? = 
VAR _ChareDate =
    SELECTEDVALUE ( TIMECHARGE[ChargeDate] )
RETURN
    IF (
        NOT ISBLANK (
            CALCULATE (
                COUNTROWS ( CONTRACT ),
                CONTRACT[Emp_Start_Dt] <= _ChareDate,
                CONTRACT[Emp_End_Dt] >= _ChareDate
            )
        ),
        "Yes"
    )

 

This will show "Yes" only on the matching rows:
2020-04-20_13-40-44.jpg

I have attached my sample .pbix for you to look at.

 

View solution in original post

1 REPLY 1
jdbuchanan71
Super User
Super User

Hello @Kirt1965 

The behavior of a visual is to only show rows where the measures are not blank.  We can us that along with a measure to compare the ChargeDate to the Start_Date and End_Date like so.

 

Match? = 
VAR _ChareDate =
    SELECTEDVALUE ( TIMECHARGE[ChargeDate] )
RETURN
    IF (
        NOT ISBLANK (
            CALCULATE (
                COUNTROWS ( CONTRACT ),
                CONTRACT[Emp_Start_Dt] <= _ChareDate,
                CONTRACT[Emp_End_Dt] >= _ChareDate
            )
        ),
        "Yes"
    )

 

This will show "Yes" only on the matching rows:
2020-04-20_13-40-44.jpg

I have attached my sample .pbix for you to look at.

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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