Forum Discussion

powerbi2srm's avatar
powerbi2srm
Icon for Resolver II rankResolver II
3 years ago
Solved

Power BI doesn’t recognize a relationship

I have three tables: - dim_date: a calendar table - hr_employee: info abouts employees - hr_contract: info about employee contracts. It's related to hr_employee by "employee_id" and related to dim...
  • AmiraBedh's avatar
    3 years ago

    The issue you're experiencing is that the relationships and filter contexts are not being propagated through the model in the way you expect. DAX filter context is all about relationships and how tables relate to one another.

    Given the model you've described, you have the following relationships:

    1. `hr_employee` and `hr_contract` are related via `employee_id`.
    2. `hr_contract` and `dim_date` are related via multiple date IDs.

    The issue with the measure using `hr_employee` and `hr_contract` as separate tables is that, in DAX, the filter context doesn't just "jump" across tables. The relationship between `dim_date` and `hr_contract` doesn't mean that a change in the date selection will automatically affect the `hr_employee` table, which is why you're seeing a constant value.

    On the other hand, when you have a single table (`all_info`), all of the fields are in the same table, so filter contexts apply more intuitively to your formula.

    Double check that `hr_employee` is related to `hr_contract` on `employee_id` and `hr_contract` has relationships to `dim_date` on the appropriate date ID fields.
    Modify your measure to incorporate the relationships between tables. This may mean ensuring that your logic takes into account the relationships.

     

    num_employees_end_period =
    VAR min_selected_date = MIN(dim_date[date_id])
    VAR max_selected_date = MAX(dim_date[date_id])
    
    VAR ContractsWithinPeriod =
    FILTER(
    hr_contract,
    OR(
    hr_contract[max_end_date_id] = 0,
    hr_contract[max_end_date_id] >= min_selected_date
    ) &&
    hr_contract[min_start_date_id] <= max_selected_date
    )
    
    VAR result =
    CALCULATE(
    DISTINCTCOUNT(hr_employee[employee_id]),
    ContractsWithinPeriod
    )
    
    RETURN result